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

const keyword usage

I am writing software for an embedded application and here is the
question.

GCC would emit data declared like
const char text[] = "abc";
to .rodata (i.e. "read only data") section. I can put this section to
flash memory and that would be OK.

I have a structure with one member of it being
const char** array;
This 'array` holds a number of pointers like 'text` described above.
Suppose the structure name is 'str', than the typical declaration I
use is:

const char* arr[] = {text, text, text};
str s = {..., arr, ...};

Those objects ('arr' and 's') are declared global and GCC places them
in .data section;

But the rub is that sometimes I need to assign elements of 'arr' array
dynamically. I have the following declaration:

char* dyn_arr[5]; /*And the real values are assigned in the
program;*/
str dyn_s = {..., dyn_arr, ...};

When compiling this GCC says
'initialization from incompatible pointer type'

When assigning:
dyn_arr[0] = text;

GCC says: 'assignment discards qualifiers from pointer target type'.

Those things with const are to avoid copying read-only data from flash
memory where whole program is stored to RAM as it is done with .data
section which contains "rw" variables;

I see the following solutions:
1) ignore GCC warnings (I wouldn't like doing that)
2) remove 'const' keyword and place read-only data to separate section
which I would not copy to RAM

Your suggestions?

Thanks,
Pavel
Nov 14 '05 #1
2 2477

"Pavel" <pa******@mail.ru> wrote in message
news:36**************************@posting.google.c om...
I am writing software for an embedded application and here is the
question.
<snip>
But the rub is that sometimes I need to assign elements of 'arr' array
dynamically. I have the following declaration:

char* dyn_arr[5]; /*And the real values are assigned in the
program;*/
str dyn_s = {..., dyn_arr, ...};

When compiling this GCC says
'initialization from incompatible pointer type'
And it should. The 'const' keyword is saying to the compute "This won't
change" so it can (safely) be optimized and stuffed in (for instance) flash
or (more specifically) in a read-only output segment.

If you *do* assign values to it, do not use the const keyword. You'd be
lying to the compiler, which is *really* pathetic.
When assigning:
dyn_arr[0] = text;

GCC says: 'assignment discards qualifiers from pointer target type'.

Those things with const are to avoid copying read-only data from flash
memory where whole program is stored to RAM as it is done with .data
section which contains "rw" variables;

I see the following solutions:
1) ignore GCC warnings (I wouldn't like doing that)
I fully agree with that sentiment.
2) remove 'const' keyword and place read-only data to separate section
which I would not copy to RAM
Sounds OK.
Your suggestions?


Split up your texts into a *really* const char[] part and a non-const char[]
part. That way, you have the advantages of const where applicable and the
char array[] where neccesary. I do not know how your data is structured
internally, so I can't really judge if this is realistic, but it would be
the cleanest solution IMO.

HTH,

dandelion
Nov 14 '05 #2
On Thu, 23 Dec 2004 00:25:18 -0800, Pavel wrote:
I am writing software for an embedded application and here is the
question.

GCC would emit data declared like
const char text[] = "abc";
to .rodata (i.e. "read only data") section. I can put this section to
flash memory and that would be OK.

I have a structure with one member of it being
const char** array;
This 'array` holds a number of pointers like 'text` described above.
Suppose the structure name is 'str', than the typical declaration I
use is:

const char* arr[] = {text, text, text};
The error below is highlighting a possible design problem here. text is
read-only, an attempt to write to it is an error. The type of arr reflects
this, you can't write to text through arr without doing something very
deliberate like a cast.
str s = {..., arr, ...};
OK, arr is an initialiser for the .array member of type const char **.
Similarly this is OK and s.array can't be used to write to the read-only
data without some skullduggery.
Those objects ('arr' and 's') are declared global and GCC places them
in .data section;
IOW they are writable, which is fine because those objects
themselves aren't const.
But the rub is that sometimes I need to assign elements of 'arr' array
dynamically. I have the following declaration:

char* dyn_arr[5]; /*And the real values are assigned in the
program;*/
str dyn_s = {..., dyn_arr, ...};
You can view this as trying to make dyn_array part of a datastructre that
can't write to the underlying strings, but you can do so through dyn_arr
itself which is inconsistent. There would also be "holes" in the const
system if the language didn't enforce the rules that apply to this case.
When compiling this GCC says
'initialization from incompatible pointer type'

When assigning:
dyn_arr[0] = text;

GCC says: 'assignment discards qualifiers from pointer target type'.

Those things with const are to avoid copying read-only data from flash
memory where whole program is stored to RAM as it is done with .data
section which contains "rw" variables;

I see the following solutions:
1) ignore GCC warnings (I wouldn't like doing that)
You could use a cast here. In fact that may not silence the warnings but
at least it avoids a constraint violation. Casts should be avoided where
possible, but are there to be used when you can show that no problem
results and there isn't a better way. There may still be a better way here.
2) remove 'const' keyword and place read-only data to separate section
which I would not copy to RAM


You don't need to do that. I'd use a cast if that was the only other
option.

Consider defining dyn_arr as

const char *dyn_arr[5];

That is consistent with the rest of your datastructure and resolves the
type conflicts. This means that you can't use dyn_arr to modify the
strings its elements point to. That's a good thing if they point to
things like text that are read-only. You can still write to dyn_arr itself
i.e. make its elements point to different strings.

The question is really how you manage the strings. If you can set up a
string and then set dyn_arr[x] to point to it then no problem:

const char *dyn_arr[5];
char mystr[10];
strcpy(mystr, "A string");
dyn_arr[1] = mystr;
is fine. What you can't do directly is use dyn_arr[1] to modify mystr. You
can of course create a new string in a different object and change dyn_arr
to point to that. And you can still modify mystr directly or through a
different datastructure that doesn't reference it as const, and then
access that modified string through dyn_arr[1].

Lawrence
Nov 14 '05 #3

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

Similar topics

3
by: Jan13 | last post by:
Hi, I'm new to programming in C++ (using VC6) and ran into the following problem: I want to declare and define a class member variable as 'static const', but something seems to go wrong with...
12
by: Ioannis Vranos | last post by:
Just some thought on it, wanting to see any explanations. It was advised in this newsgroups that we should avoid the use of keyword register. However it is a language feature, and if it...
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
3
by: LuB | last post by:
I'm writing a Win32 application - and more specifically, doing event programming. I want the application to be const compliant but I'm faced with a bit of a conundrum. Physically, many of my...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
17
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
29
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment:...
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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...
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.