Thread: Question about the Basics of Memory Leakage

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Question about the Basics of Memory Leakage

    Code:
    #include "stdafx.h"
    #include <iostream.h>
    
    class Transportation
    {
    public:
    
    	double dCarEngineSize;
    	int iMotorcycleCCs;
    	int iSemiNumberofAxels;
    };
    
    int main(int argc, char* argv[])
    {
    	Transportation& vehicleInfo =  *(new Transportation);
    	vehicleInfo.dCarEngineSize = 3.1;
    	vehicleInfo.iMotorcycleCCs = 750;
    	vehicleInfo.iSemiNumberofAxels = 6;
    
    	cout <<	vehicleInfo.dCarEngineSize << endl <<
    			vehicleInfo.iMotorcycleCCs << endl <<
    			vehicleInfo.iSemiNumberofAxels << endl;
    	delete &vehicleInfo;
    	return 0;
    }
    It compiles, great, but is it doing what I want it to?

    I want to delete the object from the heap, so it doesn't cause a leak, is this correct? If not, what am I doing wrong?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    new returns a pointer, not a reference, and delete expects the same pointer that was returned by new. So, replace '&' with '*' on the new line and just remove '&' on the delete line. You will also have to use the pointer operator instead of the dot operator on all other lines.

    [edit]Or if you really really must use that dot operator[/edit]
    Code:
    int main(int argc, char* argv[])
    {
    	void* ptr = (void *)new Transportation;
    	Transportation& vehicleInfo =  *(Transportation*)ptr;
    	vehicleInfo.dCarEngineSize = 3.1;
    	vehicleInfo.iMotorcycleCCs = 750;
    	vehicleInfo.iSemiNumberofAxels = 6;
    
    	cout <<	vehicleInfo.dCarEngineSize << endl <<
    			vehicleInfo.iMotorcycleCCs << endl <<
    			vehicleInfo.iSemiNumberofAxels << endl;
    	delete ptr;
    	return 0;
    }
    Last edited by Ancient Dragon; 09-22-2005 at 10:22 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think Shamino's code is better than Ancient Dragon's. What's the point of using void*?

    Shamino, normally, you just use the pointer and operator-> instead of using a reference to the value. When most people see a reference they don't think that it will need to be deleted, so doing it might cause confusion. Your code will work, but is there a reason you don't just use the pointer.
    Code:
    int main(int argc, char* argv[])
    {
    	Transportation* vehicleInfo =  new Transportation;
    	vehicleInfo->dCarEngineSize = 3.1;
    	vehicleInfo->iMotorcycleCCs = 750;
    	vehicleInfo->iSemiNumberofAxels = 6;
    
    	cout <<	vehicleInfo->dCarEngineSize << endl <<
    			vehicleInfo->iMotorcycleCCs << endl <<
    			vehicleInfo->iSemiNumberofAxels << endl;
    	delete vehicleInfo;
    	return 0;
    }
    BTW, <iostream.h> is old and non-standard C++ and won't compile on many modern compilers. Consider changing it to <iostream>.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Daved
    I think Shamino's code is better than Ancient Dragon's. What's the point of using void*?
    The point? none -- I agree with you about using the pointer operation. I was NOT recommending the code I posted, just showing how it could be done. Otherwise, it pretty much sucks cannel water.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    yah, the book was telling me that there was a way I could make it so I could use the . operator instead of the -> thingy... I guess theres really no difference, except it might cause confusion...

    So what I'm doing in my code, is making a new * when I should be making a &, and I shuold be deleting a * when I'm deleting a &



    , I think thats what you're saying ancientdragon

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    or to make it exception safe, and easier to use...

    Code:
    #include <memory>
    int main(int argc, char* argv[])
    {
    	std::auto_ptr<Transportation> vehicleInfo(new Transportation);
    	vehicleInfo->dCarEngineSize = 3.1;
    	vehicleInfo->iMotorcycleCCs = 750;
    	vehicleInfo->iSemiNumberofAxels = 6;
    
    	cout <<	vehicleInfo->dCarEngineSize << endl <<
    			vehicleInfo->iMotorcycleCCs << endl <<
    			vehicleInfo->iSemiNumberofAxels << endl;
    	return 0;
    }
    auto_ptr will delete the object for you when it goes out of scope.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> So what I'm doing in my code, is making a new * when I should be making a &, and I shuold be deleting a * when I'm deleting a &

    Your code is fine, just potentially confusing like you said. If for whatever reason you want to use the . operator instead of the -> operator, your code is the correct way to do it.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    right right, so all except for the -> thingy part, its correct? and I COULD use the auto_ptr thingy to automatically delete it...

    does this work in every case?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure what you mean by "all except for the -> thingy part". Your code is 100% correct (except for the <iostream.h> part). It uses new to allocate space for a Transportation instance and it deletes that instance later by using operator& to get the address.

    You can use the auto_ptr thing so that you don't have to remember to delete the memory yourself. There are rules about when you can use auto_ptr, so it doesn't work in every case.

    BTW, I assumed you were doing this for learning purposes, but the normal way to do this is of course without using new or pointers at all.
    Code:
    int main(int argc, char* argv[])
    {
    	Transportation vehicleInfo;
    	vehicleInfo.dCarEngineSize = 3.1;
    	vehicleInfo.iMotorcycleCCs = 750;
    	vehicleInfo.iSemiNumberofAxels = 6;
    
    	cout <<	vehicleInfo.dCarEngineSize << endl <<
    			vehicleInfo.iMotorcycleCCs << endl <<
    			vehicleInfo.iSemiNumberofAxels << endl;
    	return 0;
    }

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Yes I'm just learning how to send things directly to the heap instead of the stack, most programs have to do this, so i'm assuming its something I need to know...

    Can you list a few reasons why I need to do this insead of just using the stack all the time?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the size of what you are allocating is very large, then you want to do it on the heap instead of the stack, since the size of the stack is much smaller than the heap.

    If the object you are creating needs to remain "alive" for longer than the current block of code, then you need to use the heap, since local variables on the stack are cleaned up automatically when they go out of scope. A common case of this is a function (Factory) that returns a base class pointer to a new derived instance so that it can be used polymorphically.

    Smart pointers (like auto_ptr) can almost always be used to hold the dyamically allocated pointer and delete it appropriately. However, they are a bit more advanced tool that you probably don't want to worry about just yet. If you don't use smart pointers, IMO the next best way to keep track of heap allocated objects is with a simple pointer like my first example showed.

  12. #12
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I see, do you think an array of

    data[3][14][28] is big enough to consider to put into the heap?

    Not to mention we need 4 or more of them,
    so
    3*14*28*4 = 4706 objects.......

    wow thats alot... i just realized
    Last edited by Shamino; 09-23-2005 at 09:54 PM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    4706 ints, which are probably 4 bytes on your machine, is a little less than total 19000 bytes. That is not that much, and it can stay on the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 03:36 AM
  2. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  5. c style string memory leak question
    By curlious in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2003, 06:31 AM