free and delete[]
-
hi all, people may feel that the question what i ask is very basic but guess what i didnt found the right answer for this thats the reason i am posting here so please help me... 1. when we allocate 50bytes of memory via malloc or new and while using free or delete[] on this variable how does it knows to delete all those 50 blocks..... 2.
int i = 10; int &a = i; a = 30;
how does compiler knows that it need to change the value of i to 30. please help me....... -
hi all, people may feel that the question what i ask is very basic but guess what i didnt found the right answer for this thats the reason i am posting here so please help me... 1. when we allocate 50bytes of memory via malloc or new and while using free or delete[] on this variable how does it knows to delete all those 50 blocks..... 2.
int i = 10; int &a = i; a = 30;
how does compiler knows that it need to change the value of i to 30. please help me.......1. The OS keeps track of all allocated blocks on the heap. So it knows about the total size, number of objects etc. This information is used to free the heap. 2. a is a reference to the variable i. Even though references were introduced in C++, internally it is still implemented as a pointer. So
int &a = i;
means pointer a that holds the address of variable i.a = 30;
means put the value 30 into the address where a is pointing to. Since i is the variable assigned to that address, its value becomes 30.«_Superman_» I love work. It gives me something to do between weekends.
-
hi all, people may feel that the question what i ask is very basic but guess what i didnt found the right answer for this thats the reason i am posting here so please help me... 1. when we allocate 50bytes of memory via malloc or new and while using free or delete[] on this variable how does it knows to delete all those 50 blocks..... 2.
int i = 10; int &a = i; a = 30;
how does compiler knows that it need to change the value of i to 30. please help me.......hawk23reddy wrote:
how does compiler knows that it need to change the value of i to 30.
Quoting C++ FAQ Lite[^] Underneath it all, a reference i to object x is typically the machine address of the object x. But when the programmer says i++, the compiler generates code that increments x. In particular, the address bits that the compiler uses to find x are not changed. Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object. :)
Navaneeth How to use google | Ask smart questions
-
1. The OS keeps track of all allocated blocks on the heap. So it knows about the total size, number of objects etc. This information is used to free the heap. 2. a is a reference to the variable i. Even though references were introduced in C++, internally it is still implemented as a pointer. So
int &a = i;
means pointer a that holds the address of variable i.a = 30;
means put the value 30 into the address where a is pointing to. Since i is the variable assigned to that address, its value becomes 30.«_Superman_» I love work. It gives me something to do between weekends.
«_Superman_» wrote:
So int &a = i; means pointer a that holds the address of variable i
I doubt. I think reference is not a pointer, it is the object. See here[^].
Navaneeth How to use google | Ask smart questions
-
«_Superman_» wrote:
So int &a = i; means pointer a that holds the address of variable i
I doubt. I think reference is not a pointer, it is the object. See here[^].
Navaneeth How to use google | Ask smart questions
Reference is not a pointer as far as the compiler is concerned. After the compiler processes it, it is very similar to how a pointer is.
«_Superman_» I love work. It gives me something to do between weekends.
-
hawk23reddy wrote:
how does compiler knows that it need to change the value of i to 30.
Quoting C++ FAQ Lite[^] Underneath it all, a reference i to object x is typically the machine address of the object x. But when the programmer says i++, the compiler generates code that increments x. In particular, the address bits that the compiler uses to find x are not changed. Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object. :)
Navaneeth How to use google | Ask smart questions
1. The OS keeps track of all allocated blocks on the heap. So it knows about the total size, number of objects etc. This information is used to free the heap. I doubt about it, according to my knowledge whenever application allocates memory using malloc or new, compiler will keep track of total amount of memory allocated in 4 bytes (32 bits on 32-bit OS) just ahead of starting address of allocated block. For example, int *a = new int[4]; Suppose starting address of a is 100, then 4 bytes ahead of address location 100 say 96-100 will contain value 4*sizeof(int). So when free or delete is invoked, it just look into memory space and release accordingly. Hope this explanation will answer your question.
-
«_Superman_» wrote:
So int &a = i; means pointer a that holds the address of variable i
I doubt. I think reference is not a pointer, it is the object. See here[^].
Navaneeth How to use google | Ask smart questions
N a v a n e e t h wrote:
I think reference is not a pointer, it is the object
C++ references are implemented as a pointer - trust me :-) The only real differences between references and pointers are the syntax (. rather than ->) and the fact that it's very difficult to get a null reference - the language does it's best to ensure that.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
N a v a n e e t h wrote:
I think reference is not a pointer, it is the object
C++ references are implemented as a pointer - trust me :-) The only real differences between references and pointers are the syntax (. rather than ->) and the fact that it's very difficult to get a null reference - the language does it's best to ensure that.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Stuart Dootson wrote:
C++ references are implemented as a pointer - trust me
Are you saying it is implemented as pointers behind the scenes? I have checked GCC's documentation and other pages. But couldn't find how they implemented it. AFAIK, C++ standard doesn't tell anything about how a compiler should implement references. A pointer has a memory address and it's own space on the stack whereas a reference share the same address of the original variable. Also the statements in FAQ[^] and whatever you said is not matching (see the Important note specially). Or am I missing something here? :)
Navaneeth How to use google | Ask smart questions
-
Stuart Dootson wrote:
C++ references are implemented as a pointer - trust me
Are you saying it is implemented as pointers behind the scenes? I have checked GCC's documentation and other pages. But couldn't find how they implemented it. AFAIK, C++ standard doesn't tell anything about how a compiler should implement references. A pointer has a memory address and it's own space on the stack whereas a reference share the same address of the original variable. Also the statements in FAQ[^] and whatever you said is not matching (see the Important note specially). Or am I missing something here? :)
Navaneeth How to use google | Ask smart questions
N a v a n e e t h wrote:
Are you saying it is implemented as pointers behind the scenes
Yep - consider this C++ code - two equivalent functions, one using a pointer, the other a reference.
void Add1(int* pInt, int x)
{
*pInt += x;
}void Add2(int& rInt, int x)
{
rInt += x;
}I compiled this with
cl -FAcs -c -O2
and got this assembly language (irrelevant bits snipped):PUBLIC ?Add1@@YAXPAHH@Z ; Add1
_TEXT SEGMENT
_pInt$ = 8 ; size = 4
_x$ = 12 ; size = 4
?Add1@@YAXPAHH@Z PROC ; Add1, COMDAT; 3 : *pInt += x;
00000 8b 44 24 04 mov eax, DWORD PTR _pInt$[esp-4]
00004 8b 4c 24 08 mov ecx, DWORD PTR _x$[esp-4]
00008 01 08 add DWORD PTR [eax], ecx; 4 : }
0000a c3 ret 0
?Add1@@YAXPAHH@Z ENDP ; Add1
_TEXT ENDS
PUBLIC ?Add2@@YAXAAHH@Z ; Add2
_TEXT SEGMENT
_rInt$ = 8 ; size = 4
_x$ = 12 ; size = 4
?Add2@@YAXAAHH@Z PROC ; Add2, COMDAT; 8 : rInt += x;
00000 8b 44 24 04 mov eax, DWORD PTR _rInt$[esp-4]
00004 8b 4c 24 08 mov ecx, DWORD PTR _x$[esp-4]
00008 01 08 add DWORD PTR [eax], ecx; 9 : }
0000a c3 ret 0
?Add2@@YAXAAHH@Z ENDP ; Add2
_TEXT ENDS
ENDExactly the same code for each. The Wikipedia page on C++ references[^] says it quite nicely - ...the typical implementation...effectively compiles references into pointers which are implicitly dereferenced at each use. Basically, references are (from most C++ programmers PoV) pretty much just syntactic sugar[^] for pointers. PS - here's the gcc assembly language for that C++ code - again, the two functions are implemented exactly the same.
.globl __Z4Add1Pii
.def __Z4Add1Pii; .scl 2; .type 32; .endef
__Z4Add1Pii:
LFB2:
pushl %ebp
LCFI0:
movl %esp, %ebp
LCFI1:
movl 8(%ebp), %edx
movl 12(%ebp), %eax
addl %eax, (%edx)
popl %ebp
ret
LFE2:
.align 2
.p2align 4,,15
.globl __Z4Add2Rii
.def __Z4Add2Rii; .scl 2; .type 32; .endef
__Z4Add2Rii:
LFB3:
pushl %ebp
LCFI2:
movl %esp, %ebp
LCFI3:
movl 8(%ebp), %edx
mov -
N a v a n e e t h wrote:
Are you saying it is implemented as pointers behind the scenes
Yep - consider this C++ code - two equivalent functions, one using a pointer, the other a reference.
void Add1(int* pInt, int x)
{
*pInt += x;
}void Add2(int& rInt, int x)
{
rInt += x;
}I compiled this with
cl -FAcs -c -O2
and got this assembly language (irrelevant bits snipped):PUBLIC ?Add1@@YAXPAHH@Z ; Add1
_TEXT SEGMENT
_pInt$ = 8 ; size = 4
_x$ = 12 ; size = 4
?Add1@@YAXPAHH@Z PROC ; Add1, COMDAT; 3 : *pInt += x;
00000 8b 44 24 04 mov eax, DWORD PTR _pInt$[esp-4]
00004 8b 4c 24 08 mov ecx, DWORD PTR _x$[esp-4]
00008 01 08 add DWORD PTR [eax], ecx; 4 : }
0000a c3 ret 0
?Add1@@YAXPAHH@Z ENDP ; Add1
_TEXT ENDS
PUBLIC ?Add2@@YAXAAHH@Z ; Add2
_TEXT SEGMENT
_rInt$ = 8 ; size = 4
_x$ = 12 ; size = 4
?Add2@@YAXAAHH@Z PROC ; Add2, COMDAT; 8 : rInt += x;
00000 8b 44 24 04 mov eax, DWORD PTR _rInt$[esp-4]
00004 8b 4c 24 08 mov ecx, DWORD PTR _x$[esp-4]
00008 01 08 add DWORD PTR [eax], ecx; 9 : }
0000a c3 ret 0
?Add2@@YAXAAHH@Z ENDP ; Add2
_TEXT ENDS
ENDExactly the same code for each. The Wikipedia page on C++ references[^] says it quite nicely - ...the typical implementation...effectively compiles references into pointers which are implicitly dereferenced at each use. Basically, references are (from most C++ programmers PoV) pretty much just syntactic sugar[^] for pointers. PS - here's the gcc assembly language for that C++ code - again, the two functions are implemented exactly the same.
.globl __Z4Add1Pii
.def __Z4Add1Pii; .scl 2; .type 32; .endef
__Z4Add1Pii:
LFB2:
pushl %ebp
LCFI0:
movl %esp, %ebp
LCFI1:
movl 8(%ebp), %edx
movl 12(%ebp), %eax
addl %eax, (%edx)
popl %ebp
ret
LFE2:
.align 2
.p2align 4,,15
.globl __Z4Add2Rii
.def __Z4Add2Rii; .scl 2; .type 32; .endef
__Z4Add2Rii:
LFB3:
pushl %ebp
LCFI2:
movl %esp, %ebp
LCFI3:
movl 8(%ebp), %edx
movWOW! That's food for thought. Thanks. Let me investigate further on this. :)
Navaneeth How to use google | Ask smart questions
-
N a v a n e e t h wrote:
Are you saying it is implemented as pointers behind the scenes
Yep - consider this C++ code - two equivalent functions, one using a pointer, the other a reference.
void Add1(int* pInt, int x)
{
*pInt += x;
}void Add2(int& rInt, int x)
{
rInt += x;
}I compiled this with
cl -FAcs -c -O2
and got this assembly language (irrelevant bits snipped):PUBLIC ?Add1@@YAXPAHH@Z ; Add1
_TEXT SEGMENT
_pInt$ = 8 ; size = 4
_x$ = 12 ; size = 4
?Add1@@YAXPAHH@Z PROC ; Add1, COMDAT; 3 : *pInt += x;
00000 8b 44 24 04 mov eax, DWORD PTR _pInt$[esp-4]
00004 8b 4c 24 08 mov ecx, DWORD PTR _x$[esp-4]
00008 01 08 add DWORD PTR [eax], ecx; 4 : }
0000a c3 ret 0
?Add1@@YAXPAHH@Z ENDP ; Add1
_TEXT ENDS
PUBLIC ?Add2@@YAXAAHH@Z ; Add2
_TEXT SEGMENT
_rInt$ = 8 ; size = 4
_x$ = 12 ; size = 4
?Add2@@YAXAAHH@Z PROC ; Add2, COMDAT; 8 : rInt += x;
00000 8b 44 24 04 mov eax, DWORD PTR _rInt$[esp-4]
00004 8b 4c 24 08 mov ecx, DWORD PTR _x$[esp-4]
00008 01 08 add DWORD PTR [eax], ecx; 9 : }
0000a c3 ret 0
?Add2@@YAXAAHH@Z ENDP ; Add2
_TEXT ENDS
ENDExactly the same code for each. The Wikipedia page on C++ references[^] says it quite nicely - ...the typical implementation...effectively compiles references into pointers which are implicitly dereferenced at each use. Basically, references are (from most C++ programmers PoV) pretty much just syntactic sugar[^] for pointers. PS - here's the gcc assembly language for that C++ code - again, the two functions are implemented exactly the same.
.globl __Z4Add1Pii
.def __Z4Add1Pii; .scl 2; .type 32; .endef
__Z4Add1Pii:
LFB2:
pushl %ebp
LCFI0:
movl %esp, %ebp
LCFI1:
movl 8(%ebp), %edx
movl 12(%ebp), %eax
addl %eax, (%edx)
popl %ebp
ret
LFE2:
.align 2
.p2align 4,,15
.globl __Z4Add2Rii
.def __Z4Add2Rii; .scl 2; .type 32; .endef
__Z4Add2Rii:
LFB3:
pushl %ebp
LCFI2:
movl %esp, %ebp
LCFI3:
movl 8(%ebp), %edx
movHi Stuart and Navaneeth, I was browing old posts and came across this discussion. The standards state that It is unspecified whether or not a reference requires storage which gives compiler venders alot of room for implementation. However I believe you are both correct depending on how the compiler performs optimization and has implemented references. I make this assertion because the ISO standards explicitly allow both scenarios. Essentially it is not correct to say that references are always pointers. It is equally incorrect to say that references always refer directly to the object. It really comes down to language semantics. Best Wishes, -David Delaune