473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

The one who can't C

Hi
Problems translating C-code to Basic.
Is unsigned char x; by default setting x to 0?

/Henning
--
Time is present only to prevent everything from happening at once.
Still it seems that everything happens at once.
Then there must be a bug in time.
To find the bug in time, isn't that what we all hope for.
Nov 14 '05 #1
17 1057
Henning <co***********@coldmail.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2


--
Time is present only to prevent everything from happening at once.
Still it seems that everything happens at once.
Then there must be a bug in time.
To find the bug in time, isn't that what we all hope for.
<Je***********@physik.fu-berlin.de> skrev i meddelandet
news:2n************@uni-berlin.de...
Henning <co***********@coldmail.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


The code is for an Atmel Risc MCU. I guess it is written in IAR's C4AVR.
The var's are defined within functions, so I guess I have to look closer
what is happening.
Thx for helping!
/Henning
Nov 14 '05 #3
[snips]

On Tue, 03 Aug 2004 23:34:43 +0000, Jens.Toerring wrote:
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).


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.
Nov 14 '05 #4
Kelsey Bjarnason <ke*****@xxnospamyy.lightspeed.bc.ca> wrote:
On Tue, 03 Aug 2004 23:34:43 +0000, Jens.Toerring wrote:
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).


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.


For normal types, yes. However, all possible unsigned char
representations are required to be valid; there are no trap values in
unsigned char.

Richard
Nov 14 '05 #5
Henning wrote:
Hi
Problems translating C-code to Basic.
Is unsigned char x; by default setting x to 0?

<snip>

Perhaps you shouldn't be translating C to anything if you can't be
bothered to pick up a book on C and at least take a glance at it.
Mark F. Haigh
mf*****@sbcglobal.net
Nov 14 '05 #6
Richard Bos wrote:
Kelsey Bjarnason <ke*****@xxnospamyy.lightspeed.bc.ca> wrote:
On Tue, 03 Aug 2004 23:34:43 +0000, Jens.Toerring wrote:
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).


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.


For normal types, yes. However, all possible unsigned char
representations are required to be valid; there are no trap
values in unsigned char.


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. Think of a parity
bit.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02

Nov 14 '05 #7
CBFalconer wrote:

Richard Bos wrote:
Kelsey Bjarnason <ke*****@xxnospamyy.lightspeed.bc.ca> wrote:
On Tue, 03 Aug 2004 23:34:43 +0000, Jens.Toerring wrote:

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).

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.


For normal types, yes. However, all possible unsigned char
representations are required to be valid; there are no trap
values in unsigned char.


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.


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.

--
pete
Nov 14 '05 #8
[snips]

On Wed, 04 Aug 2004 15:57:12 +0000, pete wrote:
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?

This is a little different from say, using a pointer-to-byte to poke
around memory (video memory, say), as it's assumed that the target region
actually does exist and pointing the pointer at it initializes the pointer.

Then we have this:

double d[100];
unsigned char *s = (unsigned char *)&d;

Attempts to read *s, IMO, are at risk of a fault. Unlike the case of
poking about in video memory, etc, there's no assurance that the memory
for d has been allocated yet. Ponder the following in terms of the
generated pseudo assembler:

double d[100];
unsigned char *s = (unsigned char *)&d;
unsigned char c;
c = *s;
d[0] = 100.0;

..asm:

data d ?
data s ?
data c ?

addr1 <- addr d ; get address of d
mlock [s], 4 ; assign space for s
s <- addr1 ; store address of d in s
mlock [c],1 ; assign space for c
c <- (s) ; store value of *s in c
mlock [d], 100 * 8 ; assign space for d

That is, the system doesn't even _create_ a memory region for d until d is
initialized; the attempt to view the contents of that region via c = *s is
therefore trying to access memory which simply does not exist at all.

Is there a reason an implementation cannot work this way?
Nov 14 '05 #9
pete wrote:
CBFalconer wrote:
Richard Bos wrote:
.... snip ...
For normal types, yes. However, all possible unsigned char
representations are required to be valid; there are no trap
values in unsigned char.


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.


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.


