John wrote:
> --- In [email protected], shamir shakir <dewswo...@...> wrote:
>> So, is there any function to run-time checking here that I can use? 
> 
> There isn't anything in the C Standard - I suspect C++ is the same, although 
> I'm not a C++ programmer.
> 
> Try checking your compiler/IDE documentation - there might be an option that 
> adds some sort of checking to the generated code.
> 
>> And why did the OS allocated more space ?
> 
> The OS might allocate memory in blocks, and the smallest block might be (for 
> example) 1k. It might not be the most space efficient algorithm, but it might 
> be quicker and simpler than one that allocates exactly the amount you request.
> 
> If you are interested, try Googling for 'memory allocation' eg.
> http://en.wikipedia.org/wiki/Dynamic_memory_allocation

It is probably safer to say that the Standard Library implementation of 
malloc() will generally manage the pages of memory returned by the OS. 
You get a pointer to the start of the allocated RAM.  OS calls are 
generally expensive.  So compiler libraries will look for ways to avoid 
calling the OS.  The OS will generally dish up page-sized chunks of RAM 
(on 32-bit hardware running Windows, a page is 4K - most OSes have a way 
of programmatically determining page size).

http://msdn.microsoft.com/en-us/library/ms724381%28VS.85%29.aspx

The Standard library will (usually) take the pages and add them to an 
internal table and assumes you will not "overstep array bounds" (a 
nebulous phrase).  Some compilers, such as VC++, in debug mode, will 
make larger requests for RAM (if necessary) add magic bytes of data 
around your allocation.  If you write data over these magic bytes, the 
debugger will detect the access violation and stop almost right away 
(usually).  I haven't needed this feature for a really long time.  gdb 
(*NIX) should have something similar.

Additional useful links:
http://support.microsoft.com/kb/286470
http://www.gnu.org/s/libc/manual/html_node/Heap-Consistency-Checking.html

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to