Q40777: Using /ND to Put Uninitialized Data in a Named Segment
Article: Q40777
Product(s): See article
Version(s): 5.10 | 5.10
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 16-MAY-1989
Question:
When I compile the example below with the /NDsegname switch, why don't
the variables a and b end up in the segname_BSS segment?
#include <stdio.h>
int a,b,c=2;
main()
{
}
How do I get my uninitialized global data and static data to go into
the named data segment (namely segname_BSS)?
Response:
Variables that are declared outside of a function and not initialized
are communal. The BTDATA_BSS segment is for uninitialized static data.
So, when compiling with /ND:
int a; /* goes to FAR_BSS */
int b = 5; /* goes to STUFF */
static c; /* goes to STUFF_BSS */
The reason that "int a;" is treated in this way is that it's legal to
have such declarations in several modules as long as at most one of
the declarations contains an initializer. The linker combines all
these definitions into one. If it were subject to the /ND switch, the
variable could be in different segments in different modules, which
would be impossible to link.
So, to put the variable in the STUFF group, either declare the
variable static or initialize it (like either b or c above).
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
Copyright Microsoft Corporation 1986-2002.