const c string in data section?

S

Steven Woody

hi,

i am using an IAR embedded c compiler. some times when i run out of
the overall data segment
capacity, i found i can reduce the data segment usage by simply
removing some kind of statements such as below,

printf( "hello, %d\n", 100 )

it seems tell me that "hello, %d\n" was allocated in data segment.
but for the compiler, read only data should be put in code segment
rather than data segment. so, i thing my question should be: is the
"hello, %d\n" really a kind of READ ONLY data?

thanks.

-
woody
 
W

Walter Roberson

i am using an IAR embedded c compiler. some times when i run out of
the overall data segment
capacity, i found i can reduce the data segment usage by simply
removing some kind of statements such as below,
printf( "hello, %d\n", 100 )
it seems tell me that "hello, %d\n" was allocated in data segment.
but for the compiler, read only data should be put in code segment
rather than data segment. so, i thing my question should be: is the
"hello, %d\n" really a kind of READ ONLY data?

String literals are allowed to be read-only, and to the extent
that "code vs data" can be meaningfully talked about within the
C standard, they are data rather than code (e.g., they are
objects not function pointers). But string literals are not -required-
to be read-only; your particular compiler might have a compiler
option to choose one behaviour or another.
 
E

Eric Sosman

Steven Woody wrote On 07/10/07 13:19,:
hi,

i am using an IAR embedded c compiler. some times when i run out of
the overall data segment
capacity, i found i can reduce the data segment usage by simply
removing some kind of statements such as below,

printf( "hello, %d\n", 100 )

it seems tell me that "hello, %d\n" was allocated in data segment.
but for the compiler, read only data should be put in code segment
rather than data segment. so, i thing my question should be: is the
"hello, %d\n" really a kind of READ ONLY data?

Because of a historical quirk, string constants
are read-only but not const. They are read-only in
the sense that attempting to write to one produces
undefined behavior, and non-const in the sense that
it is permitted to point at them with pointers that
aren't const-qualified.

How to get *your* strings into read-only memory?
Your first step should be to consult your compiler's
documentation; it may have a command-line flag or a
configuration option or a #pragma or something else
that causes it to put the strings where you want. If
that doesn't pan out, you might try the following
(rather clumsy) transformation:

static const char format[] = "hello, %d\n";
printf (format, 100);

This is not guaranteed to do what you want -- and it
certainly makes the code harder to read -- but if
all else fails it may be worth a try. There used to
be a program called xstr that would do a transformation
much like this one, but IIRC it botched some "unusual"
usages.
 
C

CBFalconer

Steven said:
i am using an IAR embedded c compiler. some times when i run out
of the overall data segment capacity, i found i can reduce the
data segment usage by simply removing some kind of statements such
as below,

printf( "hello, %d\n", 100 )

it seems tell me that "hello, %d\n" was allocated in data segment.
but for the compiler, read only data should be put in code segment
rather than data segment. so, i thing my question should be: is
the "hello, %d\n" really a kind of READ ONLY data?

Yes, but the compiler is mad. There is no reason the string cannot
be kept in the "data seqment", whatever that may be.
 
C

Christopher Benson-Manica

Eric Sosman said:
There used to
be a program called xstr that would do a transformation
much like this one, but IIRC it botched some "unusual"
usages.

<ot>It seems to be alive and well on this public NetBSD machine, so
perhaps it would be more appropriate to speak of the application in
the present tense.</ot>
 
E

Eric Sosman

Christopher said:
<ot>It seems to be alive and well on this public NetBSD machine, so
perhaps it would be more appropriate to speak of the application in
the present tense.</ot>

<ot>It is also present on my Solaris system, so I was
able to check -- and to verify -- its unfortunate behavior
in unusual circumstances. The past tense was a fond hope
("fond" as in Milton, Sonnet XIX) that somewhere a repaired
version may be found.)</ot>
 
S

Steven Woody

<ot>It is also present on my Solaris system, so I was
able to check -- and to verify -- its unfortunate behavior
in unusual circumstances. The past tense was a fond hope
("fond" as in Milton, Sonnet XIX) that somewhere a repaired
version may be found.)</ot>

thanks for all your kindly inputs :)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top