Internal C++ pointer/array management

Started by
4 comments, last by PhiberOptic 20 years, 8 months ago
Hi there! When I was coding some things, a question came up. How does C++ remember the size of an array located on the heap? Like this: int* pInts = new int[100]; Then I want to delete them with delete [] pInts; Okay, in this example, the compiler might easily "remember" how many ints to delete since "100" is a constant, but what if it''s a variable like: int sz = 100; int* pInts = new int[sz]; delete [] pInts; Does the compiler generate a size variable somewhere to remember how many ints I''ve allocated or what? ---------------------------------------------- Petter Nordlander "There are only 10 kinds of people in the world. The who understand binary and those who don''t"
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
Advertisement
It might, or it might not. The C++ standard just dictates that it should work, not precisely how it is implemented.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
I can''t see any way it should work if it does not. But I thought there might be one..

----------------------------------------------
Petter Nordlander

"There are only 10 kinds of people in the world. The who understand binary and those who don''t"
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
quote:Original post by PhiberOptic


Does the compiler generate a size variable somewhere to remember how many ints I''ve allocated or what?



It has to. However, it does this automatically only for convenience.

Same type of automation works in arrays of any type.
The compiler stores automatically the size of one element.

Niko Suni

C++ FAQ Lite [16.13] After p = new Fred[n], how does the compiler know there are n objects to be destructed during delete[] p?

[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Thanks! Perfectly! Exactly what I was looking for...
btw, I might have you on my ICQ list.. I''m not sure.. :-)

----------------------------------------------
Petter Nordlander

"There are only 10 kinds of people in the world. The who understand binary and those who don''t"
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"

This topic is closed to new replies.

Advertisement