Joni
Joni

Reputation: 215

dynamic allocation and returning a local variable

In this piece of code I'm trying to create a list which contains all the chars form an input file, and my main problem is with the sentence "You can't return a local variable of the function" Iv'e been told which made me very confused. I dynamically allocated a List and return it, can I just define List list without a dynamic allocation and return it? I believe it's wring since all the information would be automatically deleted and I would be left only with the address of the original list I created.

Here's the code for more information:

typedef struct Item {
    char tav;
    struct Item* next;
} Item;

typedef struct List {
    Item* head;
} List;

List* create(char* path) {
    FILE* file;
    List* list;
    Item* trav;
    Item* curr;
    char c;

    file=fopen(path, "r");
    if (file==NULL) {
        printf("The file's not found");
        assert(0);
    }

    if (fscanf(file, "%c", &c)!=1) {
        printf("The file is empty");
        assert(0);
    }
    trav=(Item *)calloc(1, sizeof(Item));
    trav->tav=c;
    list=(List *)calloc(1, sizeof(List)); /* allocating dynamiclly the list so it won't be lost at the end of the function*/
    list->head=trav;

    while (fscanf(file, "%c", &c)==1) {
        curr=(Item*)calloc(1, sizeof(Item));
        curr->tav=c;
        trav->next=curr;
        trav=curr;
    }
    trav->next=NULL;

    fclose(file);

    return list;

}

Am I correct? Is this necessary? can I define List instead a pointer to one a return it?

Upvotes: 0

Views: 554

Answers (2)

Shahbaz
Shahbaz

Reputation: 47533

You can't return a local variable of the function

This sentence is completely wrong. For example in this function:

int f(void)
{
    int x = 5;
    return x;
}

is a perfectly valid function in which you are returning a local variable.

What you should know is that you can't (or better say shouldn't) return the address of a local variable of the function. This is because after the function returns the address points to garbage and is not usable anymore.

In your example, you can very safely define a local List and return it.

Note that you still need to dynamically allocate trav, i.e. you can't take a local variable of type Item and the point list->head to it for the same reason above.

Upvotes: 1

Alok Save
Alok Save

Reputation: 206536

You cannot return a pointer to variable local to the function. The local variable does not live beyond the scope({,}) of the function.
Returning address of a variable local to a function will give you what is called as an Undefined Behavior.

You can very well return:

  • a local variable by value or
  • a pointer pointing to dynamically allocated memory

Prefer the first unless you are really bothered about the memory overhaed due to returning a copy.

can I just define List list without a dynamic allocation and return it?

Yes,
Provided you have:

List create(char* path); 

Upvotes: 1

Related Questions