Results 1 to 5 of 5

Thread: Pointers

  1. #1

    Thread Starter
    Lively Member slx47's Avatar
    Join Date
    Apr 2002
    Location
    US
    Posts
    127

    Pointers

    ok i want to make a function and use a pointer and make it in free store part of the memory like this:

    #include <iostream.h>

    void hi();

    int main ()

    {
    cout<<"hi"<<endl;
    hi();
    cout<<pAge<<endl;

    /* it doesnt remember pAge when it should be in the free store part of memory which means it should stay in memory right ? */

    }

    void hi()

    {
    int * pAge = new short int;
    *pAge = 6;

    }

    why is pAge deleted from memory when i put it in free store where it shouldnt be deleted ?

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    That is because of your function:
    PHP Code:
    void hi() 


    int pAge = new short int
    *
    pAge 6


    You are trying to reinitialize the pointer as a new integer inside the function and the function will delete the pointer as soon as it's returned. So you have nothing left in the point after you call the function.

    A working approach would be like this:
    PHP Code:
    #include <iostream.h> 

    void hi(); 

    int main () 


    int pAge = new short int;  //initialize the pointer outside the function
    cout<<"hi"<<endl
    hi(); 
    cout<<pAge<<endl

    /* it doesnt remember pAge when it should be in the free store part of memory which means it should stay in memory right ? */ 



    void hi() 


    //just set the pointer's value
    *pAge 6

    Baaaaaaaaah

  3. #3

    Thread Starter
    Lively Member slx47's Avatar
    Join Date
    Apr 2002
    Location
    US
    Posts
    127
    ok now I see what was wrong. Thanks for the reply.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It doesn't free the memory, it just forgets the pointer. You've created a fine memory leak here...

    BTW, abdul's code won't work either because the identifier pAge is local to the function main and can't be used in hi.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    And note that you should be using the Standard C++ Library headers (i.e. <iostream> not <iostream.h>).


    And yes, the campaign starts here
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width