开发者

Simple c malloc

this doesn't work:

void function(char* var)
{
    var = (char*) malloc (100);
}

int main()
{
    char* str;
    function(str);
    strcpy(str, "some random stri开发者_StackOverflowng");
    printf("%s\n", str);

    return 0;
}

this does:

void function(char* var)
{
    //var = (char*) malloc (100);
}

int main()
{
    char* str;
    //function(str);
    str = (char*) malloc (100);
    strcpy(str, "some random string");
    printf("%s\n", str);

    return 0;
}

Why?


You have to pass the address of the pointer to assign the address you want inside the function, otherwise you are just passing a copy of it:

void function(char** var)
{
    *var = malloc (100);
}

int main()
{
    char* str;
    function(&str);
    strcpy(str, "some random string");
    printf("%s\n", str);

    return 0;
}


You need to use a pointer to pointer as a parameter for that, char **.
When you pass a pointer as a parameter, it gets copied, so what you get inside the function is just another pointer pointing to the same place.

You need to receive a char ** and do:

my_alloc_fun(void **ptr){
    *ptr= malloc(size);
}
void *myptr;
my_alloc_fun(&myptr);

This way, you get the address of the pointer inside the function, so that you can make the original pointer to point to the newly allocated memory.


When you call function(str), you are passing the value of str to function. This value is some uninitialized garbage value because you haven't set it to anything in main, but that's not the problem.

The problem is that function has its own pointer, var, into which it puts that garbage value so that one could say var points to the same thing (garbage) that str points to. However, they are not the same variable. They are two distinct variables, and changing var has no effect on str.

When you say var = (char*) malloc (100);, you are allocating memory somewhere and then telling var to point to it. Now var points to a different location than str. Immediately you return from that function, losing var and the location of that memory. This by the way is a memory leak.

When you return back to main, str is as it ever was - pointing to garbage.

A numerical example:

char *str;     // str -> 0xfeedface   (garbage)
function(str);

// inside 'function', an initialization like this occurs
char *var = str;  // var -> 0xfeedface (same garbage)
var = (char*) malloc (100); // var -> 0x12341234 (alloc'd memory)
return;

// back in 'main'
strcpy(str, "some random string"); // str still points to 0xfeedface!

To do this properly, you need to change strs value. This means function needs a pointer to str, or a pointer to a pointer.

void function(char **var)
{
   *var = (char*)malloc(sizeof(char) * 100);
}

int main()
{
   char *str;
   function(&str);
   strncpy(str, "some random string", 99);
   ...
   free(str);  // important in general
}


Pointers are just like any other variable; the difference is only in what their value represents. Whereas a double has a value that represents a double-precision floating point number, and an int has a value that represents a signed integer, a pointer has a value that represents the location of another variable.

Pointers themselves are still passed by-value to other functions; so your example doesn't work for exactly the same reason that this analagous function doesn't work:

void function(int var)
{
    var = 100;
}

int main()
{
    int num;
    function(num);
    printf("%d\n", num);

    return 0;
}

The answer in both cases is the same: When function() changes the value of the parameter var, it is changing only its local copy of that - it isn't changing the variable within main().


See my answer to C Programming: malloc() inside another function for an explanation about how to think about this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