Memory Allocation Question

Discussion in 'C++' started by Nephi Immortal, Nov 22, 2011.

  1. I have a question about memory allocation. I discuss stack. You
    declare and define four char variables. One byte of char type
    allocates one byte into stack, but it actually allocates 16 bytes
    while leaving 15 bytes unused or wasted.

    char a = ‘1’; // allocates one byte leaving 15 bytes wasted
    char b = ‘2’; // allocates one byte leaving 15 bytes wasted
    char c = ‘3’; // allocates one byte leaving 15 bytes wasted
    char d = ‘4’; // allocates one byte leaving 15 bytes wasted

    Total four variables have four bytes. Stack allocates total 64 bytes
    and only four bytes are used. Correct?
    Why can’t stack allocate four bytes instead of 16 bytes if it is on
    32 bit machine and 8 bytes on 64 bit machine?

    I read several websites. They claim that heap allocates 32,768
    bytes, but only 1,000 bytes are used.

    char* e = new char[ 1000 ];

    Is it true that heap allocates 32K each variable? If not, how much
    total bytes do heap allocate each variable?
     
    Nephi Immortal, Nov 22, 2011
    #1
    1. Advertising

  2. Nephi Immortal

    Ian Collins Guest

    On 11/22/11 06:10 PM, Nephi Immortal wrote:
    > I have a question about memory allocation. I discuss stack. You
    > declare and define four char variables. One byte of char type
    > allocates one byte into stack, but it actually allocates 16 bytes
    > while leaving 15 bytes unused or wasted.


    I very much doubt that, have you tried?

    #include <iostream>

    int main()
    {
    char a = '1';
    char b = '3';
    char c = '3';
    char d = '4';

    std::cout << std::hex << (void*)&a << ' '
    << (void*)&b << ' '
    << (void*)&c << ' '
    << (void*)&d << std::endl;
    }

    gives

    fffffd7fffdffa07 fffffd7fffdffa06 fffffd7fffdffa05 fffffd7fffdffa04

    on my box.

    >
    > I read several websites. They claim that heap allocates 32,768
    > bytes, but only 1,000 bytes are used.


    You should improve your reading list.

    > char* e = new char[ 1000 ];
    >
    > Is it true that heap allocates 32K each variable? If not, how much
    > total bytes do heap allocate each variable?


    The answer depends on the implementation. Have you tried it?

    char* p = new char[1000];
    char* p1 = new char[1000];

    std::cout << std::hex << (void*)p << ' ' << (void*)p1
    << ' ' << p1-p << std::endl;

    gives

    4374f0 4378f0 400

    on my box.

    --
    Ian Collins
     
    Ian Collins, Nov 22, 2011
    #2
    1. Advertising

  3. Nephi Immortal

    Goran Guest

    On Nov 22, 6:10 am, Nephi Immortal <> wrote:
    >         I have a question about memory allocation.  I discuss stack.  You
    > declare and define four char variables.  One byte of char type
    > allocates one byte into stack, but it actually allocates 16 bytes
    > while leaving 15 bytes unused or wasted.
    >
    > char a = ‘1’; // allocates one byte leaving 15 bytes wasted
    > char b = ‘2’; // allocates one byte leaving 15 bytes wasted
    > char c = ‘3’; // allocates one byte leaving 15 bytes wasted
    > char d = ‘4’; // allocates one byte leaving 15 bytes wasted
    >
    >         Total four variables have four bytes.  Stack allocates total 64 bytes
    > and only four bytes are used.  Correct?


    No, indeterminate, rather. First off, this is a group that discusses C+
    + language. But C++ spec knows nothing about the stack. Granted, all
    implementations (OS/compiler/linker) use it, so...

    Second, in typical implementations, this is false. What usually
    happens is that all variables are aligned so that access is efficient.
    So it might happen that, on a 16-bit system, your characters are each
    on a 16-bit boundary (or 32, or 64-bit boundary). You need to try to
    see, on your implementation, what happens.

    >         Why can’t stack allocate four bytes instead of 16 bytesif it is on
    > 32 bit machine and 8 bytes on 64 bit machine?
    >
    >         I read several websites.  They claim that heap allocates 32,768
    > bytes, but only 1,000 bytes are used.


    Again, C++ language says absolutely nothing about how dynamic storage
    (heap) is organized. So you have to check to see what happens on your
    implementation. That said, these several websites are wrong. This is
    not happening in reality. What they might be talking about, are memory
    allocation routines provided by the operating system, who tend to be
    (or, rather, used to be in earlier days) coarse-grained, and
    therefore, whatever you ask, you get a good-sized chunk of e.g. 32K. I
    highly doubt that any operator new, or malloc() implementation, ever,
    used to allocate 32K.

    > char* e = new char[ 1000 ];
    >
    >         Is it true that heap allocates 32K each variable?  If not, how much
    > total bytes do heap allocate each variable?


    You must check to see what actually happens on your implementation.
    Typically, you should expect 8 to 16 octets of overhead, and possibly
    some padding. You should __not__ expect 32K.

    Goran.
     
    Goran, Nov 22, 2011
    #3
  4. Goran <> wrote:
    >> char* e = new char[ 1000 ];
    >>
    >>         Is it true that heap allocates 32K each variable?  If not, how much
    >> total bytes do heap allocate each variable?

    >
    > You must check to see what actually happens on your implementation.
    > Typically, you should expect 8 to 16 octets of overhead, and possibly
    > some padding. You should __not__ expect 32K.


    The memory manager may well ask the system to increase the heap by 32k
    or some such number, so that allocation may indeed look from the outside
    like it allocated 32k. It's just that subsequent allocations (which are
    smaller than 31k) will use that allocated space as far as possible.
    (In other words, if you make *two* allocations of 1000 bytes, the second
    one will not increase the amount of heap space requested from the system.)

    Of course how much the heap is increased at a time depends on the compiler,
    the runtime environment and/or the OS.
     
    Juha Nieminen, Nov 22, 2011
    #4
    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:
    767
    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:
    635
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,952
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    1,033
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,229
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page