question about const and text segment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cppsks

    #1

    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.


  • Victor Bazarov

    #2
    Re: question about const and text segment

    cppsks wrote:[color=blue]
    > 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?[/color]


    Did you declare it 'extern'?

    V

    Comment

    • Bob Hairgrove

      #3
      Re: question about const and text segment

      On Fri, 22 Oct 2004 12:06:04 -0500, "cppsks" <sksjava@hotmai l.com>
      wrote:
      [color=blue]
      >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.
      >[/color]

      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@Ho me.com

      Comment

      • cppsks

        #4
        Re: question about const and text segment

        "Bob Hairgrove" <invalid@bigfoo t.com> wrote in message
        news:vpiin0dcp5 5dlj2nl9b183hpj 53l8lklap@4ax.c om...[color=blue]
        > On Fri, 22 Oct 2004 12:06:04 -0500, "cppsks" <sksjava@hotmai l.com>
        > wrote:
        >[color=green]
        > >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[/color][/color]
        segment).[color=blue][color=green]
        > >
        > >Is this how const globals are supposed to work, or did the linker screw[/color][/color]
        up?[color=blue][color=green]
        > >
        > >Thanks.
        > >[/color]
        >
        > 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?
        >[/color]

        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.



        Comment

        • Victor Bazarov

          #5
          Re: question about const and text segment

          cppsks wrote:[color=blue]
          > "Bob Hairgrove" <invalid@bigfoo t.com> wrote in message
          > news:vpiin0dcp5 5dlj2nl9b183hpj 53l8lklap@4ax.c om...
          >[color=green]
          >>On Fri, 22 Oct 2004 12:06:04 -0500, "cppsks" <sksjava@hotmai l.com>
          >>wrote:
          >>
          >>[color=darkred]
          >>>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[/color][/color]
          >
          > segment).
          >[color=green][color=darkred]
          >>>Is this how const globals are supposed to work, or did the linker screw[/color][/color]
          >
          > up?
          >[color=green][color=darkred]
          >>>Thanks.
          >>>[/color]
          >>
          >>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?
          >>[/color]
          >
          >
          > 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?[/color]

          Every object declared 'const' has _internal_linka ge_ 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

          Comment

          Working...