Discussion:
Memory allocation
(too old to reply)
Lorry Astra
2010-05-29 12:08:01 UTC
Permalink
Hi friends,
here's a code sample:

void mallocate(char*);
int main()
{
char* c = NULL;
mallocate(c);
// strcpy(c,"ab"); Here "strcpy" is an error, because c is still NULL
return 0;
}

void mallocate(char* p)
{
p = malloc(3*sizeof(char));
return;
}

In my opinion, I pass a char pointer from "main" to "mallocate", that means
I pass an pointer to "mallocate", In "mallocate", I allocate memory for
pointer "p", but why I can not get it from "main" function? I think I don't
grasp the root cause, Could anybody describe for me? Thank you.

==============================
By the way, I know the correct way is:
void mallocate(char** p)
{
*p = malloc(3*sizeof(char));
return;
}
or
char* mallocate(char* p)
{
p = malloc(3*sizeof(char));
return p;
}
===============================
Thank you.



Lorry
Anthony Wieser
2010-05-29 13:19:45 UTC
Permalink
Post by Lorry Astra
Hi friends,
void mallocate(char*);
int main()
{
char* c = NULL;
mallocate(c);
// strcpy(c,"ab"); Here "strcpy" is an error, because c is still NULL
return 0;
}
void mallocate(char* p)
{
p = malloc(3*sizeof(char));
return;
}
In my opinion, I pass a char pointer from "main" to "mallocate", that means
I pass an pointer to "mallocate", In "mallocate", I allocate memory for
pointer "p", but why I can not get it from "main" function? I think I don't
grasp the root cause, Could anybody describe for me? Thank you.
Your problem is you are passing p by value, so the copy of p's pointer is
passed to mallocate. There's no way that the original value of p can be
modified.

Declared as a reference
void mallocate(char* &p)
your code would work fine.
--
Anthony Wieser
Wieser Software Ltd
Martin B.
2010-05-29 13:32:00 UTC
Permalink
Post by Lorry Astra
Hi friends,
void mallocate(char*);
int main()
{
char* c = NULL;
mallocate(c);
// strcpy(c,"ab"); Here "strcpy" is an error, because c is still NULL
return 0;
}
void mallocate(char* p)
{
p = malloc(3*sizeof(char));
return;
}
In my opinion, I pass a char pointer from "main" to "mallocate", that means
I pass an pointer to "mallocate", In "mallocate", I allocate memory for
pointer "p", but why I can not get it from "main" function? I think I don't
grasp the root cause, Could anybody describe for me? Thank you.
You should read a pointer _and_ argument passing tutorial on the C language.

Short answer:
The "p" inside mallocate is a var that holds an address. (as is the
different "c" in the main fn.)
It is a different var than the "c" passed to it (pass by value).
You assign the _address_ that malloc returns to this "p" variable.
This does in no way influence the "c" var as the _value_ of the "c" was
copied into the "p" when you invoked mallocate.

br,
Martin
Ulrich Eckhardt
2010-05-31 06:58:33 UTC
Permalink
Post by Lorry Astra
void mallocate(char*);
int main()
{
char* c = NULL;
mallocate(c);
// strcpy(c,"ab"); Here "strcpy" is an error, because c is still
NULL return 0;
}
void mallocate(char* p)
{
p = malloc(3*sizeof(char));
return;
}
In my opinion, I pass a char pointer from "main" to "mallocate", that
means I pass an pointer to "mallocate", In "mallocate", I allocate memory
for pointer "p", but why I can not get it from "main" function?
Others already explained it, but I'll take another shot. Your misconception
is that you "allocate memory for pointer p". You allocate memory, and store
the address of that memory in p. So, the pointer value stored in "p" is a
result of the malloc() call, there is no other inherent connection between
that memory and "p", let alone to the "c" in the calling function.
[...]
Post by Lorry Astra
char* mallocate(char* p)
{
p = malloc(3*sizeof(char));
return p;
}
This one is stupid, because it receives a pointer value that is not used
anywhere.

BTW: The definition of sizeof or the size that malloc() takes is in
multiples of the size of a "char". That means that "char" has (by
definition!) the size 1.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Loading...