473,406 Members | 2,336 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,406 software developers and data experts.

A problem initializing a static char array

Hi All,

Consider the following piece of code:

void func (void)
{
static unsigned char arr[3] = "\x00\xAA\xBB";

fprintf (stderr, "0x%x\n", arr[0]);
fprintf (stderr, "0x%x\n", arr[1]);
fprintf (stderr, "0x%x\n", arr[2]);
}

On a x86 machine, with the gcc (v3.3.5) compiler, the above piece of
code (on a OpenBSD 3.8 machine) gives the following result:
[...]
0x0
0xaa
0xbb
[...]

However, with the VxWorks 5.4 C compiler (which I believe is Wind
River's) variant of gcc, the above piece of code gives the following
result (run on a x86 machine running VxWorks 5.4):

[...]
0x0
0x0
0x0
[...]

If I not use the static qualifier initializing the array, then VxWorks
host too gives the same result as OpenBSD host. Can someone explain
the discrepancy and why the VxWorks compiler would be initializing
all the elements of the array to 0x00?

TIA
Jai
Jun 27 '08 #1
6 4345
Jai Prabhu <ja*******@softhome.netwrites:
Consider the following piece of code:

void func (void)
{
static unsigned char arr[3] = "\x00\xAA\xBB";

fprintf (stderr, "0x%x\n", arr[0]);
fprintf (stderr, "0x%x\n", arr[1]);
fprintf (stderr, "0x%x\n", arr[2]);
}

On a x86 machine, with the gcc (v3.3.5) compiler, the above piece of
code (on a OpenBSD 3.8 machine) gives the following result:
[...]
0x0
0xaa
0xbb
[...]

However, with the VxWorks 5.4 C compiler (which I believe is Wind
River's) variant of gcc, the above piece of code gives the following
result (run on a x86 machine running VxWorks 5.4):

[...]
0x0
0x0
0x0
[...]

If I not use the static qualifier initializing the array, then VxWorks
host too gives the same result as OpenBSD host. Can someone explain
the discrepancy and why the VxWorks compiler would be initializing
all the elements of the array to 0x00?
I can't think of any really plausible reason. It *looks* like a
compiler bug, but it's impossible to be certain of that without more
context.

Did you remember the required "#include <stdio.h>"?

As I recall, VxWorks is an embedded system, which means it's likely
that you're using a freestanding implementation. Conforming
freestanding implementations aren't required to provide most of the
standard library, including stdio; if they do provide portions of it,
it doesn't necesssarily behave as the standard describes. It's
unlikely that a variation in the behavior of fprintf would cause this
kind of problem, but it's hard to be sure.

You haven't shown us a complete program, or even a call to func(). I
can't think of anything outside the definition of func() that would
cause the symptom you're seeing (other than a malicious macro
definition for "static"), but it's hard to be sure.

If it's a system-specific issue, try asking in comp.os.vxworks.

[Hey, aioe.org is back!]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #2
"Jai Prabhu" <ja*******@softhome.netwrote in message
news:g0**********@aioe.org...
If I not use the static qualifier initializing the array, then VxWorks
host too gives the same result as OpenBSD host. Can someone explain
the discrepancy and why the VxWorks compiler would be initializing
all the elements of the array to 0x00?
VxWorks is an embedded OS and embedded tool chains often try to do
optimizations to strings, usually keyed on seeing "static" and/or a quoted
string.

So, you might also try:

static unsigned char arr[3] = { 0x00, 0xAA, 0xBB };
and/or
static unsigned char arr[3] = "\xAA\x00\xBB";

to see if perhaps you're running afoul of one of these optimizations.

- Bill

Jun 27 '08 #3
Jai Prabhu wrote:
Hi All,

Consider the following piece of code:

void func (void)
{
static unsigned char arr[3] = "\x00\xAA\xBB";

fprintf (stderr, "0x%x\n", arr[0]);
fprintf (stderr, "0x%x\n", arr[1]);
fprintf (stderr, "0x%x\n", arr[2]);
}

On a x86 machine, with the gcc (v3.3.5) compiler, the above piece of
code (on a OpenBSD 3.8 machine) gives the following result:
[...]
0x0
0xaa
0xbb
[...]

However, with the VxWorks 5.4 C compiler (which I believe is Wind
River's) variant of gcc, the above piece of code gives the following
result (run on a x86 machine running VxWorks 5.4):

[...]
0x0
0x0
0x0
[...]

If I not use the static qualifier initializing the array, then VxWorks
host too gives the same result as OpenBSD host. Can someone explain
the discrepancy and why the VxWorks compiler would be initializing
all the elements of the array to 0x00?
It looks like a compiler bug, possibly stemming from
the compiler incorrectly treating the initializer as a
string and getting confused by the leading '\0'. As an
experiment, you might want to see what happens with

static unsigned char arr[3] = "\x42\xAA\xBB";

or with

static unsigned char arr[3] = "\x42\x00\xBB";

--
Er*********@sun.com
Jun 27 '08 #4
Jai Prabhu wrote:
Hi All,

Consider the following piece of code:

void func (void)
{
static unsigned char arr[3] = "\x00\xAA\xBB";
Note that "\xAA" and '\xAA' need not yield 0xAA on
non vanilla 2c implementations.
fprintf (stderr, "0x%x\n", arr[0]);
fprintf (stderr, "0x%x\n", arr[1]);
fprintf (stderr, "0x%x\n", arr[2]);
BTW, "0x%x" can be simplified to "%#x"

It wouldn't hurt to force the unsigned char's to unsigned int,
as required by %x.
}
<snip>
... with the VxWorks 5.4 C compiler (which I believe is Wind
River's) variant of gcc, the above piece of code gives the
following result (run on a x86 machine running VxWorks 5.4):

[...]
0x0
0x0
0x0
[...]

If I not use the static qualifier initializing the array, then VxWorks
host too gives the same result as OpenBSD host. Can someone
explain the discrepancy and why the VxWorks compiler would
be initializing all the elements of the array to 0x00?
You should confirm that is actually the case, e.g. do a memcmp
with a zero byte initialised object. Otherwise, post a complete
compilable source that exhibits the problem.

--
Peter
Jun 27 '08 #5
Peter Nilsson wrote:
Jai Prabhu wrote:
>Hi All,

Consider the following piece of code:

void func (void)
{
static unsigned char arr[3] = "\x00\xAA\xBB";

Note that "\xAA" and '\xAA' need not yield 0xAA on
non vanilla 2c implementations.
>fprintf (stderr, "0x%x\n", arr[0]);
fprintf (stderr, "0x%x\n", arr[1]);
fprintf (stderr, "0x%x\n", arr[2]);

BTW, "0x%x" can be simplified to "%#x"

It wouldn't hurt to force the unsigned char's to unsigned int,
as required by %x.
Nor it would bring anything, no?
Aren't unsigned chars integer-promoted to unsigned int?
Peter

--
Pietro Cerutti
Jun 27 '08 #6
On Tue, 20 May 2008 00:07:24 +0200, Pietro Cerutti <"gahr<S P A M>gahr
wrote:
Peter Nilsson wrote:
>Jai Prabhu wrote:
>>fprintf (stderr, "0x%x\n", arr[0]);
fprintf (stderr, "0x%x\n", arr[1]);
fprintf (stderr, "0x%x\n", arr[2]);

BTW, "0x%x" can be simplified to "%#x"

It wouldn't hurt to force the unsigned char's to unsigned int, as
required by %x.

Nor it would bring anything, no?
Aren't unsigned chars integer-promoted to unsigned int?
They are usually promoted to signed int. They are only promoted to
unsigned int when UCHAR_MAX INT_MAX (when a conversion to signed int
might change the value), which almost certainly is not the case on your
system.
Jun 27 '08 #7

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

Similar topics

1
by: Steven T. Hatton | last post by:
Is there a way to initialize an member array of user defined objects that is declared static const? For example: const vec3f I(1,0,0); const vec3f j(0,1,0); class corner_functor {
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
4
by: ccdrbrg | last post by:
I am trying to initialize an arrary of pointers to structs with constants. Sample code: struct mystruct { char *text; int number; };
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
5
by: amparikh | last post by:
I have some test code which demonstrates the problem. I know I could solve this by just returning a pointer, but I better use a reference. In real code, what I actually want to return is a...
7
by: nk | last post by:
Hi, I'm a newbie on this language. I would be very happy if you help me about the following issue: The code below, reads some names(strings), stores them, and stores the addresses in the pointer...
4
by: John | last post by:
I can do this double T = { {1,3,4},{7,8,9}}; but not this double T; T = { { a,b,c} , {d,e,f} }
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
3
by: theBNich | last post by:
Hi, I currently have an enumeration in class Lexer: enum lexType { /* token keywords */ lexIF, lexTHEN, lexWHILE, lexDO, lexBEGIN, lexEND, lexELSE, lexPROGRAM, ... /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.