Is it good to use char instead of int to save memory?

D

dspfun

Hi,

Is it recommended to use char instead of int for variables that you
know never will contain a value larger than 255? A small example
follows:

#include <stdio.h>
int main(void)
{
//int i = 1;
char i = 1; //is this better than previous line?
printf("i=%d\n",i);
return 0;
}

What are the pros and cons of using char instead of int when values
will not exceed 255?

Brs,
Markus
 
M

Marc Boyer

Le 18-03-2010 said:
Is it recommended to use char instead of int for variables that you
know never will contain a value larger than 255? A small example
follows:

#include <stdio.h>
int main(void)
{
//int i = 1;
char i = 1; //is this better than previous line?
printf("i=%d\n",i);
return 0;
}

What are the pros and cons of using char instead of int when values
will not exceed 255?

First, you have to use signed char or unsigned char to get portable
code, because char can be signed or not.

Second, well, it depends... On modern PC CPU, there are alignment constraints
leading common compilers to use the same amount of memory for a single
variable char and a single variable int.

But, it could be different in other contexts. For example, some collegue of
me have worked on a DSP where int and char are both 32bits values.

This is different with arrays.
int vali[1024];
char valc[1024];

In this case, valc will use less memory than vali (the ratio beeing
sizeof(char) ).

Marc Boyer
 
D

Dr Malcolm McLean

What are the pros and cons of using char instead of int when values
will not exceed 255?
As others have said, you need to specify signed char or unsigned char.
Plain chars are for actual characters.
It's rare that memory is so tight that declaring a single variable as
"char" is a saving worth having, and the operation on a char might
well be slower. int is meant to be the natural integer representation
for the machine.
However if you have a large array, char might save significnat space.
The obvious example is a raster buffer for image data. Usually we
allow 8 bits per rgb channel. So an array of unsigned chars is the
obvious representation.
 
S

spinoza1111

Hi,

Is it recommended to use char instead of int for variables that you
know never will contain a value larger than 255? A small example
follows:

#include <stdio.h>
int main(void)
{
  //int i = 1;
  char i = 1; //is this better than previous line?
  printf("i=%d\n",i);
  return 0;

}

What are the pros and cons of using char instead of int when values
will not exceed 255?

Brs,
Markus

I'm sure one of the regulars will be all agog to remind you that chars
don't necessarily range between 0 and 255.

Pro:

* Memory saved
* Speed IF your "digital signal processor" has hardwired character
arithmetic
* Everyone will think you're smart

Con:

* Memory lost when your "digital signal processor" does char arith by
transforming the char into an integer
* Speed in the same scenario
* Negative numbers?
* Everyone will think you're an idiot

Decision:

When all else fails, program on purpose
When the wind is out of your sails, code what you mean
A char actor is not a number, and one would suppose
That arithmetic doesn't happen between consenting chars, jellybean.

Don't get cute, don't show off:
The urge to grandstand should be blown off.
If it's a natural number be a regular guy
And use int, say I with a sigh.
 
E

Eric Sosman

Hi,

Is it recommended to use char instead of int for variables that you
know never will contain a value larger than 255? A small example
follows:

#include<stdio.h>
int main(void)
{
//int i = 1;
char i = 1; //is this better than previous line?

No.
printf("i=%d\n",i);
return 0;
}

What are the pros and cons of using char instead of int when values
will not exceed 255?

If you need [0..255] you should use unsigned char, not plain
char. (If you need [-127..127] make it signed char.)

Using a char (or short) variant instead of an int will reduce
data size. This is worth doing only if the data is "too big,"
which usually means one of two things:

- You are storing "billyuns and billyuns" of data instances.

- You are packing data into an externally-specified and size-
limited format ("Make it all fit in a 32-byte record").

On some processors, using sub-int data types may increase
code size (sometimes significantly) and execution time (usually
not by much).
 
J

Julienne Walker

I'm sure one of the regulars will be all agog to remind you that chars
don't necessarily range between 0 and 255.

Unfortunately, the OP didn't give us an absolute range of allowed
values to work with, so it would be unwise to assume the lower limit
of his allowed range given only the upper limit. In this case I would
ask for more information before answering with an unwarranted
assumption, or give multiple answers depending on the most reasonable
assumptions:

