Join 136,002 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,376 people online right now. Registration is fast and FREE... Join Now!
Thanks for your reply, Tom. I think the problem is the same using ints. Imagine I want to store in an array eight bits (11110000). Defining bools like ints or chars, this array would take 8 bytes in memory, instead of just 1.
Any other possibility? I cannot find stdbool.h on my Visual Studio.
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with GCC; see the file COPYING. If not, write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/* As a special exception, if you include this header file into source files compiled by GCC, this header file does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */
/* * ISO C Standard: 7.16 Boolean type and values <stdbool.h> */
#ifndef _STDBOOL_H #define _STDBOOL_H
#ifndef __cplusplus
#define bool _Bool #define true 1 #define false 0
#else /* __cplusplus */
/* Supporting <stdbool.h> in C++ is a GCC extension. */ #define _Bool bool #define bool bool #define false false #define true true
#endif /* __cplusplus */
/* Signal that all the definitions are present. */ #define __bool_true_false_are_defined 1
#endif /* stdbool.h */
I don't know that much about bits and bytes, but an int is 4 bits on most machines I think? So an array of 8 ints would only be 4 bytes. That's not that bad.
A short int is only 2 bits.
I can't think of any other suggestions. Why are you so worried about memory?
I don't know that much about bits and bytes, but an int is 4 bits on most machines I think? So an array of 8 ints would only be 4 bytes. That's not that bad.
A short int is only 2 bits.
No, all types must be at least the size of char, according to the standard - and char must be exactly one byte. Of course, there's nothing which specifies how many bits should be in a byte (This is system architecture dependent, although 8 bits is one common size, particularly on personal computers).
C/C++ cannot represent objects smaller than 1 byte - This also remains true when using a struct or union to define bitfields, any type which does not fit into a full byte is subsequently padded to a round number of bytes.
stdbool.h I'm afraid only works for C++, but not for C. Memory is critical in my project because I store every BIT of a file in an array... so I'm using 8 times the size I need, and I really have to solve it.
So there is not another solution than using a BYTE to represent each BIT in the array?
When you read data into an array in C, you're reading in a series of bytes. So unless you want to talk compression algorithms, if you read in every byte from the file, your array will be (at minimum) the same size as your file.
How are you reading the data into the file (code please), and how is the data in the file encoded?
When you read data into an array in C, you're reading in a series of bytes. So unless you want to talk compression algorithms, if you read in every byte from the file, your array will be (at minimum) the same size as your file.
My array is not the same size as my file, is 8 TIMES this size. Because what I'm doing is:
CODE
charArray=readAllBytes(file); // Defined as char [sizeOfFileInBytes]; boolArray=toBits(charArray); // Defined as char [sizeOfFIleInBytes * 8]; /* toBits gets, for each char, 8 chars representing each bit of the charArray[i] */
What I need is to store in boolArray each BIT (not BYTE) of the file, and to use as less memory as possible.
Well, that doesn't tell us anything - those aren't builtins, so we have no idea what those functions do. But there doesn't appear to be any need to size your array with sizeOfFIleInBytes * 8 - allocating space to hold just sizeOfFIleInBytes bytes should suffoce.
When you read in from a file using, say, fread() or fget(), you're reading in a specified number of bytes from the file. So a file 1024 bytes in size can be read into an character array 1024 elements long.
Now, if your file "example.txt" (for example) is a text file made up entirely of the literal character sequence "0000010100111001", the following code would read the entire file into an array:
CODE
#include <stdio.h>
int main() { FILE* myfile; myfile=fopen("example.txt","r+");
char array[16];
fread(array, 1, 16, myfile); // reads 16objects, each 1 byte long, into <array>
return 0; }
The file is 16 bytes in size, and the array is 16 bytes in size. If you check the array by sticking in a display loop
CODE
for (int i=0; i<16; ++i) { printf("%c", array[i]); }
after the reading step, you'll find the exact same "0000010100111001" displayed.
Now because you only have two types of characters in the file, the literals '0' and '1', there is the possibility that this could be compressed down to 2 bytes (on a system where each byte is made up of 8 bits). But if you're reading the file into an array that is 128 bytes long, you're probably doing something incorrectly. If you provide the actual code that you're using to read in the data (rather than the call to the function where the reading takes place), and an example line or two from the file, we may be able to help some more.
Why not overlay 'bit array' functionality onto your byte array?
Since you know the number of bits in a byte is equal to CHAR_BIT, and you presumably know the size of your byte array, you could do something along these lines -
CODE
unsigned char find_bit_at(unsigned char* arr, int pos) { /*Find the array position of the byte containing the bit */ int byte_pos = pos / CHAR_BIT;
/*Find the bit position in the byte */ int bit_pos = pos % CHAR_BIT;
return arr[byte_pos] & ( 1<<bit_pos ); }
For example, Assuming CHAR_BIT == 8, if you wanted bit 20 in a 4-byte array (a 32 bit array), the above function would calculate the byte at position 2, of which, it would return the bit at position 4.
(Code is untested, but I hope you see the general idea)
stdbool.h I'm afraid only works for C++, but not for C. Memory is critical in my project because I store every BIT of a file in an array... so I'm using 8 times the size I need, and I really have to solve it.
So there is not another solution than using a BYTE to represent each BIT in the array?
devesa
stdbool.h is part of the C99 standard. It works fine in C.