any wrong with this piece of code?

Y

YYweii

Hi all

my teacher say there is something wrong with this piece of code, but i
can't figure it out, help !

void GetMemory(char *p, int num)
{
p = new char[num];
}

int main()
{
char *p;
GetMemory(p, 100);
...
...
}
 
K

Kai-Uwe Bux

YYweii said:
Hi all

my teacher say there is something wrong with this piece of code, but i
can't figure it out, help !

void GetMemory(char *p, int num)
{
p = new char[num];
}

int main()
{
char *p;
GetMemory(p, 100);
...
...
}

You pass the pointer p by value where you want to pass by reference.


Best

Kai-Uwe Bux
 
I

Ian Collins

YYweii said:
Hi all

my teacher say there is something wrong with this piece of code, but i
can't figure it out, help !

void GetMemory(char *p, int num)
{
p = new char[num];

Think very carefully about what this line does. What does changing p do?
 
J

Jim Langston

YYweii said:
Hi all

my teacher say there is something wrong with this piece of code, but i
can't figure it out, help !

void GetMemory(char *p, int num)
{
p = new char[num];
}

int main()
{
char *p;
GetMemory(p, 100);
...
...
}

Parameters in C and C++ are passed by value. The p inside of GetMemory is
different than the p inside of main. Whatever p contained is copied to the
local variable p in the subroutine. Any changes made to p in GetMemory,
therefore, are only visible to the p in GetMemory. You need access to the
pointer itself to change the pointer.

There are 2 ways to do this. In C it was customary to send a pointer to the
pointer.

void GetMemory( char**p, int num )
{
*p = new char[num];
}

That changes the pointer itself. You pass the address of the pointer to
GetMemory, then change the pointer so it's visible in main. In C++ this
will still work, but it is more common and probably better to pass a
reference to the pointer.

void GetMemory( char*&p, int num )
{
p = new char[num];
}

Just remember, whatever is passes as a parameter in C and C++ is *always* a
copy. References can be considered the exception to the rule. A reference
is an alias.
 
U

utab

YYweii said:
my teacher say there is something wrong with this piece of code, but i
can't figure it out, help !
void GetMemory(char *p, int num)
{
p = new char[num];
}
int main()
{
char *p;
GetMemory(p, 100);
...
...
}

Parameters in C and C++ are passed by value. The p inside of GetMemory is
different than the p inside of main. Whatever p contained is copied to the
local variable p in the subroutine. Any changes made to p in GetMemory,
therefore, are only visible to the p in GetMemory. You need access to the
pointer itself to change the pointer.

Pointers are always a bit difficult to understand for me. I have a
question on this:

p is a pointer to a char in which p contains an address, right?

And that address will be used to be the start address of the char
array, this is also right I guess. But the function is using a pointer
to a char as its parameter so passing an address should be ok. is not
passing p right conceptually, in main?

And in the function, p is char * so the new operator as well returns a
pointer to a char.

Or I mixed everything desperately :( ?
There are 2 ways to do this. In C it was customary to send a pointer to the
pointer.

void GetMemory( char**p, int num )
{
*p = new char[num];

}

That changes the pointer itself. You pass the address of the pointer to
GetMemory, then change the pointer so it's visible in main. In C++ this
will still work, but it is more common and probably better to pass a
reference to the pointer.

void GetMemory( char*&p, int num )
{
p = new char[num];

}

Just remember, whatever is passes as a parameter in C and C++ is *always* a
copy. References can be considered the exception to the rule. A reference
is an alias.
 
I

Ian Collins

utab said:
Pointers are always a bit difficult to understand for me. I have a
question on this:
That's where those of us who stared out as assembler programmers have
the edge!
p is a pointer to a char in which p contains an address, right?
Yes, the value of p is an address.
And that address will be used to be the start address of the char
array, this is also right I guess.

Once assigned, yes.
But the function is using a pointer
to a char as its parameter so passing an address should be ok. is not
passing p right conceptually, in main?
It is passing the value of p, that is the address p is currently
pointing to.
And in the function, p is char * so the new operator as well returns a
pointer to a char.
In the function, the parameter p is a copy of the pointer passed to the
function. In effect, it is a local variable within the function. It is
not the same as the value passes, it is a *copy* of the value. Remember
C and C++ pass by value. The function can change the data the pointer
points to (remember it's value is an address).

So all that is being changed within the function is the value of the
local copy of p.

If you want to modify main's p, you have to pass the address or a
reference to to the function. Remember the thing the function can
change is the data pointed to by the pointer, so if the parameter is a
pointer to a pointer, the value of the original pointer can be changed.
Or I mixed everything desperately :( ?
A wee bit, yes!
 
J

Jim Langston

utab said:
YYweii said:
my teacher say there is something wrong with this piece of code,
but i can't figure it out, help !
void GetMemory(char *p, int num)
{
p = new char[num];
}
int main()
{
char *p;
GetMemory(p, 100);
...
...
}

Parameters in C and C++ are passed by value. The p inside of
GetMemory is different than the p inside of main. Whatever p
contained is copied to the local variable p in the subroutine. Any
changes made to p in GetMemory, therefore, are only visible to the p
in GetMemory. You need access to the pointer itself to change the
pointer.

Pointers are always a bit difficult to understand for me. I have a
question on this:

p is a pointer to a char in which p contains an address, right?

Yes. In main p is uninitialized, but lets say you unitiliazed it to NULL.
In main, then main's p would be 0x0000
And that address will be used to be the start address of the char
array, this is also right I guess. But the function is using a pointer
to a char as its parameter so passing an address should be ok. is not
passing p right conceptually, in main?

Now, you pass p from main to your function. Your function then gets passed
the value 0x0000. So now the p in your function contains the same address
as the p in main.
And in the function, p is char * so the new operator as well returns a
pointer to a char.

Correct. Lets say that new returned the address 0x1234. So the p in your
function is changed to 0x1234. Notice, however, that the value was copied
to the function. The p in main still contains 0x0000
Or I mixed everything desperately :( ?

When the function exits the local variable p gets deleted. So even though
it's pointing to 0x1234, nothing is done with that value, it's not seen in
main since just the value of p was passed to the function..
 
U

utab

utab said:
YYweii wrote:
Hi all
my teacher say there is something wrong with this piece of code,
but i can't figure it out, help !
void GetMemory(char *p, int num)
{
p = new char[num];
}
int main()
{
char *p;
GetMemory(p, 100);
...
...
}
Parameters in C and C++ are passed by value. The p inside of
GetMemory is different than the p inside of main. Whatever p
contained is copied to the local variable p in the subroutine. Any
changes made to p in GetMemory, therefore, are only visible to the p
in GetMemory. You need access to the pointer itself to change the
pointer.
Pointers are always a bit difficult to understand for me. I have a
question on this:
p is a pointer to a char in which p contains an address, right?

Yes. In main p is uninitialized, but lets say you unitiliazed it to NULL.
In main, then main's p would be 0x0000
And that address will be used to be the start address of the char
array, this is also right I guess. But the function is using a pointer
to a char as its parameter so passing an address should be ok. is not
passing p right conceptually, in main?

Now, you pass p from main to your function. Your function then gets passed
the value 0x0000. So now the p in your function contains the same address
as the p in main.
And in the function, p is char * so the new operator as well returns a
pointer to a char.

Correct. Lets say that new returned the address 0x1234. So the p in your
function is changed to 0x1234. Notice, however, that the value was copied
to the function. The p in main still contains 0x0000
Or I mixed everything desperately :( ?

When the function exits the local variable p gets deleted. So even though
it's pointing to 0x1234, nothing is done with that value, it's not seen in
main since just the value of p was passed to the function..

your and Ian's explanation made things crystal clear for me, Thanks.

The point I missed was the copy of the pointer.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top