H
Henning
Hi
Problems translating C-code to Basic.
Is unsigned char x; by default setting x to 0?
/Henning
Problems translating C-code to Basic.
Is unsigned char x; by default setting x to 0?
/Henning
Henning said:Problems translating C-code to Basic.
Is unsigned char x; by default setting x to 0?
It depends. If you define an automatic variable it's not initialized,
i.e. if you have something like this
int myfunction( char first_arg, int sec_arg )
{
unsigned char x;
...
}
then 'x' will have some random value (which might, just by luck, be 0).
When you port code you may still have to keep in mind that on some
platforms also such automatic variables get always initialized to 0
and if the program is relying on that non-standard feature you will
have to minimc this behavior.
If, on the other hand, 'x' is a global variable (i.e. one that isn't
defined within a function) then it will be initialized to 0.
Regards, Jens
It depends. If you define an automatic variable it's not initialized,
i.e. if you have something like this
int myfunction( char first_arg, int sec_arg )
{
unsigned char x;
...
}
then 'x' will have some random value (which might, just by luck, be 0).
Kelsey Bjarnason said:Maybe I'm missing something; is there any guarantee that x will have _any_
value whatsoever? Last I checked, attempting to take the value of an
uninitialized variable was undefined behaviour; the implementation is
perfectly within its rights to note that while there _will be_ an address
reserved for x, there may not have yet been one reserved, hence an attempt
to retrieve the value could merrily cause a crash.
<snip>Henning said:Hi
Problems translating C-code to Basic.
Is unsigned char x; by default setting x to 0?
Richard said:For normal types, yes. However, all possible unsigned char
representations are required to be valid; there are no trap
values in unsigned char.
CBFalconer said:I doubt whether it occurs anywhere, but while your assertion of no
trap values is correct, there is no prohibition against other
means of detecting uninitialized values. For example, every byte
of storage could have an associated bit, reset on power-on, and
set when anything is written into the byte.
I discussed this matter on comp.std.c once,
and my recollection was that the consensus was that
you could read unitialised bytes as unsigned char,
and from there it followed that reading an uninitialized
unsigned char variable was merely unspecified behavior.
Kelsey Bjarnason said:[snips]
I discussed this matter on comp.std.c once,
and my recollection was that the consensus was that
you could read unitialised bytes as unsigned char,
and from there it followed that reading an uninitialized
unsigned char variable was merely unspecified behavior.
I'd question that. Try this:
unsigned char x,y;
x = y;
What prevents an implementation from simply not allocating space for y
until it has been initialized - which would mean the code goes zot?
pete said:That would be a trap representation.
I discussed this matter on comp.std.c once,
and my recollection was that the consensus was that
you could read unitialised bytes as unsigned char,
and from there it followed that reading an uninitialized
unsigned char variable was merely unspecified behavior.
CBFalconer said:As I said, and you snipped, think of a parity bit.
pete said:I know unsigned char as having only value bits.
Kelsey Bjarnason said:What prevents an implementation from simply not allocating space for [an object of type char and named]
y until it has been initialized ...
There are at least some limitations on this kind of thing. The
following is well-defined:
unsigned char x;
unsigned char *ptr = &x;
*ptr = 42;
x has to at least have a valid address before it's initialized.
Conceivably there needn't be any memory there, but if there isn't then
the memory has to be allocated *at that address* on the first
assignment.
Hi
Problems translating C-code to Basic.
Is unsigned char x; by default setting x to 0?
RoSsIaCrIiLoIA said:On Wed, 4 Aug 2004 01:20:12 +0200, "Henning"
if it is *out* every function or procedure (global):
YES you have to set x=0
if it is *in* any function or procedure
if there is
"static unsigned char x;" then you have to set x=0
else you can set x=0 or not;
I would say if you see "unsigned char x;" set always x=0.
^^^^^^^^^^^^^^^******************************************
<sigh> could you (RoSsIaCrIiLoIA) check your posts for correctness
*before*
you post them. Since I'm criticising someone this ensures this post
will
have at least one error...
if it outside any function (has file scope) then no you don't have to
initialise it as the compiler automatically initialises it to
0 (zero).
unsigned a;Note C has no "procedures".
unsigned a;
void proced(void){++a;}
Does it seem a function?
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.