As I said, and you snipped, think of a parity bit. Use of such
would give a 50% chance of a bad parity interrupt on reading any
byte in any manner in any language (including C) before
initialization. There is no reason the hardware should not have
the equivalent to detect reads from uninitialized storage. Such
hardware (parity) exists, and is quite common, although now
superseded by ECC in general. The standards were never intended
to preclude normal hardware.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02
Nov 14 '05 #10
Kelsey Bjarnason <ke*****@xxnospamyy.lightspeed.bc.ca> writes:
[snips]

On Wed, 04 Aug 2004 15:57:12 +0000, pete wrote:
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?


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11
CBFalconer wrote:

pete wrote:
CBFalconer wrote:
Richard Bos wrote:
... snip ...
For normal types, yes. However, all possible unsigned char
representations are required to be valid; there are no trap
values in unsigned char.

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.


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.


As I said, and you snipped, think of a parity bit.


I know unsigned char as having only value bits.

--
pete
Nov 14 '05 #12
pete wrote:

CBFalconer wrote:

pete wrote:
CBFalconer wrote:
> Richard Bos wrote:
>>

... snip ...
>>
>> For normal types, yes. However, all possible unsigned char
>> representations are required to be valid; there are no trap
>> values in unsigned char.
>
> 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.

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.


As I said, and you snipped, think of a parity bit.


I know unsigned char as having only value bits.


A parity bit is an extra bit whose value is calculated from all
the other bits on write. It may describe odd or even parity. On
read, the parity is again calculated, and if it doesn't match a
hardware error has been detected. The result should be a system
interrupt.

Todays systems, when they have not been cheapened by eliminating
the feature, have replaced parity with ECC, which uses a set of
syndrome bits over a wider word to automatically correct one bit
errors, and detect multiple bit errors.

Since memory errors can be due to actual hardware failure, usually
repeatable, or external events (such as noise, or cosmic rays, or
alpha particles) that are not repeatable, it is foolish to
eliminate ECC checking on the memory system. Unfortunately the
majority of PCs sold today have done just that, and are inherently
untrustworthy.

All of this is transparent to the code using the memory. It
doesn't know it is there.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02
Nov 14 '05 #13
>Kelsey Bjarnason <ke*****@xxnospamyy.lightspeed.bc.ca> writes:
What prevents an implementation from simply not allocating space for [an object of type char and named] y until it has been initialized ...

In article <news:ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.org> writes: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.


Moreover, whether or not x (in Keith's code) or y is initialized,
two different pointers to the object must compare equal -- e.g.:

unsigned char *ptr = &x;
...
unsigned char *p2 = &x;
*ptr = 42;
unsigned char *p3 = &x; // assuming C99; or add a block
assert(ptr == &x);
assert(ptr == p2);
assert(ptr == p3);

I think it would be "challenging", to say the least, to make an
implementation work with lazy allocation.

Note also that it is legal to do things like:

struct with_holes x, y;
... /* set up all the fields in x */
memcpy(&y, &x, sizeof y);

where the (compiler-allocated) "holes" inside x are never initialized.
The memcpy() call accesses the uninitialized bytes within the object
x, copying their bitwise representations to the object y, even if
those bit patterns are "trap representations" when considered as
something other than "unsigned char"s.

Of course, there is no guaranteed way to produce holes -- bitfields
are perhaps the closest, using unnamed :0 fields to force alignment,
but even this operation is up to the implementation -- so if one
really wanted to work at a perverse implementation (for the
DeathStation 9000 perhaps), there might be some way to obey the
"letter of the law" of the Standard while violating the spirit. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #14
On Wed, 4 Aug 2004 01:20:12 +0200, "Henning"
<co***********@coldmail.com> wrote:
Hi
Problems translating C-code to Basic.
Is unsigned char x; by default setting x to 0?


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.
Nov 14 '05 #15
RoSsIaCrIiLoIA <n@esiste.ee> wrote in message news:<m9********************************@4ax.com>. ..
On Wed, 4 Aug 2004 01:20:12 +0200, "Henning"
<co***********@coldmail.com> wrote:
Problems translating C-code to Basic.
Is unsigned char x; by default setting x to 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 is *out* every function or procedure (global):
YES you have to set x=0
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). Note C has no "procedures".
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;
If it is inside a function and is marked "static" then no you don't
have to initialise it as the compiler automatically initialises it to
0 (zero). This
is only true the first time the function is called, any changes made
are permenent.

