A doubt on basics -- memory allocation

Pages: 12
1
2
A* a=new A();
A b;


I read somewhere that, objecs are always created in the heap and in the above casse the diff is that the first line requires delete and second one gets automatically deleted..

Is it true that the objects are always created in the heap??

No. The second line creates the object on the stack. I suppose there could be some bizarre implementation without a stack, allocating everything on the heap, or something, but...
I read somewhere that, objecs are always created in the heap


I think that's the case in Java.

It certainly is not the case in C++.

+1 to what Zhuge said. line1 is on the heap, line2 is on the stack.
I confirm Zhuge and Disch's statements.

Keeping that mind, what you said next, is true. Heap objects require delete and Stack objects don't. They are local and get destroyed at the end of the scope automatically.
Now please do the illustration of the stack frame in figure 2

http://www.tenouk.com/Bufferoverflowc/Bufferoverflow2a.html

Now in case of


class ClasName
{
int i;
int j;
}


if an object is created, how would the stack frame look like??
What about it? If the object is a local variable, then it is on the stack.
Since the above has no member functions, no constructors or destructors,
and no inheritance, then i is placed at a lower memory address than j.
jsmith wrote:
What about it?

Exactly. I don't see the importance of discussing how the stack may be laid out. From a user perspective it is a stack--LIFO.
Last edited on
@Jsmith..
Alrtie..
when you create an object.. How is data tied to that object? what ll happen if 2 objects are created on stack using recursion or some other means???

What ll happend when arbitrarly large number of variables/objects are tried to push into the stack?? will stack overflow error occur??
The following is just an approximation and is not technically correct.

Think of an "object" as just an instance of a struct. The non-virtual member functions are nothing more than free
functions where there is an implicit first parameter that is a pointer to the struct instance that the function should
operate on. The struct contains one implicit function pointer data member for each virtual function it contains.

Below is a simple example:

1
2
3
4
5
6
7
8
int main()
{
    int a = 20;
    int* b;
    b = new int(10);
    delete b;
  return 0;
}


Here is the assembly code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
	.file	"sim.cpp"
	.text
.globl main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	.cfi_personality 0x3,__gxx_personality_v0
	pushq	%rbp
	.cfi_def_cfa_offset 16
	movq	%rsp, %rbp
	.cfi_offset 6, -16
	.cfi_def_cfa_register 6
	subq	$16, %rsp
	movl	$20, -4(%rbp)	 ;$20 is moved onto the stack
	movl	$4, %edi
	call	_Znwm             ;here allocates memory
	movq	%rax, %rdx       ;move the address of that memory to %rdx
	movl	$10, (%rdx )      ;move $10 to (%rdx), ie, that memory
	movq	%rax, -16(%rbp)
	movq	-16(%rbp), %rax
	movq	%rax, %rdi
	call	_ZdlPv
	movl	$0, %eax
	leave
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
	.section	.note.GNU-stack,"",@progbits


a is on stack, while *b is on heap.
Last edited on
Thank you for the explanation.. I have a question here..

How is the value tied to the variable name...


int a =20; as shown in the assembly moves it into the stack... the same way if another variable say
int b =20; is moved into the stack.

Now if i modifie the value of a=30, how does the compiler know its the value of a that is being modified because there are two integer values being pushed into the stack..

In other words, how is the label a tied up to the value 30..
Now if i modifie the value of a=30, how does the compiler know its the value of a that is being modified because there are two integer values being pushed into the stack.


It knows because you told it: "a = 30".

The stack just grows like a stack, but it can be accessed like an array. That is, any element anywhere on the stack can be accessed... not just the most recently pushed element.
karthick88 wrote:
In other words, how is the label a tied up to the value 30


As the compiler is generating code it keeps the symbols 'a' and 'b' in a table which records what their relative offset (index) is on the stack. That's generally called the 'symbol table'.

http://en.wikipedia.org/wiki/Symbol_table
Ok.. thanks.. Now where is the stack and where is the symbol table.. Are they both in the main memory?
karthick88 wrote:
Ok.. thanks.. Now where is the stack and where is the symbol table.. Are they both in the main memory?


Remember that the compiler's symbol table only exists when the compiler is running. It is just what it uses to keep track of what all the variable names are, where they have been placed and their types/scope etc... And its down to the compiler where the symbol table is. It will probably be some kind of data structure like a tree map or a vector in main memory.

The stack is indeed in main memory.
Last edited on
Here is good example:(copied from somewhere. only for linux)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
#include<malloc.h>
#include<unistd.h>

int bss_var;
int data_var0=1;

int main(int argc,char **argv)
{
	printf("below are addresses of types of process's mem\n");
	printf("Text location:\n");
	printf("\tAddress of main(Code Segment):%p\n",main);
	printf("____________________________\n");

	int stack_var0=2;
	printf("Stack Location:\n");
	printf("\tInitial end of stack:%p\n",&stack_var0);
	int stack_var1=3;
	printf("\tnew end of stack:%p\n",&stack_var1);
	printf("____________________________\n");

	printf("Data Location:\n");
	printf("\tAddress of data_var(Data Segment):%p\n",&data_var0);
	static int data_var1=4;
	printf("\tNew end of data_var(Data Segment):%p\n",&data_var1);
	printf("____________________________\n");

	printf("BSS Location:\n");
	printf("\tAddress of bss_var:%p\n",&bss_var);
	printf("____________________________\n");

	char *b = sbrk((ptrdiff_t)0);
	printf("Heap Location:\n");
	printf("\tInitial end of heap:%p\n",b);
	brk(b+4);
	b=sbrk((ptrdiff_t)0);
	printf("\tNew end of heap:%p\n",b);

	return 0;
}


below are addresses of types of process's mem
Text location:
	Address of main(Code Segment):0x4005f4
____________________________
Stack Location:
	Initial end of stack:0x7fff728f9afc  //the stack grows downward
	new end of stack:0x7fff728f9af8
____________________________
Data Location:
	Address of data_var(Data Segment):0x601038
	New end of data_var(Data Segment):0x60103c
____________________________
BSS Location:
	Address of bss_var:0x601050
____________________________
Heap Location:
	Initial end of heap:0x1513000
	New end of heap:0x1513004


You can draw the map using the data above if you like. :-)
Last edited on
Cool... Thanks A lot for all your efforts to explain...
The stack just grows like a stack, but it can be accessed like an array. That is, any element anywhere on the stack can be accessed... not just the most recently pushed element.


I have always been under the impression that the stack is a LIFO container with frames being the elements. Each frame would represent the collection of variables in the current scope. I don't see the need to access anything other than the variables in the top frame. At least this is how I have thought about it--it is probably not technically correct.
Moorecms question makes me think of another situation...

Say there are two threads...

t1 in which function1 is being executed
t2 in which function2 is being executed


Now say if t1 is now being currently executed. So function1 would be on top of the stack. Now if t1 is stoped and t2 is started, what will happen to function1's frame? will it be saved somwhere ?? In short how does that scheme work?

Threads have individual stacks.
Pages: 12