memory allocation

E

Evangelista Sami

hello everybody

what is the size of the memory allocated for this declaration

int array[10];

it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer)
or sizeof(int) * 10 ? (just ten for the array and no pointer)

thanks
 
N

Noah Roberts

Evangelista said:
hello everybody

what is the size of the memory allocated for this declaration

int array[10];

it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer)
or sizeof(int) * 10 ? (just ten for the array and no pointer)

There is no pointer. "array" is a symbol representing 10 concurrent
integers. It's address is the beginning of this region. If, on the
other hand, array was a pointer then its *value* would be the adress of
the first element, but it's address would be something completely different.

Hope that helps.
 
T

T.M. Sommers

Evangelista said:
what is the size of the memory allocated for this declaration

int array[10];

it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer)
or sizeof(int) * 10 ? (just ten for the array and no pointer)

The latter; there is no pointer.
 
A

Antonio

Evangelista Sami said:
it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer)
or sizeof(int) * 10 ? (just ten for the array and no pointer)
sizeof(int) * 10
 
R

Richard Bos

Noah Roberts said:
Evangelista said:
what is the size of the memory allocated for this declaration

int array[10];

it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer)
or sizeof(int) * 10 ? (just ten for the array and no pointer)

There is no pointer. "array" is a symbol representing 10 concurrent
integers.

Moreover, even when you do have a pointer somewhere else, you don't know
that the size of that pointer is the same as the size of an int.

Richard
 
M

Micah Cowan

hello everybody

what is the size of the memory allocated for this declaration

int array[10];

it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer)
or sizeof(int) * 10 ? (just ten for the array and no pointer)

"Do not try and bend the array. That's impossible.
Instead, only try to realize the truth."
"What truth?"
"There is no pointer."

-Micah
 
E

Evangelista Sami

Noah Roberts said:
Evangelista said:
what is the size of the memory allocated for this declaration

int array[10];

it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer)
or sizeof(int) * 10 ? (just ten for the array and no pointer)

There is no pointer. "array" is a symbol representing 10 concurrent
integers.

Moreover, even when you do have a pointer somewhere else, you don't know
that the size of that pointer is the same as the size of an int.

Richard



thanks for your responses.

i should have formulated my question this way :

in term of size in memory does these two solutions have the same effects?


int array[10];

or

int *array;
array = (int *) malloc(sizeof(int) * 10);
 
T

T.M. Sommers

Evangelista said:
i should have formulated my question this way :

in term of size in memory does these two solutions have the same effects?


int array[10];

or

int *array;
array = (int *) malloc(sizeof(int) * 10);

In this case, the second version will consume an extra sizeof(int*) bytes.
 
I

Irrwahn Grausewitz

Micah Cowan said:
[email protected] (Evangelista Sami) said:
what is the size of the memory allocated for this declaration

int array[10];

it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer)
or sizeof(int) * 10 ? (just ten for the array and no pointer)

"Do not try and bend the array. That's impossible.
Instead, only try to realize the truth."
"What truth?"
"There is no pointer."

*LOL* :D
 
C

Chris Torek

Evangelista said:
i should have formulated my question this way :
in term of size in memory does these two solutions have the same effects? [slightly edited -- bad-practice cast removed, for instance]
int array[10]; /* vs */
int *array = malloc(sizeof(int) * 10);

In this case, the second version will consume an extra sizeof(int*) bytes.

It would probably be better to say that the second version
uses *at least* an extra sizeof(int *) bytes, provided that
the malloc() call succeeds.

In particular, a "real life" malloc() will need some sort of
auxiliary storage to record outstanding allocations and/or existing
free areas in some sort of "memory arena". This space may be
adjacent to, or distant from, the memory blocks handed back by
calls to malloc(), but it will occupy some sort of "overhead" space.
The amount of overhead varies from one implementation to another,
and for small allocations and certain types of allocators found
more often in Lisp implementations than in C ones, the actual
overhead for small areas such as "10 ints' worth of memory" might
only be a few bits, in effect, because they might track allocations
by addresses, mapping certain addresses to particular "bucket
sizes". More typically, there will probably be one or more
"void *" pointers and perhaps some additional overhead per handed-out
block, so that asking for 20 or 40 bytes requires as much as 32 or
64 bytes. If sizeof(int) is 4 and sizeof(int *) is 8, this might
then use 64+8 = 72 bytes total, vs only 40 bytes for the array.

Of course, as long as the result is correct, a compiler *is* allowed
to use something like Divine Guidance to avoid any extra overhead.
It just is not very likely. :)
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top