If it is inside a function and is not marked "static" then it is
automatic
variable and it is not initialised.
I would say if you see "unsigned char x;" set always x=0.


unless, that is, you want it initialised to something else...
--
Nick Keighley
Nov 14 '05 #16
On 11 Aug 2004 02:07:58 -0700, ni***********@marconi.com (Nick
Keighley) wrote:
RoSsIaCrIiLoIA <n@esiste.ee> wrote in message news:<m9********************************@4ax.com>. ..
On Wed, 4 Aug 2004 01:20:12 +0200, "Henning"
<co***********@coldmail.com> wrote:

******************************************
>Problems translating C-code to Basic. ^^^^^^^^^^^^^^^
****************************************** >Is unsigned char x; by default setting x to 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 is *out* every function or procedure (global):
YES you have to set x=0


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).


you speak about *translation* from C to Basic? Right? In this contest
if you want to translate from C to Basic this:
unsigned char x;

that is *out* functions and procedure then YOU
(or the Basic compiler) have to initialise the 'x translation' in
Basic to 0. Because like you say "the [C] compiler automatically
initialises it to 0 (zero)." That is what I have said (but perhaps it
was not so clear, excuse me)
I don't know where Basic compiler initialises them to zero.
Note C has no "procedures".

unsigned a;
void proced(void){++a;}

Does it seem a function?
Nov 14 '05 #17
RoSsIaCrIiLoIA wrote on 11/08/04 :
Note C has no "procedures".

unsigned a;
void proced(void){++a;}

Does it seem a function?


It *is* a function. You know, C is not Pascal.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: farzad | last post by:
Hi I try to install php_ming.so in my php . I am using Redhat 8.0 php 4.2.2 apache 2.0.4. I am doing as the howto install file want me to do. as follow....
7
by: Adams-Blake Co. | last post by:
This may be OT a bit, so maybe someone can point me in the right direction. I want to test a script that sends out an e-mail. I want to test it locally on my Mandrake Linux 8.2 box. It runs OK...
3
by: lawrence | last post by:
I haven't been able to reach www.php.net for days. Most of the rest of the web is working for me, though I've bad trouble reaching any English sites. Anyone else having trouble?
6
by: Michel | last post by:
Hi, I like to build a small simple dinamic website: Point-of-sale (detail-shop), So entering stock, logging all sales, print out invoices and download the logs to be imported into Excel. I...
3
by: nadia | last post by:
Is it possible to do the following in php: I want to have a main form open. In the form I want a button that will open a popup window so the user can search for something. The user can then select...
6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
5
by: Alper Adatoz | last post by:
Hi, i have a little problem. i hope u guys give me a clear solution (: db: mssql i just want to put jpeg file to the image field at the mssql db. and after that i want to call it back..
2
by: Sam Hou | last post by:
Dear all, I don't know where my problem is. Hopefully someone can give me some hint to explore the problem more. I am running Apache 2.0.48 wih PHP 4.3.4. The apache is running as nobody. I...
14
by: NotGiven | last post by:
I am guessing I would hold a variable of when it's opened, then in the script that runs when the page is offloaded, I coudl calcualte it. How do you store a time variable? How do you calculate...
4
by: Ryan Hubbard | last post by:
I would like to limit multiple logins for a user. How and when does the session id expire? Can I set it so after x minutes of inactivity it would expire. How do I check if session id exists? If...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.