Results 1 to 7 of 7

Thread: what is free store in C++ memory?

  1. #1
    Join Date
    Apr 2007
    Posts
    33
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default what is free store in C++ memory?

    Hi
    I have a question about the free store in C++. first of all, what is that?
    second, what is the difference of creating and declaring a pointer in these following two methods


    Qt Code:
    1. Cat * ptrToCat: new Cat;
    2. delete ptrToCat;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Cat * ptrToCat
    To copy to clipboard, switch view to plain text mode 


    thnx for your time in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: what is free store in C++ memory?

    Quote Originally Posted by Masih View Post
    have a question about the free store in C++. first of all, what is that?
    It's also called a heap. This is the memory that your application can allocate and deallocate dynamically (using new/delete operators or their equivalents).

    Quote Originally Posted by Masih View Post
    second, what is the difference of creating and declaring a pointer in these following two methods

    Qt Code:
    1. Cat * ptrToCat: new Cat;
    2. delete ptrToCat;
    To copy to clipboard, switch view to plain text mode 
    There should be "=" instead of ":", anyway in this snippet you:
    1. create a pointer,
    2. create a new Cat object,
    3. initialize the pointer to make it point to that object
    4. and finally you destroy that Cat object leaving a dangling pointer (i.e. a non-null pointer that doesn't point to any object).


    Quote Originally Posted by Masih View Post
    Cat * ptrToCat
    Here you simply create a pointer without initializing it (and without disturbing any cats ). This is also a dangling pointer, because it points to some random address (although some compilers might set it to 0, but you shouldn't rely on that).

  3. The following user says thank you to jacek for this useful post:

    Masih (1st June 2007)

  4. #3
    Join Date
    Apr 2007
    Posts
    33
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: what is free store in C++ memory?

    Quote Originally Posted by jacek View Post
    It's also called a heap. This is the memory that your application can allocate and deallocate dynamically (using new/delete operators or their equivalents).


    There should be "=" instead of ":", anyway in this snippet you:
    1. create a pointer,
    2. create a new Cat object,
    3. initialize the pointer to make it point to that object
    4. and finally you destroy that Cat object leaving a dangling pointer (i.e. a non-null pointer that doesn't point to any object).



    Here you simply create a pointer without initializing it (and without disturbing any cats ). This is also a dangling pointer, because it points to some random address (although some compilers might set it to 0, but you shouldn't rely on that).

    As another question! what is a Null-pointer and what is a non-null pointer? how can a pointer exist but doesn't point to anything?
    Does these dangling pointers use memory? Is it possible to destroy them? Can these dangling pointers make some problem in the program?

    thanks( lot's of questions and lot's of ignorance )

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: what is free store in C++ memory?

    As another question! what is a Null-pointer and what is a non-null pointer?
    First of all, a pointer is just a memory location that contains a memory address. That memory address is the starting address of the object at which the pointer points to .

    So, a null pointer is a memory location that contains 0x00000000.
    A non null pointer( valid ) is a mem location that contains a valid address in the memory space of the process that allocated it.

    Dangling pointers:
    Qt Code:
    1. int *pointer = new int;
    2. *pointer = 5;
    3.  
    4. int *danglingPointer = pointer;
    5. delete pointer;
    To copy to clipboard, switch view to plain text mode 
    After you delete the pointer, danglingPointer will point to an unallocated memory( previously freed by deleting pointer ). If you try to access in any way danglingPointer( dereference it, etc ), you will get a memory violation ( segmentation fault, etc ).

    Hope you understand.

    Regards

  6. The following user says thank you to marcel for this useful post:

    Masih (4th June 2007)

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: what is free store in C++ memory?

    Quote Originally Posted by Masih View Post
    how can a pointer exist but doesn't point to anything?
    Suppose somebody gave you a book and asked you to read page 1234. You open the book and see that there is no such page in that particular book, yet the page number exists.

  8. The following user says thank you to jacek for this useful post:

    Masih (4th June 2007)

  9. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: what is free store in C++ memory?

    Quote Originally Posted by jacek View Post
    Suppose somebody gave you a book and asked you to read page 1234. You open the book and see that there is no such page in that particular book, yet the page number exists.
    Yes, this is the best analogy so far.

    A pointer is just like a normal variable, but it's contents, instead of a value that you can normally use in your program, are a memory address of, let's say another variable.

    If the memory at that address has been deallocated, then your pointer still exists, but it does not point to anything( anything valid ).

    So it is no magic going on here. It's a about understanding what a pointer really is.
    Regards

  10. The following user says thank you to marcel for this useful post:

    Masih (4th June 2007)

  11. #7
    Join Date
    Apr 2007
    Posts
    33
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Arrow Re: what is free store in C++ memory?

    I've recently seen another explanation from this book : " An introduction to design patterns in C++ with Qt4" by Ezust & Ezust .
    Wanted to post it here

    Zero (0), often represented by the macro NULL in C programs, is a special value that can be legally assigned to a pointer, usually when it is being initialized (or re-initialized). 0 is not the address of any object. A pointer that stores the value 0 is called a null pointer. Stroustrup recommends the use of 0 rather than the macro NULL in C++ programs.

Similar Threads

  1. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 20:42
  2. Replies: 2
    Last Post: 13th February 2006, 16:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.