memory usage question

S

Sean P.

why does "int *iptr = new int[1024];" use up to 8KB of memory when i
only declared a ptr to 1024 integers? Intuitively, it should be only
1024*sizeof(int)+4 (for the pointer). Does C++ add an accompanying
pointer to every element or something? thanks.
 
J

John Dibling

why does "int *iptr = new int[1024];" use up to 8KB of memory when i
only declared a ptr to 1024 integers? Intuitively, it should be only
1024*sizeof(int)+4 (for the pointer). Does C++ add an accompanying
pointer to every element or something? thanks.

How did you determine the memory usage?

</dib>

John Dibling
email: dib@substitute_my_full_last_name_here.com
Witty banter omitted for your protection
 
D

David White

Sean P. said:
why does "int *iptr = new int[1024];" use up to 8KB of memory when i
only declared a ptr to 1024 integers? Intuitively, it should be only
1024*sizeof(int)+4 (for the pointer). Does C++ add an accompanying
pointer to every element or something? thanks.

How do you know that 8 kb has been used? What is sizeof(int) on your
machine?

Pedantic note: "Usage" means _how_ something is used, not how much.

DW
 
J

John Harrison

Sean P. said:
why does "int *iptr = new int[1024];" use up to 8KB of memory when i
only declared a ptr to 1024 integers? Intuitively, it should be only
1024*sizeof(int)+4 (for the pointer). Does C++ add an accompanying
pointer to every element or something? thanks.

There is always a small overhead when allocating memory. But there is no way
that on a system where sizeof(int) == 4 that allocating 1024 ints uses 8Kb.
More likely your method of finding out how much memory is being used is
faulty.

john
 
H

Harald Grossauer

Sean said:
why does "int *iptr = new int[1024];" use up to 8KB of memory when i
only declared a ptr to 1024 integers? Intuitively, it should be only
1024*sizeof(int)+4 (for the pointer). Does C++ add an accompanying
pointer to every element or something? thanks.

Maybe your machine has a page size of 4kb, then
4*sizeof(int)+a_little_bit_overhead takes two pages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top