Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > any wrong with this piece of code?

Reply
Thread Tools

any wrong with this piece of code?

 
 
YYweii
Guest
Posts: n/a
 
      03-14-2008
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);
...
...
}
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-14-2008
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);
> ...
> ...
> }


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


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-14-2008
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];


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

--
Ian Collins.
 
Reply With Quote
 
YYweii
Guest
Posts: n/a
 
      03-14-2008
I see. Many thanks!
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      03-14-2008
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.

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.
--
Jim Langston
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
utab
Guest
Posts: n/a
 
      03-14-2008
On Mar 14, 11:23 am, "Jim Langston" <(E-Mail Removed)> wrote:
> 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?

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.
> --
> Jim Langston
> (E-Mail Removed)


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-14-2008
utab wrote:
>
> 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!

--
Ian Collins.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      03-15-2008
utab wrote:
> On Mar 14, 11:23 am, "Jim Langston" <(E-Mail Removed)> wrote:
>> 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..


--
Jim Langston
(E-Mail Removed)


 
Reply With Quote
 
utab
Guest
Posts: n/a
 
      03-15-2008
On Mar 15, 1:24 am, "Jim Langston" <(E-Mail Removed)> wrote:
> utab wrote:
> > On Mar 14, 11:23 am, "Jim Langston" <(E-Mail Removed)> wrote:
> >> 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..
>
> --
> Jim Langston
> (E-Mail Removed)


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

The point I missed was the copy of the pointer.

Thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's wrong with this piece of code? xz C++ 1 03-13-2008 04:07 PM
Download a file piece by piece Patrick Plattes Ruby 2 11-30-2006 07:48 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
what's wrong with this piece of code peter.linotte@gmail.com VHDL 3 06-11-2006 11:21 AM
Piece of mind to anyone who wishes to know if there are any installations happening behind your back ry@DundasValley Computer Security 6 10-28-2003 04:00 AM



Advertisments