Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11678

Re: Memory Allocation Question

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Memory Allocation Question
Date 2011-11-22 19:18 +1300
Message-ID <9j0t5bFr4nU4@mid.individual.net> (permalink)
References <ea77e8d4-e211-4a3e-ab6a-c254ea68fbf9@w7g2000yqc.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web