placing constatnt data in code area 
Author Message
 placing constatnt data in code area

Hello, All

Can anybody say me, how can I place constatnt data in code area?
Is there standard way to do it?

when I try following code, I got successfull result
/******************************************/
const int x = 10;   /* variable x placed in code area */
int y;
int main( void )
{
        y = x;
        return 0;

Quote:
}

/******************************************/

but when i use "char*" instead "int", compiler places x in data area
/******************************************/
char* const x = (char* const)10;   /* variable x placed in DATA area
*/
char* y;
int main( void )
{
        y = (char*)x;
        return 0;

Quote:
}

/******************************************/

I understand that I can use following code to resolve this problem,
but may be exist another approach?
/******************************************/
#include <stdint>
const uintptr_t x = (uintptr_t)10;   /* variable x placed in CODE area
*/
uintptr_t y;
int main( void )
{
        y = x;
        return 0;

Quote:
}

/******************************************/

WBR
    Alexey



Sun, 14 Aug 2005 22:53:46 GMT  
 placing constatnt data in code area


Quote:
> Hello, All

> Can anybody say me, how can I place constatnt data in code area?
> Is there standard way to do it?

No. BTW, code area is a Harvard architure term typically, e.g. you have
the code address range and the data address range which typically share
addresses but not physical space. C doesn't know about this.

Quote:
> but when i use "char*" instead "int", compiler places x in data area
> /******************************************/
> char* const x = (char* const)10;   /* variable x placed in DATA area
> */

What happens if you say

const char *x = 10;

The * really looks better with the x, not char, e.g. if you do this it can
trick the eyes:

char*     ptr1, ptr2; /* only ptr1 is a char *, ptr2 is a simple char */



Sun, 14 Aug 2005 23:27:05 GMT  
 placing constatnt data in code area

Quote:

> Hello, All

> Can anybody say me, how can I place constatnt data in code area?
> Is there standard way to do it?

What do you mean by code area? I've never heard of it.

--
Freenet distribution not available
Everyone hates me because I'm paranoid.



Mon, 15 Aug 2005 05:38:58 GMT  
 placing constatnt data in code area

Quote:


> What happens if you say

> const char *x = 10;

> The * really looks better with the x, not char, e.g. if you do this it can
> trick the eyes:

> char*     ptr1, ptr2; /* only ptr1 is a char *, ptr2 is a simple char */

const char *x = 10;   /* this mean pointer(modifiable) to constant
data and it can't be placed in code area (i.e. code area not
modifiable) */

char* const x = 10;   /* this mean constant pointer to non-constant
string and this variavle my be placed in non-modifiable code area */

