Basic memory question

This is a discussion on Basic memory question within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> class foo{ void *a; public: foo(){ a = malloc(10); std::cout << "baz" << std::endl; } foo(int size){ ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    Basic memory question

    Code:
    #include <iostream>
    
    class foo{
            void *a;
    
            public:
                    foo(){
                            a = malloc(10);
    
                            std::cout << "baz" << std::endl;
                    }
                    foo(int size){
                            a = malloc(size);
                            std::cout << "bar" << std::endl;
                    }
                    ~foo(){
                            free(a);
                    }
    };
    
    
    void bar()
    {
            foo x(5);
    }
    
    void baz()
    {
            foo *y = new foo;
            delete(y);
    }
    
    
    int main()
    {
            bar();
            baz();
    
    
            return 0;
    }

    I presume that the destructor in this case will destroy x when we are leaving the scope of bar, eventhough it's allocated on the heap, but what happens with y in baz where I use new? The constructor presumably takes care of the allocation, is the whole object a bit larger that the malloced memory in this case?

    Is there something else besides malloc I can use here?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    The foo object x is located on the stack, not the heap. The memory X allocates internally for pointer "a" is on the heap. When exiting bar, the destructor will be called freeing the memory on the heap pointed to by "a". X itself will then be popped off the stack.

    With the function baz, the pointer itself "y" is on the stack, but the memory for the foo object that "y" points to is on the heap and again, the memory that this foo object on the heap allocates for the pointer "a" also comes from the heap. When delete is called on the pointer "y", the destructor for that object is called which once again will free the memory allocated and assigned to the pointer "a". The foo object pointed to by "y" will then be released and finally at the conclusion of the function baz, the pointer "y" will be popped off the stack.

    [edit]A danger here is if you were to try and malloc that foo object in baz. Neither the constructor nor the destructor will be called if you use malloc/free. You should definitely stick with new/delete.[/edit]
    Last edited by hk_mp5kpdw; 09-13-2010 at 02:18 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thanks hk_mp5kpdw, I figured something like that but the reason I had some thoughts on where the object would be placed in this case, is that the class contains nothing but the memory area of a in this case.

    What options besides malloc is there for an arbitrary sized memory area then? Can i use new with [] uninitialized?

  4. #4
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Why do you want a blob of uninitialized heap memory?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    In this case to gain understanding, I'm just fooling around really. But for this purpose inside a class is there something that is preferable to malloc in C++, I found some info about Allocator() in STL earlier but just skimmed through it briefly.

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I believe a one-dimensional array can be initialized with a variable, such as
    Code:
    int y = 16;
    char c[] = new char[y]
    Where the size y will be evaluated at runtime, and could be received from input.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by Subsonics View Post
    In this case to gain understanding, I'm just fooling around really. But for this purpose inside a class is there something that is preferable to malloc in C++, I found some info about Allocator() in STL earlier but just skimmed through it briefly.
    The better question would be, what isn't preferable to malloc, in C++!
    In C++ you use new and new[], and these make malloc at best redundant or even harmful in many cases.

    In a proper C++ program you neither use malloc, nor void pointers. Actually you seldom need new[] either, as std::vector is there to handle that for you.

    You should also never write a class that disobeys the rule of three.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by neandrake View Post
    I believe a one-dimensional array can be initialized with a variable, such as
    Code:
    int y = 16;
    char c[] = new char[y]
    Where the size y will be evaluated at runtime, and could be received from input.
    That certainly looks live Java.
    In C++, new returns a pointer. It cannot be converted to a char array.
    So it should ne

    char* c = new char[y];

    And

    delete [] c;

    But again, it's better to use a vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by iMalc View Post
    The better question would be, what isn't preferable to malloc, in C++!
    It's not a better question if you want to find out what is preferable.

    Quote Originally Posted by iMalc View Post
    In C++ you use new and new[], and these make malloc at best redundant or even harmful in many cases.

    In a proper C++ program you neither use malloc, nor void pointers. Actually you seldom need new[] either, as std::vector is there to handle that for you.
    Point taken, "thou shalt never use malloc and free".

    Any takers on allocators in STL?

    Thanks everyone.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    What about them? Do you understand what they are and what they do?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Not really, I have looked over it very briefly. As I understand they offer something similar to what we are talking about here.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    They don't. They control how containers allocate and destroy elements. They also need to allocate memory via new/malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So the most basic means available for this is new [], then. New and delete certainly seems to give me a safer and easier to manage interface than malloc and free, even though it all boils down to malloc and free in the background, so I'm not complaining. Just like to get some feedback on my initial ideas in this area, it help to gain understanding of this for me. Thanks.

  14. #14
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by Elysia View Post
    That certainly looks live Java.
    cheers
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

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, 02:36 AM
  2. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 10:41 PM
  3. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 12:00 PM
  4. memory question
    By madsmile in forum C++ Programming
    Replies: 8
    Last Post: 06-25-2002, 11:53 AM
  5. Damn memory Question again
    By Dohojar in forum C Programming
    Replies: 5
    Last Post: 03-21-2002, 06:44 PM

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