about memory leaks with this simple program

This is a discussion on about memory leaks with this simple program within the C++ Programming forums, part of the General Programming Boards category; will this cause memory leaks if I don't make the variables become pointers? or will the compiler automatically do this ...

  1. #1
    Unregistered
    Guest

    about memory leaks with this simple program

    will this cause memory leaks if I don't make the variables become pointers? or will the compiler automatically do this for me? I am using Visual C++ 6.0.

    Code:
    #include<iostream.h>
    
    int main()
    {
    int number = 255;
    return 0;
    }

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    look here

    i think it depends how you use it after it points and before you delete

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    No and no, there is no need for pointers in that 'program'

  4. #4
    Unregistered
    Guest
    so the variable won't be memory resident after I terminate the program? so why are pointers needed?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    you only get memory leaks if you forget to delete unnecessary pointers.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Good question, the answer will not become apparent until you get a little further in C++. What is a pointer? A pointer is some that points to an address of where a datatype is stored. Take the following code for example.
    Code:
    int main( void )
    {
         int nNum = 10; // Initialize nNum to 10 from Stack
         
         // Initialize a pointer to an int , and set to NULL
         int *pNum = NULL;
         // Set what pNum points to as the address of nNum     
         pNum = &nNum;
         
         // If we want the address of nNum
         cout << "Address = " << pNum << endl;
         
         // If we dereference the pointer, we get the data
         cout << "Data = " << *pNum << endl;
         return ( 0 );
    }
    Alas, I'm probably not the best person to explain this, but say you have a very large Class. Whenever you want a function to manipulate or use data from your class you must pass it as a paramter. Say we have Enemy as a class.

    void Foo( int nSomething, Enemy Temp );

    This would make a copy of the WHOLE class and pass it to the function. This is bad on numerous levels. Lots of calls to constructors and such and just very slow. We would like to just give the function the address of where the class is and let it manipulate the ACTUAL data instead of a copy.

    void Foo( int nSomething, Enemy &Temp );

    That's basically one reason pointers are good. Another is you have an array and won't know until run-time how many elements you will need. Consider the following code.

    Code:
    void Foo( int nItems )
    {
         char Array[ nItems ]; // This will NOT compile!!
         return;
    }
    
    void Foo( int nItems )
    {
         char *pArray = new char [ nItems ]; // This works
         . // Something
         . // Something
         . // Something
         delete [] pArray; // Make sure you always free it
         return;
    }
    These might not make that much sense to you now, but they should become clearer as time passes. Hope I didn't mess anything up while explaining. Perhaps someone else can explain it more clearly.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

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