Hi, I would like to have consideration of you people on this list. I have some doubts on memory allocation in C. i.e. how the variables are allocated memory space. Difference between allocation from heap and allocation from stack. And when do we have memory allocated from heap and when from stack. Thanks in advance. Regards, A+

stack used is used for local variable and the size of the stack is know but
heap is used for dynamic memory allocation

On Fri, Apr 9, 2010 at 3:51 PM, ajay.kakumanu via cpp-l <
cpp-l@groups.ittoolbox.com> wrote:

Posted by ajay.kakumanu
on Apr 9 at 6:20 AM
Dear Anish,
Here I am giving Memory Layout of C programme.
Generally C-programme has following memory segements.

  1. Code Segment : All instructions are stored here.
  2. Text Segment(read only segment) : all strings, constant are stored here
    3 Data Segment : Static and gobal variables are stored here.
    4 Stack Segment : Local variables, function parameters, return values …etc

5 Heap Segment : All dynamic memory allocations done here.

Data Segment has 2 subsections, Why?
Pleas find the following description which I have given long back.

static int x, &
static int x =5,
In both statements X is stored in data segment only,
But the difference is internally Data segment have 2 sections
a) Intialised section
b) un-Intialised section

if we use first statement
static int x, then x is stored in un-Initialized section
if we use Second statement
static int x =5,then X is stored in Initialized section

the Reason why again divided the Data segment into 2 sections
it is easy to initialized all un-initialized static variables to
Zeroes (using memset?), If all Un-Initialized data are stored at same
place.

please find the following example
Int a; // Un Initialized Global variables, stored in Un-Initialized
Data segment.
Int q
; // Initialized Global variables, stored in Initialized Data segment.
Void Main()
{
Int x; // Auto Variable ( Stored in Stack Segment)
Int y=10; // Auto Variable ( Stored in Stack Segment)

Const int z=25; // Stored in Stack Segment

Char *ptr=0;
Ptr=malloc/calloc( 100 bytes);// dynamic memory allocation( allocated from
Heap
Segment)

Char *str=" I am a good buy "; // This String is stored in Read Only
Segment (we can also say Text Segment)

Static int p=10;// Initialized Static variables, stored in
Initialized Data Segment.
Static int x; // Un Initialized Static variables, stored in
Un-Initialized Data Segment.

Fun( 10,20);
}
Fun(int x,int y) // function parameters x and y variables are stored
in Stack segment.
{
Return 10,// Return values are stored in Stack Segment.
}
Please let me know if u need any info.

Thanks and Regards
Ajay kakumanu

On 4/8/10, sudheer23 via cpp-l

Dear Anish,
Here I am giving Memory Layout of C programme.
Generally C-programme has following memory segements.

  1. Code Segment : All instructions are stored here.
  2. Text Segment(read only segment) : all strings, constant are stored here
    3 Data Segment : Static and gobal variables are stored here.
    4 Stack Segment : Local variables, function parameters, return values …etc
    5 Heap Segment : All dynamic memory allocations done here.

Data Segment has 2 subsections, Why?
Pleas find the following description which I have given long back.

static int x, &
static int x =5,
In both statements X is stored in data segment only,
But the difference is internally Data segment have 2 sections
a) Intialised section
b) un-Intialised section

if we use first statement
static int x, then x is stored in un-Initialized section
if we use Second statement
static int x =5,then X is stored in Initialized section

the Reason why again divided the Data segment into 2 sections
it is easy to initialized all un-initialized static variables to
Zeroes (using memset?), If all Un-Initialized data are stored at same
place.

please find the following example
Int a; // Un Initialized Global variables, stored in Un-Initialized
Data segment.
Int q
; // Initialized Global variables, stored in Initialized Data segment.
Void Main()
{
Int x; // Auto Variable ( Stored in Stack Segment)
Int y=10; // Auto Variable ( Stored in Stack Segment)

Const int z=25; // Stored in Stack Segment

Char *ptr=0;
Ptr=malloc/calloc( 100 bytes);// dynamic memory allocation( allocated from Heap
Segment)

Char *str=" I am a good buy "; // This String is stored in Read Only
Segment (we can also say Text Segment)

Static int p=10;// Initialized Static variables, stored in
Initialized Data Segment.
Static int x; // Un Initialized Static variables, stored in
Un-Initialized Data Segment.

Fun( 10,20);
}
Fun(int x,int y) // function parameters x and y variables are stored
in Stack segment.
{
Return 10,// Return values are stored in Stack Segment.
}
Please let me know if u need any info.

Thanks and Regards
Ajay kakumanu

hi
according to me, when you use library functions such as new,malloc etc. then
memory is allocated from heap and this heap in c++ known as free store which
is nothing but a block in memory from where memory is allocated to desire
variables.

sudheer23 said…

int arr= (int) malloc (size of(int) *10) ; //It allocates storage
for 10 integers from heap , heap is used.


Technically speaking, that allocates 10 pointers to integers, not 10
integers, and to do it correctly, you would code

int *arr= malloc (sizeof(int *) *10) ;

