Board index » cppbuilder » size of a const at compile time

size of a const at compile time


2006-03-28 02:18:16 AM
cppbuilder61
Hello,
At the moment (well for many years) I have following code:
#define MAX_LEN_TITLE 20
const u_8* blank_title = (u_8*)" ";
Where blank_title is just a string to clear a title on a form (embedded
system). The annoying thing is of course when I have to do a maintenance
and for example a customer wants a title length of 22. Then I have to
change also the const declaration.
Not a big deal of course, but can this also automatically happen at
compile time ? For example:
const u_8 blank_title[MAX_LEN_TITLE + 1] ... euh but how to fill it with
blanks at compile time ?
of course another solution would be to make it a variable in the pool,
but I like to have it in EPROM as I have not much spare memory.
--
rgds, Wilfried
www.mestdagh.biz
 
 

Re:size of a const at compile time

"Wilfried Mestdagh" < XXXX@XXXXX.COM >wrote in message
Quote
Hello,

At the moment (well for many years) I have following code:

#define MAX_LEN_TITLE 20
const u_8* blank_title = (u_8*)" ";

Where blank_title is just a string to clear a title on a form
(embedded system). The annoying thing is of course when I have to do
a maintenance and for example a customer wants a title length of 22.
Then I have to change also the const declaration.

Why do you need blank_title at all? Wouldn't strcpy(form_title, "");
be easier?
--
Bruce
 

Re:size of a const at compile time

" Bruce Salzman" < XXXX@XXXXX.COM >writes:
Quote
Why do you need blank_title at all? Wouldn't strcpy(form_title, "");
be easier?
Or form_title[0] = 0;
 

{smallsort}

Re:size of a const at compile time

Wilfried Mestdagh < XXXX@XXXXX.COM >writes:
Quote
At the moment (well for many years) I have following code:

#define MAX_LEN_TITLE 20
Bad idea.
enum
{
MAX_LEN_TITLE = 20
};
makes MAX_LEN_TITLE a well-behaved name.
Quote
const u_8* blank_title = (u_8*)" ";
This is very strange.
" " is of type 'array of 21 char const'. Why do you
first cast it to 'pointer to u_8 (non-const)' (whatever u_8 is) and then
to 'pointer to u_8 const'?
What's wrong with
char const blank_title[] = " ";
Quote
Where blank_title is just a string to clear a title on a form
(embedded system). The annoying thing is of course when I have to do a
maintenance and for example a customer wants a title length of
22. Then I have to change also the const declaration.
Could you do things the other way round? E.g.:
char const longer_blank_title[] = " ";
enum
{
MAX_LEN_TITLE = sizeof longer_blank_title
/ sizeof longer_blank_title[0];
};
Quote
Not a big deal of course, but can this also automatically happen at
compile time?
No.
If MAX_LEN_TITLE has to be defined independently of blank_title, you
could use a BOOST_STATIC_ASSERT
(cf. www.boost.org/doc/html/boost_staticassert.html) to make
sure that blank_title has the correct length. This doesn't fill
blank_title, but the compiler will tell you when to adjust its
filling.
 

Re:size of a const at compile time

Hi Thomas,
Quote
What's wrong with
char const blank_title[] = " ";
Nothing wrong. Then I have to typecast it afterwards to not have warnings.
Quote
Could you do things the other way round? E.g.:
char const longer_blank_title[] = " ";
enum
{
MAX_LEN_TITLE = sizeof longer_blank_title
/ sizeof longer_blank_title[0];
};
Yes. This is a splendid idea. This way I can synchronize this if any
change is needed.
Quote
If MAX_LEN_TITLE has to be defined independently of blank_title, you
could use a BOOST_STATIC_ASSERT
(cf. www.boost.org/doc/html/boost_staticassert.html) to make
sure that blank_title has the correct length. This doesn't fill
blank_title, but the compiler will tell you when to adjust its
filling.
Thanks. I will check this out.
--
rgds, Wilfried
www.mestdagh.biz
 

Re:size of a const at compile time

