This is an absolutly noob question, but I don't seem to find an appropiate answer anywhere so here it goes, given this code:
#include <stdio.h>
#include <stdlib.h>
void initialize( char * buffer)
{
buffer = malloc(1);
*buffer='a';
}
int main(void) {
char * buff;
buff = malloc(sizeof(char));
*buff = 'b';
initialize(buff);
puts("Contents of buffer are: ");
puts(buff);
return 0;
}
The output of main is always 'b', which is really weird to me. I thought that by passing a pointer to initialize I could modify the value of the pointer declared in main, but for some reason it seems to be passing the variable by value, and when returning I have the values specified in main, thus 'b'.
I would like to know why is this...should I pass a reference to a pointer instead? something like char *& ??
Regards, Alex
You need to modify your
initialize()
function to take a pointer to your buffer and pass in the address of the pointer. Although it receives the buffer you pass in, you cannot change whatbuff
is pointing to that way. Just be aware that you will have a memory leak in this case and you cannot print the contents as a string as it isn't properly null terminated.Answers to the comments below:
When I read your code, I thought that you wanted to change what the
buff
variable (inmain()
) was pointing to, a new buffer. I thought that when I saw that you were allocating a new buffer in yourinitialize()
function. To be able to change a value of a variable from a different location (or function), you'd need the address to that variable. Since it was originally a character pointerchar *
, you would have needed a pointer to that character pointer,char **
. And yes, this is like a reference in C++, only more verbose.If you had intended to just modify the contents of the buffer that was passed in, Krtek covers that. All you needed to do was leave out the new allocation of the buffer and modify what the buffer was pointing to.
Actually, the memory leak was caused by allocating a new buffer while not freeing the previously allocated memory. You effectively had this:
This was a different issue here.
puts()
takes a null-terminated string and prints it to standard output followed by a new line. It wouldn't be a memory leak to print potentially garbage text, it's just... something else. Your buffer only contained space for a single character (without the null-terminator). You therefore shouldn't useputs()
to print the contents of the buffer. You should only be printing that single character. The use ofprintf()
with the'\n'
was to end up with the same output behavior asputs()
.You modified the copy of your pointer to point in another place, but your real pointer doesn't point to it. If you wan't your code to work use this function.
then call
initialize(&buff);
When you do this
The buffer variable passed originally from main is unaffected as it has been passed by value.
If you write this:
Then you will see the change.
Edit1: You need to understand * operator. buffer is a pointer in itself. *buffer is actually the location pointed to by buffer and changing it doesn't affect buffer actually.
The first thing you do in your function it to reassign another place in the memory to
buffer
with a newmalloc
. Then you modify this new memory location, butbuff
in the main function isn't modified, since it's still pointing on the old location.If you remove the malloc, it will work :
Now we are modifying the initial memory location.
If you want the called function to affect the value of
buff
you need to pass its address:and call it as:
but note that this will leak the memory allocated in main. Since you've already allocated memory in main you can just remove the malloc:
In case a bug-free snippet without memory leaks is preferred, here are some alternatives to what has already been posted, more applicable to programming in the real world.
Note that the same "code module" that mallocs should also free. Do not malloc in one module and free in another, that is very bad practice!
Version 1:
Version 2:
my_functions.h
my_functions.c