static cons and static

A

andy

Hello

I am compiling some C code and I have an issue

If I declare

static const short a[2] = (-1, 3)

int main(.....)

short b;

b= a[0] I get -1
b= a[1] I get 3

If I declare w/o const

static short a[2] = (-1, 3)

It does not work any longer ...

Sound like the ARM libraries are not initializing these data (which compiles OK)

ANy idea?

Thanks
 
T

Thomas Stegen

andy said:
static const short a[2] = (-1, 3)

int main(.....)

short b;

b= a[0] I get -1
b= a[1] I get 3

If I declare w/o const

static short a[2] = (-1, 3)

It does not work any longer ...

Thanks

Can you please post a full program that demonstrates this behaviour?
From the clc perspective this should work.

(it might be linker issue, I have seen something similar happen on a
different platform because the linker was not properly set up)
 
E

Eric Sosman

andy said:
Hello

I am compiling some C code and I have an issue

If I declare

static const short a[2] = (-1, 3)

int main(.....)

short b;

b= a[0] I get -1
b= a[1] I get 3

If I declare w/o const

static short a[2] = (-1, 3)

It does not work any longer ...

Sound like the ARM libraries are not initializing these data (which compiles OK)

ANy idea?

Yes, but my "idea" pertains only to what you've shown
us and not to your actual code. What you've shown is *not*
your code -- that's easy to see, because it will not even
compile, much less run. I don't feel like wasting my time
trying to guess what your code *might* look like, nor yours
by suggesting fixes that won't do you any good because they
don't apply to your actual code.

Now, if you were to post the actual code rather than a
half-baked paraphrase, things might be different. Hint.
 
A

Andrey Tarasevich

andy said:
...
I am compiling some C code and I have an issue

If I declare

static const short a[2] = (-1, 3)

int main(.....)

short b;

b= a[0] I get -1
b= a[1] I get 3

No, you don't. The code above won't even compile. Post real code.
...
ANy idea?
...

Post real code. Your question in its current form makes no sense.
 
J

j

Thomas Stegen said:
andy said:
static const short a[2] = (-1, 3)

int main(.....)

short b;

b= a[0] I get -1
b= a[1] I get 3

If I declare w/o const

static short a[2] = (-1, 3)

It does not work any longer ...

Thanks

Can you please post a full program that demonstrates this behaviour?
From the clc perspective this should work.

Nope. He does not have an initializer list that would correspond
to his aggregate type.
 
M

Mark A. Odell

(e-mail address removed) (andy) wrote in

I am compiling some C code and I have an issue

If I declare

static const short a[2] = (-1, 3)

int main(.....)

short b;

b= a[0] I get -1
b= a[1] I get 3

If I declare w/o const

static short a[2] = (-1, 3)

It does not work any longer ...

<OT>
This is most likely a question for comp.arch.embedded where we are
accustomed to setting up the C run-time. You probably are not, thus he
initialized data section has not yet been copied into the run-time
location variables. Read your toolchain's documentation on crt0.s and
init.c and how to use them (assuming your ARM tools are gnu'ish). And post
complete code too! </OT>
 
T

Thomas Matthews

andy said:
Hello

I am compiling some C code and I have an issue

If I declare

static const short a[2] = (-1, 3)
Should that be:
static const short a[2] = {-1, 3};
Note that I am using curly braces {} instead
of parens().
int main(.....)

short b;

b= a[0] I get -1
b= a[1] I get 3

If I declare w/o const

static short a[2] = (-1, 3)

It does not work any longer ...

Sound like the ARM libraries are not initializing these data (which compiles OK)

ANy idea?

Thanks

My understanding of C and the ARM compiler is thus:
1. An array declared as static const will be
represented as a pointer to the data. The data
will reside in a read-only or constant section.

2. An array declared as const (sans static) will
be represented as an array on the "stack" and
it will be initialized by copying the data from
a read-only area to the area on the stack.

3. An array declared as static will be initialized
as any other globally initialized variable, by
the code that initializes the C run-time environment.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
W

Wilco Dijkstra

Thomas Matthews said:
andy wrote:
int main(.....)

short b;

b= a[0] I get -1
b= a[1] I get 3

If I declare w/o const

static short a[2] = (-1, 3)

It does not work any longer ...

What exactly doesn't work? Unless you're on purpose
bypassing the built-in startup code data will be initialized.
If you want to roll your own startup code then you have to get
it right of course, ie. do scatter loading, decompression
of RW data, clearing of ZI data and (often forgotten) C library
initialization.

Note the code generated for the const case might be surprising
as the compiler will do constant propagation on the array
accesses. This allows the linker to remove the const
array completely if there are no other references.

My understanding of C and the ARM compiler is thus: ....
2. An array declared as const (sans static) will
be represented as an array on the "stack" and
it will be initialized by copying the data from
a read-only area to the area on the stack.

Only if it is a local variable. Global variables without
static behave much in the same way as static variables,
except that they are visible in other compilation units.

Wilco
 
W

William McNicol

andy said:
Hello

I am compiling some C code and I have an issue

If I declare

static const short a[2] = (-1, 3)

int main(.....)

short b;

b= a[0] I get -1
b= a[1] I get 3

If I declare w/o const

static short a[2] = (-1, 3)

It does not work any longer ...

Sound like the ARM libraries are not initializing these data (which compiles OK)

ANy idea?

Thanks
 
S

Simon Richard Clarkstone

andy said:
static const short a[2] = (-1, 3)

If everyone looks carefully, they will see that these are round
parentheses, not curly braces. This means that (-1, 3) is an
expression. Though it is a constant expression, the compiler must be
(rightly or wrongly, what does the Standard say?) very confused, as it
is not required to be able to evaluate this at compile-time, but there
is no time at runtime to evaluate this either (it isn't in a function).
I the varying behaviour is presumably due to the compiler being
confused in different ways depending on whether the const is present.
If you meant to type curly braces for an array initialiser:

static const short a[2] = {-1, 3};

then I don't know what is wrong.
 
T

Thomas Stegen

j said:
andy said:
static const short a[2] = (-1, 3)
static short a[2] = (-1, 3)
Can you please post a full program that demonstrates this behaviour?
From the clc perspective this should work.

Nope. He does not have an initializer list that would correspond
to his aggregate type.

True, I read those parenthesises as curly brackets...

Might just be a type turned copy and paste error though, and
then my comment stands on it's own feet. Perhaps...

Uncompilable fragments are a bad idea anyway. That is my excuse.
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top