Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11677 > unrolled thread
| Started by | Nephi Immortal <immortalnephi@gmail.com> |
|---|---|
| First post | 2011-11-21 21:10 -0800 |
| Last post | 2011-11-22 07:59 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.c++
Memory Allocation Question Nephi Immortal <immortalnephi@gmail.com> - 2011-11-21 21:10 -0800
Re: Memory Allocation Question Ian Collins <ian-news@hotmail.com> - 2011-11-22 19:18 +1300
Re: Memory Allocation Question Goran <goran.pusic@gmail.com> - 2011-11-21 23:25 -0800
Re: Memory Allocation Question Juha Nieminen <nospam@thanks.invalid> - 2011-11-22 07:59 +0000
| From | Nephi Immortal <immortalnephi@gmail.com> |
|---|---|
| Date | 2011-11-21 21:10 -0800 |
| Subject | Memory Allocation Question |
| Message-ID | <ea77e8d4-e211-4a3e-ab6a-c254ea68fbf9@w7g2000yqc.googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-11-22 19:18 +1300 |
| Message-ID | <9j0t5bFr4nU4@mid.individual.net> |
| In reply to | #11677 |
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
[toc] | [prev] | [next] | [standalone]
| From | Goran <goran.pusic@gmail.com> |
|---|---|
| Date | 2011-11-21 23:25 -0800 |
| Message-ID | <27c07721-b92c-44a1-a87e-de04c86528d7@w15g2000yqc.googlegroups.com> |
| In reply to | #11677 |
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 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 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. 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.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-11-22 07:59 +0000 |
| Message-ID | <4ecb5676$0$4396$7b1e8fa0@news.nbl.fi> |
| In reply to | #11679 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web