473,388 Members | 1,207 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,388 software developers and data experts.

static cons and static

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
Nov 14 '05 #1
10 1841
andy wrote:

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)

--
Thomas.
Nov 14 '05 #2
andy wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #3
andy wrote:
...
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.

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #4
an********@excite.com (andy) wrote in
news:28**************************@posting.google.c om:
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>

--
- Mark ->
--
Nov 14 '05 #5
j

"Thomas Stegen" <th***********@gmail.com> wrote in message
news:2v*************@uni-berlin.de...
andy wrote:

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.

--
j
Nov 14 '05 #6
andy wrote:
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

Nov 14 '05 #7

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in message
news:tj*******************@newssvr17.news.prodigy. com...
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

Nov 14 '05 #8
andy wrote:
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

Nov 14 '05 #9
andy wrote:
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.
--
Simon Richard Clarkstone
s.************@durham.ac.uk / s***************@hotmail.com
Nov 14 '05 #10
j wrote:
"Thomas Stegen" <th***********@gmail.com> wrote in message
news:2v*************@uni-berlin.de...
andy wrote:
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.

--
Thomas.
Nov 14 '05 #11

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

Similar topics

13
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. ...
49
by: bearophileHUGS | last post by:
Adding Optional Static Typing to Python looks like a quite complex thing, but useful too: http://www.artima.com/weblogs/viewpost.jsp?thread=85551 I have just a couple of notes: Boo...
3
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
1
by: andrew | last post by:
Hi, I'm a C++ newbie, so apologies for this rather basic question. I've searched for help and, while I understand the problem (that the outer class is not yet defined), I don't understand what...
4
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when...
5
by: Sek | last post by:
hi folks, i have a bunch of strings used in my code in many places. these strings reside inside a instantiable class. so, i want to replace these with constant/static variable to control the...
3
by: wardemon | last post by:
Hi All, I have a aspx page named: ImageProcess.aspx that creates a thumbnail version of an image by passing the ImagePath, width, and height. The ImagePath is taken from a table from a database,...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
5
by: DamienS | last post by:
Hi, I have a static method in a class and I need to be able to return a reference to "this". Googling around, I found a heap of discussions of the pros/cons of "abstract static" etc. It was...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.