Notices
Results 1 to 22 of 22

Thread: How much memory is reserved when a Boolean variable is declared?

  1. #1 How much memory is reserved when a Boolean variable is declared? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    When i create a Boolean variable, what happens?
    Is there only 1 bit reserved for this variable?
    Then how does the program know the memory location of this bit when i want to read/modify it?
    How about the memory location of the memory location containing the address of the bit?
    Does the program reserve more memory to store the address of the memory location of this bit?
    Do dynamic variables reserve more data than static variables?


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Freshman
    Join Date
    Mar 2014
    Posts
    65
    If you have not allocated a mapped block to use, a word would be created for it long with its handle..

    What hardware/software are we talking about?

    In machine language, a masked word is often pushed onto the stack for temporary such data. You must keep track of that yourself.


    Reply With Quote  
     

  4. #3  
    Forum Senior
    Join Date
    Sep 2013
    Posts
    305
    There is usually 8 bits reserved (at least in C++) I think the JVM may even use more... I think a byte is the smallest addressable unit.
    Last edited by Trivium; April 21st, 2014 at 07:48 AM.
    Reply With Quote  
     

  5. #4  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    As mentioned, the answer to your questions depends on which programming language you're using, and even which versions of those languages, or which data structures you're using. Some languages/libraries do provide a bit vector object that handles the details of addressing a single bit (though it still can't reserve less than a byte on most computers).
    Reply With Quote  
     

  6. #5  
    Forum Professor river_rat's Avatar
    Join Date
    Jun 2006
    Location
    South Africa
    Posts
    1,497
    Quote Originally Posted by RamenNoodles View Post
    How about the memory location of the memory location containing the address of the bit?
    Variable memory locations are handled by the compiler and linker - and the exact details depend on the actual storage medium of the variable (stack or heap).

    Do dynamic variables reserve more data than static variables?
    That depends on exactly what your definition of static and dynamic is here.
    As is often the case with technical subjects we are presented with an unfortunate choice: an explanation that is accurate but incomprehensible, or comprehensible but wrong.
    Reply With Quote  
     

  7. #6  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    Quote Originally Posted by Trivium View Post
    There is usually 8 bits reserved (at least in C++) I think the JVM may even use more... I think a byte is the smallest addressable unit.
    Do you mean that most compilers have to reserve a minimum 1byte(8bits) to store a boolean(1bit) data?

    EDIT: Isn't it wasting memory?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  8. #7  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    Quote Originally Posted by MagiMaster View Post
    As mentioned, the answer to your questions depends on which programming language you're using, and even which versions of those languages, or which data structures you're using. Some languages/libraries do provide a bit vector object that handles the details of addressing a single bit (though it still can't reserve less than a byte on most computers).
    I'm not referring to most programming language in general.

    Isn't a boolean variable just 1 or 0?

    By bit vector, do you mean a compile-time reserved array of bits to store 1-bit(boolean) data that the application may use during run time?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  9. #8  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    These questions do not have a general answer. They depend on what programming language the program in question in written in, what computer it was written for and on design decisions made by the programmer. If you know nothing about programming or data structures, then forget bit vectors. They won't make any sense to you.

    In short though, a computer cannot address a single bit. Most current computers address single bytes. Yes this wastes space if you don't need a full bytes worth of information, but not enough space for anyone to care. (The tradeoffs to change that would not be worth it.)
    Reply With Quote  
     

  10. #9  
    Forum Senior
    Join Date
    Sep 2013
    Posts
    305
    Quote Originally Posted by MagiMaster View Post
    These questions do not have a general answer. They depend on what programming language the program in question in written in, what computer it was written for and on design decisions made by the programmer. If you know nothing about programming or data structures, then forget bit vectors. They won't make any sense to you.

    In short though, a computer cannot address a single bit. Most current computers address single bytes. Yes this wastes space if you don't need a full bytes worth of information, but not enough space for anyone to care. (The tradeoffs to change that would not be worth it.)
    If a program is programmed so poorly that a boolean has a significant impact on memory usage you're doing it wrong.
    Reply With Quote  
     

  11. #10  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    Quote Originally Posted by Trivium View Post
    Quote Originally Posted by MagiMaster View Post
    These questions do not have a general answer. They depend on what programming language the program in question in written in, what computer it was written for and on design decisions made by the programmer. If you know nothing about programming or data structures, then forget bit vectors. They won't make any sense to you.

    In short though, a computer cannot address a single bit. Most current computers address single bytes. Yes this wastes space if you don't need a full bytes worth of information, but not enough space for anyone to care. (The tradeoffs to change that would not be worth it.)
    If a program is programmed so poorly that a boolean has a significant impact on memory usage you're doing it wrong.
    Pretty much. A typical modern desktop has about 2 GB of memory, which is enough room for over 2 billion bytes of data (and things like paging to handle even more). Even if you're just storing one boolean per byte, you have to be doing some major number crunching to use up that much memory. (Or do what I do and just leave 100 tabs open in the browser. :P)
    Reply With Quote  
     

  12. #11  
    Forum Professor river_rat's Avatar
    Join Date
    Jun 2006
    Location
    South Africa
    Posts
    1,497
    Memory fragmentation can cause many issues though - one miss placed byte and suddenly you have halved your usable heap allocation size. Less of a problem in the 64bit era but good lord did this cause issues in my life a few years ago.
    As is often the case with technical subjects we are presented with an unfortunate choice: an explanation that is accurate but incomprehensible, or comprehensible but wrong.
    Reply With Quote  
     

  13. #12  
    Forum Freshman
    Join Date
    Mar 2014
    Posts
    65
    Quote Originally Posted by RamenNoodles View Post
    I'm not referring to most programming language in general.

    Isn't a boolean variable just 1 or 0?

    By bit vector, do you mean a compile-time reserved array of bits to store 1-bit(boolean) data that the application may use during run time?
    You have to refer to something, the range of options is pretty big.
    Let's say a byte is reserved for your variable using a higher level language.
    That same byte can hold seven other Boolean variables too.
    The byte is read in the same for any of the eight bits, but the unwanted bits are ignored by masking.
    Hardware I/O bits are handled the same way but the address is static.

    If only one Boolean variable is used in the program, this bit handling is not usually necessary.
    Reply With Quote  
     

  14. #13  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    Quote Originally Posted by MagiMaster View Post
    Quote Originally Posted by Trivium View Post
    Quote Originally Posted by MagiMaster View Post
    These questions do not have a general answer. They depend on what programming language the program in question in written in, what computer it was written for and on design decisions made by the programmer. If you know nothing about programming or data structures, then forget bit vectors. They won't make any sense to you.

    In short though, a computer cannot address a single bit. Most current computers address single bytes. Yes this wastes space if you don't need a full bytes worth of information, but not enough space for anyone to care. (The tradeoffs to change that would not be worth it.)
    If a program is programmed so poorly that a boolean has a significant impact on memory usage you're doing it wrong.
    Pretty much. A typical modern desktop has about 2 GB of memory, which is enough room for over 2 billion bytes of data (and things like paging to handle even more). Even if you're just storing one boolean per byte, you have to be doing some major number crunching to use up that much memory. (Or do what I do and just leave 100 tabs open in the browser. :P)
    it would matter for high-end 3d simulation games!
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  15. #14  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    What takes up memory in modern games and simulators is mainly textures. Each pixel of a texture takes up 3 or 4 bytes, which translates to 3 or 4 MB for a 1024x1024 texture (a common hi-res texture size). Second to that would be sound which takes up something like 176 kB per second. (That's for effects. Music is often stored in a different format.) High poly models can also take up quite a bit of space. Each vertex needs at least 12 bytes (and often uses at least 24). Each triangle needs another 12 bytes, although that can sometimes be cut down to around 4 bytes. For a 10k poly model, that adds up to around 280 kB per model. Animations can add to all of those.

    Even if you're wasting space using a full 64-bit integer for each game flag (usually just a boolean value), there's no where near enough flags in a game to eat up a significant amount of memory. Of course, if you're programming for a retro console (people still make new NES and GameBoy games) it might be something to watch out for. (I was reading that in some old games they would use otherwise-unused areas of the screen as extra memory.)
    Reply With Quote  
     

  16. #15  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    Yeah, thanks for the replies, I've learned that the amount of memory reserved is highly dependent on the compiler and language.

    "In short though, a computer cannot address a single bit."
    but i would like to know, is there any reason why this is so?

    or is it just protocol?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  17. #16  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,489
    There are (or have been) architectures that can address individual bits. But usually the extra cost is not worth any possible performance gains. (Note that every extra instruction has a cost in terms of silicon area, design and test time, etc.) Some embedded controllers include bit addressing because this is a useful function when controlling peripheral devices.
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  18. #17  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    The reason your desktop can't is memory. When computers were said to be 16 or 32 bits, what that meant was that an address was 16 or 32 bits long. That means there were 2^16 (65536) or 2^32 (about 4 billion) addresses. So you couldn't have more memory units than there were addresses (paging aside). If a memory unit was only 1 bit, that means that you'd only have 8 kB instead of 64 kB in a 16-bit machine or 512 MB instead of 4 GB in a 32-bit machine. With a 64-bit machine, it can address about 10^19 individual addresses, so 1/8 of that would still be plenty of memory, but most 64-bit machines are just bigger versions of older 32-bit architectures. Plus as Strange pointed out, there's other reasons it's not done.
    Reply With Quote  
     

  19. #18  
    Forum Freshman
    Join Date
    Mar 2014
    Posts
    65
    Quote Originally Posted by RamenNoodles View Post
    "In short though, a computer cannot address a single bit."
    but i would like to know, is there any reason why this is so?

    or is it just protocol?
    16-bit processors ruled for many years. The primary registers of the CPU, and the busses supplying them were 16-bits.
    A 16-bit CPU is designed to only work with 16-bit data, that being 0000h to FFFFh or 0000000000000000b to 1111111111111111b.
    Memory addressing schemes have changed quite a bit over time but the data size was set by physical architecture.
    In binary on a 16 bit processor, the difference between the numbers 0 and 1 is 0000000000000000 and 0000000000000001.
    You cannot store such a 16 bit number in a single bit address. You have to use a word (two bytes).
    A simple AND mask allows us to use this same word (two bytes) to record 16 individual bits.
    The mask 0000000000000001 reads the first bit, the mask 0000000000000010 reads the second bit, and so on. The result of such a mask is either zero or not, revealing the value of the bit.
    Bitmasks also waste memory space so this does not save anything, it is just the only way to address one bit.
    If you only use a couple of Boolean variables you would just keep them as numbers.

    Networks use masks to direct traffic between sub-networks in the same way.
    Reply With Quote  
     

  20. #19  
    Forum Freshman pzkpfw's Avatar
    Join Date
    Mar 2014
    Location
    New Zealand
    Posts
    79
    Just for another example (not strictly programming), MS SQL Server (and other DBMS's, I suppose) will gather up Booleans (bit fields) when building rows in a database table. Up to 8 into a byte, etc.

    Here, it makes sense to spend some CPU cycles handling the packing and unpacking of the bits, because a database table might have any number of rows - the potential "wasted" space could become significant. (i.e. a megabyte per Boolean; if the table has a million records).

    Say you are writing a game and the soldier object has an "IsWounded" Boolean - will there be 1 million soldiers? Does a byte for that boolean really matter? Just roll your own bit packer if that's really an issue.
    Reply With Quote  
     

  21. #20  
    Forum Freshman Randwolf's Avatar
    Join Date
    Apr 2013
    Posts
    30
    Quote Originally Posted by RamenNoodles View Post
    Yeah, thanks for the replies, I've learned that the amount of memory reserved is highly dependent on the compiler and language.
    I clicked this thread to reply just so. Knew I was late to the game, but hey... Some languages want 8 bits, others ???. Depends. I know of nothing other than assembly that will allow direct address of 0's and 1's....

    Assembly is where "banging code" came from - google it... Banging code meant exactly that.
    Reply With Quote  
     

  22. #21  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,489
    C supports bitfields in structures, which allow easy addressing of single or multiple-bit data. C++ also includes the bitset class for optimised storage of bits.
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  23. #22  
    Forum Bachelors Degree PetTastic's Avatar
    Join Date
    Jul 2010
    Location
    London UK
    Posts
    421
    Many computer architectures have strong alignment restrictions and performance issues related to alignment.
    You may find that when you turn up the optimisation settings on the compiler for speed, local bool vars (stored in the stack frame) become 32 bit aligned and so so padded to 32 bits.
    I believe in nothing, but trust gravity to hold me down and the electromagnetic force to stop me falling through
    Physics is the search for the best model not the truth, as only mythical beings know that.
    Reply With Quote  
     

Similar Threads

  1. What boolean operation will suit this need in C++?
    By RamenNoodles in forum Computer Science
    Replies: 12
    Last Post: July 8th, 2013, 08:31 AM
  2. Boolean algebra question
    By xtheunknown0 in forum Computer Science
    Replies: 5
    Last Post: April 21st, 2011, 05:08 PM
  3. Boolean algebra
    By asalvani in forum Computer Science
    Replies: 16
    Last Post: June 3rd, 2009, 02:31 AM
  4. Photographic memory (Eidetic memory)
    By blue_space87 in forum Behavior and Psychology
    Replies: 11
    Last Post: March 28th, 2009, 02:13 AM
  5. Boolean Alegbra
    By cheekychop in forum Computer Science
    Replies: 2
    Last Post: May 14th, 2008, 05:15 PM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •