PDA

View Full Version : what is free store in C++ memory?



Masih
1st June 2007, 14:18
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



Cat * ptrToCat: new Cat;
delete ptrToCat;


Cat * ptrToCat


thnx for your time in advance

jacek
1st June 2007, 16:06
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).


second, what is the difference of creating and declaring a pointer in these following two methods


Cat * ptrToCat: new Cat;
delete ptrToCat;
There should be "=" instead of ":", anyway in this snippet you:

create a pointer,
create a new Cat object,
initialize the pointer to make it point to that object
and finally you destroy that Cat object leaving a dangling pointer (i.e. a non-null pointer that doesn't point to any object).


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).

Masih
1st June 2007, 16:41
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:

create a pointer,
create a new Cat object,
initialize the pointer to make it point to that object
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:o )

marcel
1st June 2007, 17:30
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:


int *pointer = new int;
*pointer = 5;

int *danglingPointer = pointer;
delete pointer;

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

jacek
1st June 2007, 17:40
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.

marcel
1st June 2007, 17:49
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

Masih
2nd July 2007, 22:25
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 :p


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.