If we're talking about values that also won't be negative, an unsigned
char is fine and there's no need to talk about ranges because the 8-
bit range is the required minimum. On the other hand, if negative
values *are* allowed (ie. signed char), the upper limit of 255 is
beyond the guaranteed minimum and short or int would be better suited.
Pro:
*  Everyone will think you're smart
Con:
*  Everyone will think you're an idiot

I'm confident that one's mental abilities aren't judged by choice of
numeric data type in C. ^_^ Some people might, but for those people I
would recommend chilling out.
 
E

Ersek, Laszlo

I wouldn't use types lower ranking than int,
unless they were in an array.

This. The variable will be promoted, most of the time, to int or
unsigned int. Such enclosing expressions will have to prepare for both
signednesses. I'd say save those explicit casts and use unsigned int for
individual variables.

lacos
 
B

bartc

Julienne said:
Unfortunately, the OP didn't give us an absolute range of allowed
values to work with, so it would be unwise to assume the lower limit
of his allowed range given only the upper limit. In this case I would
ask for more information before answering with an unwarranted
assumption, or give multiple answers depending on the most reasonable
assumptions:

This is an even more pedantic answer than spinoza was anticipating.

I would guess the OP had a lower bound of 0 in mind, wouldn't you? Unless
there's a one in a thousand chance he's working with 9-bit chars. And one in
a million chance of something even weirder.
 
B

bartc

pete said:
I wouldn't use types lower ranking than int,
unless they were in an array.


This code prints 2147303489 on my machine, instead of 65; changing to char
ch fixes that:

#include <stdio.h>

int main(void){
int ch;
char* p=&ch;

*p=65;

printf("CH = %d\n",ch);
}

It happens that char pointers might sometimes point inside structs and
arrays, and sometimes at standalone variables.
 
D

dspfun

This is an even more pedantic answer than spinoza was anticipating.

I would guess the OP had a lower bound of 0 in mind, wouldn't you? Unless
there's a one in a thousand chance he's working with 9-bit chars. And one in
a million chance of something even weirder.

The range will be between 0 and 8.
 
D

dspfun

Is it recommended to use char instead of int for variables that you
know never will contain a value larger than 255? A small example
follows:
#include<stdio.h>
int main(void)
{
   //int i = 1;
   char i = 1; //is this better than previous line?

     No.
   printf("i=%d\n",i);
   return 0;
}
What are the pros and cons of using char instead of int when values
will not exceed 255?

     If you need [0..255] you should use unsigned char, not plain
char.  (If you need [-127..127] make it signed char.)

     Using a char (or short) variant instead of an int will reduce
data size.  This is worth doing only if the data is "too big,"
which usually means one of two things:

     - You are storing "billyuns and billyuns" of data instances.

     - You are packing data into an externally-specified and size-
       limited format ("Make it all fit in a 32-byte record").

     On some processors, using sub-int data types may increase
code size (sometimes significantly) and execution time (usually
not by much).

Could you give some more examples of why it is bad (or not worth) to
store numbers (that we know will have a range for example between 0
and 8) in unsigned chars?
 
D

dspfun

Is the same reasoning as you all have described above valid for the
following two versions of my_struct1 and my_struct2, i.e. is
my_struct2 better than my_struct1 ?

typedef struct
{
unsigned int u;
unsigned char v; //will never be larger than 255
unsigned char w;//will never be larger than 255
}my_struct1;


typedef struct
{
unsigned int u;
unsigned int v;//will never be larger than 255
unsigned int w;//will never be larger than 255
}my_struct2;

?
 
E

Eric Sosman

Is it recommended to use char instead of int for variables that you
know never will contain a value larger than 255?
[...]
On some processors, using sub-int data types may increase
code size (sometimes significantly) and execution time (usually
not by much).

Could you give some more examples of why it is bad (or not worth) to
store numbers (that we know will have a range for example between 0
and 8) in unsigned chars?

Which part of "using sub-int data types may increase code
size and execution time" are you having trouble with?

(By the way, it's considered silly or even bad form to
quote signatures.)
 
E

Eric Sosman

This code prints 2147303489 on my machine, instead of 65; changing to
char ch fixes that:

#include <stdio.h>