generally stack is used to maintain local variables and return values from functions. where as heap is used for dynamic memory allocation.
int num ; //used stack;

int arr= (int) malloc (size of(int) *10) ; //It allocates storage for 10 integers from heap , heap is used.

generally stack is used to maintain local variables and return values from functions. where as heap is used for dynamic memory allocation.
int num ; //used stack;

int arr= (int) malloc (size of(int) *10) ; //It allocates storage for 10 integers from heap , heap is used.

hi,
when you use
delete p;
It will deallocate the memory for the entire array.
but
delete p;
will deallocate the memory of the pointer pointing to it.

There is no such thing as a preordained method of doing this.
Yet again, it boils down to the compiler.

from Marshall Cline’s c++ faqs:
Q. After p = new Fred[n], how does the compiler know there are n objects to
be destructed during delete p?

Short answer: Magic.

Long answer: The run-time system stores the number of objects, n, somewhere
where it can be retrieved if you only know the pointer, p. There are two
popular techniques that do this. Both these techniques are in use by
commercial grade compilers, both have tradeoffs, and neither is perfect.
These techniques are:

  • Over-allocate the array and put n just to the left of the first Fred
    object.
  • Use an associative array with p as the key and n as the value.

~Ashish

Hi Raju,

sorry for late reply.
Actully, while allocating memory , it maintains a structure regarding memory
allocation. I donot know all the member of the structure. The structure
contains a variable indicating that no.of bytes allocated starting at that
particular location. From that number, delete or free() deallocates
memory. If anybody know the structure members, let me know.
With Warm Regards,

E. Sudarsan
Software Engineer,

int *pVal = new int[10];

delete pVal;
delete pVal; // Same Effect

No its not same…its asking for trouble…new and new are different …new allocates memory from heap for a single object…and new allocates for an array…
delete pVal is incorrect in this example

As far as ‘ delete’ is concerned, you may be knowing that “delete =
p”, just don’t releases the memory but it also calls the destructor of =

delete and delete both call destructors…delete calls destructor for each constructed element of the array…

This is the reason when u r allocating for some basic data type, =
‘delete’ and ‘ delete’ doesn’t make much difference. You can test this =

in c++ all the basic data type are implemented as class…that is why u can initialize say an integer in 2 ways -
int ii = 10; //calls operator= overloaded function
int ii(10); //calls the constructor rightr away

Cheers
Prabhat Tyagi

Hi Rahul,

As far as I know, the additional memory is continuously aligned with =
the allocated memory, though I am not an expert here. May be some Comp. =
Sc. guys who has worked on compilers may throw more light here.

As far as ‘ delete’ is concerned, you may be knowing that “delete =
p”, just don’t releases the memory but it also calls the destructor of =
the objects so if u have allocated using , you should delete them =
using so that for every object it’s destructor is called. If you use =
just ‘delete p’, the result is really catastrophic. For more =
information, read Effective C++ and More Effective C++.

This is the reason when u r allocating for some basic data type, =
‘delete’ and ‘ delete’ doesn’t make much difference. You can test this =
urself by using the purify or some other tool.

int *pVal =3D new int[10];

delete pVal;
delete pVal; // Same Effect

But say for some class, say, myClass

myClass *pObj =3D new myClass[10];

delete pObj; // Catastrophe - inviting problems
delete pObj; // Perfect

Again, I am not a Comp. Sc. guy so better if somebody corrects me if I =
am wrong.

Thanks,
Ashish

Hi,

When you use the operation new, it just doesn’t allocate the same =
amount of memory which you had request. Rather it allocates a little =
more and that little more has the information about how much is =
allocated. This information is used by delete while releasing the =
memory.

The second point which I would like to highlight here is that if your =
size of object is small then this overhead is comparable and in such =
situations, writing your own operator new is recommended. And for the =
very same reason, it’s required to provide your own delete operator when =
you are providing your own new operator.

Hope this helps,

Regards,
Ashish

Hi,
if we use int p=new int[10];
it allocats 10
sizeof(int) bytes.

When we use delete p, it releases memory.

I want to know how delete will know 10*sizeof(int) bytes to remove?

Thanks & Regards,
Raju

In simple words -

  1. Stacks holds local variables (automatic variable) and the return address of functions.

  2. Heap is used to allocate memory dynamically.

eg; Consider the following code snippet

void func(void)
{
int ii;
char *ptr;
ptr = malloc(10);
}

When this process is executing func(), ii abd ptr are stored in the stack. The memory allocated by malloc is on the heap. There are other segments of the process as well…but that is out of scope of this question :smiley:

Cheers
Prabhat Tyagi

That’s correct Ashish

jabm

Ashish via cpp-l

doesnt work. tried it. runs and does nothing.

— Sudesh Sawant via cpp-l

You can use the InitiateSystemShutdown!!

Hi Chris,

When you do a new int[10], it allocates the storage for 10 integers =
which is 10sizeof(int) on a given OS. Hence, p points to 10sizeof(int) =
array and not the 11 bytes array.

Thanks,
Ashish