Thomas Maeder [TeamB] wrote:
Quote
Or form_title[0] = 0;
I need to blank out characters written on a screen. If I should write a
null string to the screen at the given position it will write nothing
and the characters are still visible. This is not PC, it is embedded system.
--
rgds, Wilfried
www.mestdagh.biz
 

Re:size of a const at compile time

Wilfried Mestdagh wrote:
Quote
Thomas Maeder [TeamB] wrote:

>Or form_title[0] = 0;


I need to blank out characters written on a screen. If I should write a
null string to the screen at the given position it will write nothing
and the characters are still visible. This is not PC, it is embedded
system.
So what's wrong with:
memset(form_title, ' ', MAX_LEN_TITLE);
?
Tom
 

Re:size of a const at compile time

"Wilfried Mestdagh" < XXXX@XXXXX.COM >wrote in message
Quote
Hi Thomas,

>What's wrong with
>char const blank_title[] = " ";

Nothing wrong. Then I have to typecast it afterwards to not have warnings.

#define title_blank ((u_8*) blank_title)
Rufus
 

Re:size of a const at compile time

Wilfried Mestdagh < XXXX@XXXXX.COM >writes:
Quote
>Could you do things the other way round? E.g.:
>char const longer_blank_title[] = " ";
>enum
>{
>MAX_LEN_TITLE = sizeof longer_blank_title
>/ sizeof longer_blank_title[0];
>};

Yes. This is a splendid idea. This way I can synchronize this if any
change is needed.
Just make sure not to to repeat my off by one error.
enum
{
MAX_LEN_TITLE = (sizeof longer_blank_title
/ sizeof longer_blank_title[0])
- 1; // don't count the terminating 0 character
};
is probably better than the version quoted above.
 

Re:size of a const at compile time

Hi Tom,
Quote
So what's wrong with:
memset(form_title, ' ', MAX_LEN_TITLE);
Nothing wrong, but this is not a PC but an embedded system. This means
all these RTL functions are unusable. Also it cost unneeded CPU time
because it can be in eprom.
--
rgds, Wilfried
www.mestdagh.biz
 

Re:size of a const at compile time

Wilfried Mestdagh wrote:
Quote
Hi Tom,

>So what's wrong with:
>memset(form_title, ' ', MAX_LEN_TITLE);


Nothing wrong, but this is not a PC but an embedded system. This means
all these RTL functions are unusable. Also it cost unneeded CPU time
because it can be in eprom.
You haven't made it clear how this is used. You said: "Where blank_title
is just a string to clear a title on a form (embedded system)." Do you
have to copy the string to a fixed address, or can you just reassign the
title pointer? E.g. can you just do:
form_title = blank_title;
or do you need the equivalent of:
memcpy(form_title, blank_title, MAX_LEN_TITLE);
Incidentally, the embedded compiler I've used (GCC on QNX), has memset
as an intrinsic - no RTL required.
Tom
 

Re:size of a const at compile time

Wilfried Mestdagh wrote:
Quote
#define MAX_LEN_TITLE 20
const u_8* blank_title = (u_8*)" ";

const u_8 blank_title[MAX_LEN_TITLE + 1] ... euh but how to fill it with
blanks at compile time ?
I also wanted to do that when I converted ASM apps to C, but haven't
found a way to do it.
If you want to do it bad enough, you can do this in TASM.
.dataseg
public _title
public _blank_title
_title db 'This is the title',0
MAX_LEN_TITLE equ (LENGTH _title) -1
_blank_title db MAX_LEN_TITLE dup(' '),0
Or leave off the ,0 and -1 if you don't need zero-terminated strings.
 

Re:size of a const at compile time

Hi Bob,
Yes also good idea. thx.
--
rgds, Wilfried
www.mestdagh.biz
 

Re:size of a const at compile time

Hi Tom,
I want to thank you for your help, but we are not on my question
anymore. My mothers tongue is not English so my question was probably
not very clear, as also the names of the variables.
I just needed to have a constant in eprom filled with blanks and a
certain length wich are answered by Thomas and Bob.
As for the application, I need to clear a displays title which only can
be done to overwrite the screen locations with blanks. So there fore I
need the constant which has to have the length of the longest title.
--
rgds, Wilfried
www.mestdagh.biz