int main(void){
int ch;
char* p=&ch;

*p=65;

printf("CH = %d\n",ch);
}

The undefined behavior of broken code is not a good basis
on which to choose variable types.
 
D

dspfun

On 3/18/2010 7:40 AM, dspfun wrote:
Is it recommended to use char instead of int for variables that you
know never will contain a value larger than 255?
[...]
      On some processors, using sub-int data types may increase
code size (sometimes significantly) and execution time (usually
not by much).

Could you give some more examples of why it is bad (or not worth) to
store numbers (that we know will have a range for example between 0
and 8) in unsigned chars?

     Which part of "using sub-int data types may increase code
size and execution time" are you having trouble with?

     (By the way, it's considered silly or even bad form to
quote signatures.)

Sorry about including the signature!

Consider if we/you know that "using sub-int data types" does not
increase code size and execution time, what are the drawbacks of using
unsigned char for numbers in the range 0 to 255?
 
T

Thad Smith

Eric said:
On 3/18/2010 7:40 AM, dspfun wrote:
Using a char (or short) variant instead of an int will reduce
data size. This is worth doing only if the data is "too big,"
which usually means one of two things:

- You are storing "billyuns and billyuns" of data instances.

- You are packing data into an externally-specified and size-
limited format ("Make it all fit in a 32-byte record").

- You are near the resources of the processor.
On some processors, using sub-int data types may increase
code size (sometimes significantly) and execution time (usually
not by much).

On some processors using int data types increases code size and execution time
over char data types. You are normally aware if that is so.
 
E

Eric Sosman

On 3/18/2010 7:40 AM, dspfun wrote:
Is it recommended to use char instead of int for variables that you
know never will contain a value larger than 255?
[...]
On some processors, using sub-int data types may increase
code size (sometimes significantly) and execution time (usually
not by much).

Could you give some more examples of why it is bad (or not worth) to
store numbers (that we know will have a range for example between 0
and 8) in unsigned chars?

Which part of "using sub-int data types may increase code
size and execution time" are you having trouble with?
Consider if we/you know that "using sub-int data types" does not
increase code size and execution time, what are the drawbacks of using
unsigned char for numbers in the range 0 to 255?

None that I can think of, given the prior knowledge. One
problem with such knowledge, though, is that code quite often
outlives its original environment, and that something you know
about the machine on which you developed it turns out to be
untrue about the next machine on which it runs. To put it
another way, hardware changes more rapidly than software (even
though "soft" suggests the opposite).
 
B

bartc

Consider if we/you know that "using sub-int data types" does not
increase code size and execution time, what are the drawbacks of using
unsigned char for numbers in the range 0 to 255?

I really wouldn't worry about it too much. Just use char if the data is
small.

And inside structs, definitely use char: the char fields will pack tighter;
if you have many structs, you save memory (and the data is not so spread
out), and sometimes you can do tricks such as handle several char values as
a single int.

If performance is really an issue, then you can experiment with using int
instead of char for individual variables (as mixing int and char in
expressions, or passing char values to a function, may involve widening).

Using int data-types instead of char, can also increase not only code size
and execution time, but also data size.
 
B

bartc

Eric said:
The undefined behavior of broken code is not a good basis
on which to choose variable types.

But using a single char variable (which everyone has been arguing against)
fixes that.
 
E

Eric Sosman

On 3/18/2010 7:40 AM, dspfun wrote:
Is it recommended to use char instead of int for variables that you
know never will contain a value larger than 255?
[...]
On some processors, using sub-int data types may increase
code size (sometimes significantly) and execution time (usually
not by much).

Could you give some more examples of why it is bad (or not worth) to
store numbers (that we know will have a range for example between 0
and 8) in unsigned chars?

Which part of "using sub-int data types may increase code
size and execution time" are you having trouble with?
Consider if we/you know that "using sub-int data types" does not
increase code size and execution time, what are the drawbacks of using
unsigned char for numbers in the range 0 to 255?

None that I can think of, given the prior knowledge. One
problem with such knowledge, though, is that code quite often
outlives its original environment, and that something you know
about the machine on which you developed it turns out to be
untrue about the next machine on which it runs. To put it
another way, hardware changes more rapidly than software (even
though "soft" suggests the opposite).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top