question about const and text segment

C

cppsks

I have been working on making a constant array available for the clients.
However, it is placing it in the text segment, and external references
result in unresolved references to the array. Taking the const off the
definition solves the problem (the variable is placed in the data segment).

Is this how const globals are supposed to work, or did the linker screw up?

Thanks.
 
V

Victor Bazarov

cppsks said:
I have been working on making a constant array available for the clients.
However, it is placing it in the text segment, and external references
result in unresolved references to the array. Taking the const off the
definition solves the problem (the variable is placed in the data segment).

Is this how const globals are supposed to work, or did the linker screw up?


Did you declare it 'extern'?

V
 
B

Bob Hairgrove

I have been working on making a constant array available for the clients.
However, it is placing it in the text segment, and external references
result in unresolved references to the array. Taking the const off the
definition solves the problem (the variable is placed in the data segment).

Is this how const globals are supposed to work, or did the linker screw up?

Thanks.

C++ has no meaning for "segments" which are operating-system
dependent.

Did you declare the array with the "extern" keyword and export the
symbol from your executable?
 
C

cppsks

Bob Hairgrove said:
C++ has no meaning for "segments" which are operating-system
dependent.

Did you declare the array with the "extern" keyword and export the
symbol from your executable?

No, the array was declared in the header file as const (originally).
However, there were references to that array in the code and I guess if the
array is in the text segment, you can't refer to that address? I don't
know - that's why I am asking about this on this group. Thanks.

Other than the array being "const" (only readable), what other
"side-effects" are possible when it's const? Does it automatically go in the
text segment? Will there not a symbol generated for it?

Thanks.
 
V

Victor Bazarov

cppsks said:
No, the array was declared in the header file as const (originally).
However, there were references to that array in the code and I guess if the
array is in the text segment, you can't refer to that address? I don't
know - that's why I am asking about this on this group. Thanks.

Other than the array being "const" (only readable), what other
"side-effects" are possible when it's const? Does it automatically go in the
text segment? Will there not a symbol generated for it?

Every object declared 'const' has _internal_linkage_ by default. That
[usually] means that there is no way to get to it from another module.

What you have when you put your 'const' in a header is a bunch of those
consts all over the place. If you intend to have a const array of chars
shared between modules, you need to

(a) declare it in the header as such:

extern char const array[];

(b) define it in _one_ of the source files as such:

extern char const array[] = "contents of the shared array";

Then every module that includes the header with the declaration will gain
access to the array defined in one of your source modules.

V
 

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,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top