Memory leak With new

This is a discussion on Memory leak With new within the C++ Programming forums, part of the General Programming Boards category; For some reason my function isn't changing the pointer I send to it. Code: void insertOrder (dnode<T> *header, const T& ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    51

    Memory leak With new

    For some reason my function isn't changing the pointer I send to it.

    Code:
    void insertOrder (dnode<T> *header, const T& item){
    header=new dnode<T>(item);
    }
    //main
    dnode<int> *tree=NULL;
    
    insertOrder(tree,rando);//Random number from 0-99
    Whenever I run the program header keeps being NULL.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The whole point of & in a function prototype is "this function will change this variable". By contrast, if you don't have &....

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    try this

    Code:
    void insertOrder (dnode<T>* &header, const T& item)
    I made a similar question before , check it out

    dynamic array created in another method..

    you passed a pointer by value, you need to pass the pointer by reference if you intend to change with it points to.

    and also i am not experienced with this stuff that much but you might want to put a check in place to see if 'header' is currently NULL and if its not , then free the memory before allocating new memory to that pointer.
    Last edited by rodrigorules; 04-30-2010 at 08:23 PM.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    68

    a little info

    The reason why you must use the reference symbol above might become clear when you learn what a pointer actually is.

    A pointer is just a number. You could turn your pointer into an unsigned int type and use this instead. A pointer type in c/c++ is just to let you and the compiler know that the number contained is to be treated as a memory address. So, if you dont pass by reference, then all you are doing is changing the numeric value of header within the function call and nothing else.
    Code:
    void insertOrder (unsigned int header, const T& item){
    header=new dnode<T>(item);
    }

    So, I hope this explains why you must use this code instead. Pass by reference so the number returned by the new function actually is stored in the pointer you pass to the function.
    Code:
    void insertOrder (dnode<T>* &header, const T& item){
    header=new dnode<T>(item);
    }

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Quote Originally Posted by smasherprog View Post
    A pointer is just a number. You could turn your pointer into an unsigned int type and use this instead
    can you give example , i cant get this to compile

    Code:
    class A
    {
    };
    
    int main()
    {
    	unsigned int p = new A;
    }
    Code:
    invalid conversion from ‘A*’ to ‘unsigned int’

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can "turn your pointer into an unsigned int type".
    Code:
    double a = 4.0;
    double* b = &a; //can be pointer to whatever but just for example
    unsigned int c = (unsigned int) b; //turn your pointer into an unsigned int
    Not that is actually a good idea. But you can.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    68

    nooooooooooo

    I didn't mean to actually do it. I was just showing you why it is necessary to pass the pointer by reference. I was attempting to show you what a pointer type really was: a number in disguise. Do not attempt to do that!

    Some people have a difficult time understanding what pointers are and that leads to confusion over how to pass them around. If you understand what they really are, it makes it all clear.

    MM kay??
    Last edited by smasherprog; 04-30-2010 at 11:15 PM.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Ah yes & fixed it. I was forced to use the function prototype

    void insertOrder (dnode<T> *header, const T& item)

    I figured I wasn't passing the values correctly and it was only copying the address. thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 05:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21