Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Memory Allocation Question (http://www.velocityreviews.com/forums/t806181-memory-allocation-question.html)

 Nephi Immortal 11-22-2011 05:10 AM

Memory Allocation Question

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?

 Ian Collins 11-22-2011 06:18 AM

Re: Memory Allocation Question

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.

> 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

 Goran 11-22-2011 07:25 AM

Re: Memory Allocation Question

On Nov 22, 6:10*am, Nephi Immortal <immortalne...@gmail.com> 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

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.

 Juha Nieminen 11-22-2011 07:59 AM

Re: Memory Allocation Question

Goran <goran.pusic@gmail.com> 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.

 All times are GMT. The time now is 01:56 PM.