From: Lorry Astra on
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
From: Anthony Wieser on



"Lorry Astra" <LorryAstra(a)discussions.microsoft.com> wrote in message
news:3A130D2B-C855-4E1E-A879-855076DD30ED(a)microsoft.com...
> 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.
>
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

From: Martin B. on
On 29.05.2010 14:08, Lorry Astra wrote:
> 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.
>

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
From: Ulrich Eckhardt on
Lorry Astra wrote:
> 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.


> By the way, I know the correct way is:
[...]
> 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