question about const and text segment



 DEVELOP > c-Plus-Plus > question about const and text segment

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "cppsks"
Date: 22 Oct 2004 12:06:04 PM
Object: question about const and text segment
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.
.

User: "Bob Hairgrove"

Title: Re: question about const and text segment 22 Oct 2004 01:05:03 PM
On Fri, 22 Oct 2004 12:06:04 -0500, "cppsks" <sksjava@hotmail.com>
wrote:

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?
--
Bob Hairgrove
NoSpamPlease@Home.com
.
User: "cppsks"

Title: Re: question about const and text segment 22 Oct 2004 01:23:59 PM
"Bob Hairgrove" <invalid@bigfoot.com> wrote in message
news:vpiin0dcp55dlj2nl9b183hpj53l8lklap@4ax.com...

On Fri, 22 Oct 2004 12:06:04 -0500, "cppsks" <sksjava@hotmail.com>
wrote:

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?

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.
.
User: "Victor Bazarov"

Title: Re: question about const and text segment 22 Oct 2004 01:40:33 PM
cppsks wrote:

"Bob Hairgrove" <invalid@bigfoot.com> wrote in message
news:vpiin0dcp55dlj2nl9b183hpj53l8lklap@4ax.com...

On Fri, 22 Oct 2004 12:06:04 -0500, "cppsks" <sksjava@hotmail.com>
wrote:


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?



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
.



User: "Victor Bazarov"

Title: Re: question about const and text segment 22 Oct 2004 12:56:22 PM
cppsks wrote:

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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER