Memory Allocation Question

N

Nephi Immortal

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?
 
I

Ian Collins

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.
 
G

Goran

        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.
 
J

Juha Nieminen

Goran said:
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.
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top