any wrong with this piece of code?

Discussion in 'C++' started by YYweii, Mar 14, 2008.

  1. YYweii

    YYweii Guest

    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);
    ...
    ...
    }
     
    YYweii, Mar 14, 2008
    #1
    1. Advertising

  2. YYweii

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Mar 14, 2008
    #2
    1. Advertising

  3. YYweii

    Ian Collins Guest

    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.
     
    Ian Collins, Mar 14, 2008
    #3
  4. YYweii

    YYweii Guest

    I see. Many thanks!
     
    YYweii, Mar 14, 2008
    #4
  5. YYweii

    Jim Langston Guest

    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
     
    Jim Langston, Mar 14, 2008
    #5
  6. YYweii

    utab Guest

    On Mar 14, 11:23 am, "Jim Langston" <> 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
    >
     
    utab, Mar 14, 2008
    #6
  7. YYweii

    Ian Collins Guest

    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.
     
    Ian Collins, Mar 14, 2008
    #7
  8. YYweii

    Jim Langston Guest

    utab wrote:
    > On Mar 14, 11:23 am, "Jim Langston" <> 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
     
    Jim Langston, Mar 15, 2008
    #8
  9. YYweii

    utab Guest

    On Mar 15, 1:24 am, "Jim Langston" <> wrote:
    > utab wrote:
    > > On Mar 14, 11:23 am, "Jim Langston" <> 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
    >


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

    The point I missed was the copy of the pointer.

    Thanks.
     
    utab, Mar 15, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Przemo
    Replies:
    5
    Views:
    541
    Teemu Keiski
    Jul 18, 2004
  2. Replies:
    3
    Views:
    640
  3. xz
    Replies:
    1
    Views:
    283
  4. Patrick Plattes

    Download a file piece by piece

    Patrick Plattes, Nov 30, 2006, in forum: Ruby
    Replies:
    2
    Views:
    224
    Patrick Plattes
    Nov 30, 2006
  5. Replies:
    5
    Views:
    149
Loading...

Share This Page