I try also following code, but unseccessfull :(
const char* const x = 10; /* const pointer to constant string */



Mon, 15 Aug 2005 15:06:04 GMT  
 placing constatnt data in code area


Quote:
>> What happens if you say

>> const char *x = 10;

>> The * really looks better with the x, not char, e.g. if you do this it
>> can trick the eyes:

>> char*     ptr1, ptr2; /* only ptr1 is a char *, ptr2 is a simple char
>> */

> const char *x = 10;   /* this mean pointer(modifiable) to constant
> data and it can't be placed in code area (i.e. code area not
> modifiable) */

> char* const x = 10;   /* this mean constant pointer to non-constant
> string and this variavle my be placed in non-modifiable code area */

> I try also following code, but unseccessfull :(
> const char* const x = 10; /* const pointer to constant string */

Is this for an 8051? If so, come over to comp.arch.embedded as it is not
topical here. We are talking about how a compiler toolchain locates a
variable which is not specified by the C language.

<OT>
For an 8051, I'd get the pointer into code space via:

const char * code ptr = 10;

Which say the 'ptr' var should be in the code memory space while the thing it
points to is the value 10. A wholly useless construct since you cannot change
ptr or 10. I boils down to simpley.

const char code var = 10;

where var is a simple char with the value ten located in the code space. What
are you actually trying to do? Are you just interested in playing with the
compiler/linker output or do you have a real application goal?
</OT>



Mon, 15 Aug 2005 22:01:11 GMT  
 placing constatnt data in code area

Quote:
>const char *x = 10;   /* this mean pointer(modifiable) to constant
>data and it can't be placed in code area (i.e. code area not
>modifiable) */

Nope, it means a diagnostic is required, due to a constraint violation.

Quote:
>char* const x = 10;   /* this mean constant pointer to non-constant
>string and this variavle my be placed in non-modifiable code area */

The same comment as above.

Quote:
>I try also following code, but unseccessfull :(
>const char* const x = 10; /* const pointer to constant string */

Ditto.  10 is an integer constant, not a string literal.  It is NOT a
valid initialiser for *any* kind of pointer.

You could get some better suggestions if you posted correct C examples.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Mon, 15 Aug 2005 23:58:53 GMT  
 placing constatnt data in code area

Quote:


>> Can anybody say me, how can I place constatnt data in code area?
>> Is there standard way to do it?

>No. BTW, code area is a Harvard architure term typically, e.g. you have
>the code address range and the data address range which typically share
>addresses but not physical space. C doesn't know about this.

Nope, von Neumann platforms also define the code (or text) segment and
the data segment of a program (this has nothing to do with the x86
segments).  The distinction is particularly relevant for embedded systems,
which store the text segment in ROM and the data segment in RAM.

Quote:
>> but when i use "char*" instead "int", compiler places x in data area
>> /******************************************/
>> char* const x = (char* const)10;   /* variable x placed in DATA area
>> */

>What happens if you say

>const char *x = 10;

You get a compiler diagnostic, obviously.  Other than that, this doesn't
define a constant pointer, but a pointer to constant data, which is a
very different beast, because the pointer itself is NOT constant.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 16 Aug 2005 00:07:17 GMT  
 placing constatnt data in code area

Quote:

>> Can anybody say me, how can I place constatnt data in code area?
>> Is there standard way to do it?

>What do you mean by code area? I've never heard of it.

Isn't it obvious?  The part of the program's memory that contains the
machine code and, typically, the program's constants.  Also called
code segment or text segment in linker lingo.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 16 Aug 2005 00:11:00 GMT  
 placing constatnt data in code area


Wed, 18 Jun 1902 08:00:00 GMT  
 placing constatnt data in code area

Quote:

> Can anybody say me, how can I place constant data in code area?
> Is there standard way to do it?

No.
The ANSI/ISO does NOT specify the location of variables or constants.

Quote:
> when I try following code, I got successful result
>    /******************************************/
>    const int x = 10;   /* variable x placed in code area */
>    int y;
>    int main(int argc, char* argv[]) {
>      y = x;
>      return 0;
>      }
>    /******************************************/

> but when i use "char*" instead "int", compiler places x in data area
>    /******************************************/
>    char* const x = (char* const)10;   /* variable x placed in DATA area */
>    char* y;
>    int main(int argc, char* argv[]) {
>      y = (char*)x;
>      return 0;
>      }
>    /******************************************/

> I understand that I can use following code to resolve this problem,
> but may be exist another approach?
>    /******************************************/
>    #include <stdint>
>    const uintptr_t x = (uintptr_t)10;   /* variable x placed in CODE area */
>    uintptr_t y;
>    int main(int argc, char* argv[]) {
>      y = x;
>      return 0;
>      }
>    /******************************************/

How do you know where you link editor places these constants?
Are you reading the assembler emitted by your compiler?
Most machines have "load immediate" instructions
which store small integral constants in the instructions themselves.
Sometimes, constants and/or "static" data
are stored in the ".text" or "code" segment
immediately before the code itself
even if the it is labeled ".data" by your compiler.
Constants and/or static data may be also be stored
just after the ".text" (code) segment
at the beginning of the free store (heap)
or at the bottom of the automatic storage (stack).
If your processor implements the so-called Harvard Architecture,
instruction are stored in a different memory space
from the data upon which they operate.
Note that this is different from architectures
which merely have separate instruction and data caches.


Tue, 16 Aug 2005 01:16:41 GMT  
 placing constatnt data in code area


Wed, 18 Jun 1902 08:00:00 GMT  
 placing constatnt data in code area

Quote:
>Can anybody say me, how can I place constatnt data in code area?
>Is there standard way to do it?

const is the only way offered by standard, but without any guarantees.
In other words, you can use it as a hint to the compiler, but the
compiler is free to ignore it.

Quote:
>when I try following code, I got successfull result
>/******************************************/
>const int x = 10;   /* variable x placed in code area */
>int y;
>int main( void )
>{
>    y = x;
>    return 0;
>}
>/******************************************/

>but when i use "char*" instead "int", compiler places x in data area

Bad luck.  You did all you could do, within the limits of standard C.

Quote:
>/******************************************/
>char* const x = (char* const)10;   /* variable x placed in DATA area */

No good reason for having it placed there, if the previous example worked
as expected.

Quote:
>char* y;
>int main( void )
>{
>    y = (char*)x;
>    return 0;
>}
>/******************************************/

>I understand that I can use following code to resolve this problem,
>but may be exist another approach?
>/******************************************/
>#include <stdint>

This is not a valid standard C header name.  The proper spelling is
<stdint.h> and it's a C99 feature.

Quote:
>const uintptr_t x = (uintptr_t)10;   /* variable x placed in CODE area */
>uintptr_t y;
>int main( void )
>{
>    y = x;
>    return 0;
>}

Even if it works, it is not the semantic equivalent of what you want:
(uintptr_t)10 and (uintptr_t)(char* const)10 need not yield the same
value.  But that's not a problem, since you can always use the latter as
initialiser.  

Also keep in mind that the result of (char* const)10 is
implementation-defined (although I can hardly imagine a good reason for
it not to work as expected on a platform with linear address space).

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 16 Aug 2005 02:01:56 GMT  
 placing constatnt data in code area


Wed, 18 Jun 1902 08:00:00 GMT  
 placing constatnt data in code area

Quote:
>How do you know where you link editor places these constants?

It's a compiler issue, not a linker one.  The compiler chooses in what
segment to place them.  One can easily discover its choice, by examining
the generated object file with adequate tools, e.g. the Unix nm command.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 16 Aug 2005 03:11:32 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Common areas/common data/data sharing

2. Placing a control in an edit control's NC area

3. Placing window inside view area

4. Placing window inside view area

5. How to place a dialogbox on the view's client area

6. creating BMP from data placed in TXT file.

7. Placing .data and .bss segments

8. Q: How to place static const strings in text segment instead of data

9. 3 parts of data area in C

10. displaying integer data types in the client area of a window

11. Possible bug placing breakpoints in C++ code

12. Looking for code in all the wrong places...

 

 
Powered by phpBB® Forum Software