memory allocation

Discussion in 'C Programming' started by Evangelista Sami, Oct 13, 2003.

  1. 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
    Evangelista Sami, Oct 13, 2003
    #1
    1. Advertising

  2. Evangelista Sami

    Noah Roberts Guest

    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."
    Noah Roberts, Oct 13, 2003
    #2
    1. Advertising

  3. Evangelista Sami

    T.M. Sommers Guest

    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.
    T.M. Sommers, Oct 13, 2003
    #3
  4. Evangelista Sami

    Antonio Guest

    "Evangelista Sami" <> wrote in message
    news:...
    > 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
    Antonio, Oct 13, 2003
    #4
  5. Evangelista Sami

    Richard Bos Guest

    Noah Roberts <> 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
    Richard Bos, Oct 13, 2003
    #5
  6. Evangelista Sami

    Micah Cowan Guest

    (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
    Micah Cowan, Oct 13, 2003
    #6
  7. (Richard Bos) wrote in message news:<>...
    > Noah Roberts <> 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);
    Evangelista Sami, Oct 14, 2003
    #7
  8. Evangelista Sami

    T.M. Sommers Guest

    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.
    T.M. Sommers, Oct 14, 2003
    #8
  9. Micah Cowan <> wrote:

    > (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
    ()
    Irrwahn Grausewitz, Oct 14, 2003
    #9
  10. Evangelista Sami

    Chris Torek Guest

    >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 <N5Oib.1014$>
    T.M. Sommers <> 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.
    Chris Torek, Oct 14, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    698
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    603
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,862
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    988
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,184
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page