Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory Allocation Question

Reply
Thread Tools

Memory Allocation Question

 
 
Nephi Immortal
Guest
Posts: n/a
 
      11-22-2011
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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-22-2011
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
 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      11-22-2011
On Nov 22, 6:10*am, Nephi Immortal <(E-Mail Removed)> 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.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-22-2011
Goran <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
memory allocation and freeing memory Rodrigo Dominguez C Programming 11 06-14-2005 11:54 PM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM



Advertisments