Computing.Net > Forums > Programming > Using char for int to save memory?

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Using char for int to save memory?

Reply to Message Icon

Original Message
Name: Yisrael Harris
Date: September 5, 2002 at 00:08:13 Pacific
Subject: Using char for int to save memory?
OS: Don't know
CPU/Ram: Don't know
Comment:

My data size is such that I am constantly looking for ways to save memory in my program. One thing I noticed is that sizeof(int) on my computer returns 2. So I thought that I could use a smaller data type, and save 1 byte on each field. Unfortunately, on my computer sizeof(short int) and sizeof(unsigned short int) also return 2.

I am therefore searching for a data type which is only 1 byte, since that is all I really need for several numeric fields. I noticed that sizeof(char) returns 1.

So 2 questions:

1. Is there any other possible data type similar to int, aside from short int and unsigned short int, which might return 1? My guess is no.

2. Is there any way to sort of 'fool' the computer, so to speak, by using char variables when what you are really storing is int data? I tried it and, not surprisingly, got incompatibility messages -- even when I used typecasting.

Any feedback WHICH DIRECTLY ATTEMPTS TO ANSWER THESE TWO SPECIFIC QUESTIONS ONLY would be welcome. Thank you.


Report Offensive Message For Removal


Response Number 1
Name: peter4u
Date: September 5, 2002 at 05:51:40 Pacific
Reply: (edit)

I think, there is the data type "byte" and also "unsigned byte".

If you have only positive integers less than 256 you can use "unsigned byte" if you also need negative integers use "byte" and make sure that you stay in the range of -128 to +127.

Keep in mind, that you have an "alignment" issue, when you are using dataelements of different length and also pointers, especially when you use it in a structure.


If you have a structure:

struct abc {
char a,
char *b,
};

you will lose 3 or 7 bytes of usable space, because the pointer is automatically moved to an address, which is a multiple of the address size. So on a 32bit machine you would loose 3 bytes and on a 64bit machine you would loose 7.

If you'd fill that space with other userful stuff, you don't loose anything:


struct abc {
char a,
char c,
int n,
char *b,
};

If this is too confusing, pls e-mail and I'll find a way to explain in more detail.


Report Offensive Follow Up For Removal

Response Number 2
Name: Joe Neal
Date: September 5, 2002 at 12:36:14 Pacific
Reply: (edit)

What language?

In C I use "char" for a single signed byte and "unsigned char" for an unsigned byte (0 - 255). In my compilers (Borland) I may get a warning but nothing else (unless I use them wrong in my programming then I get loops and crashes!). The only thing I have to keep in mind is how dinky small the data is and the difference between signed and unsigned. For example:

char V1; // legal values -128 to 127
// actually, this might be -127 to 128 depending on operating system and compiler

unsigned char V2; // legal values 0 to 255

At least I used to be a byte-grinch until I found out:

Setting the compiler to WORD alignment pads out all byte-sized data to 2 bytes. (Meaning I wasted space). Even when saving data to disk...

Not having it WORD aligned was slower.

Using char sized variables for loops is slower (the compiler works a routine to pad it to integer size and loop!).

Writing code to interpret and debug non-standard data wastes too much time. And can use more memory than is saved (code isn't free - every line takes memory).

Type casting isn't free, either.

In the end, my take: buy more storage if at all possible. If not, learn to create your own interpretive variables if you can (an array of integers read one byte at a time). Or use more effecient storage.

Maybe even learn to use home-grow compression routines if data tends to repeat a lot.

Most people I have seen had to define a "byte" for use in C-programs something like this:

#typedef unsigned char BYTE;
#typedef unsigned char byte;

Which is just using an unsigned char with a different name.


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software