473,378 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

memory allocation

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
Nov 13 '05 #1
9 2516
Evangelista Sami wrote:
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.

--
Noah Roberts
- "If you are not outraged, you are not paying attention."

Nov 13 '05 #2
Evangelista Sami wrote:
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.

Nov 13 '05 #3

"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
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
Nov 13 '05 #4
Noah Roberts <nr******@dontemailme.com> wrote:
Evangelista Sami wrote:
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
Nov 13 '05 #5
ev******@cnam.fr (Evangelista Sami) writes:
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

Nov 13 '05 #6
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<3f****************@news.nl.net>...
Noah Roberts <nr******@dontemailme.com> wrote:
Evangelista Sami wrote:
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);
Nov 13 '05 #7
Evangelista Sami wrote:

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.

Nov 13 '05 #8
Micah Cowan <mi***@cowan.name> wrote:
ev******@cnam.fr (Evangelista Sami) writes:
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
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #9
>Evangelista Sami wrote:
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 article <N5*****************@nnrp1.ptd.net>
T.M. Sommers <tm**@mail.ptd.net> writes: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. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.