简体   繁体   中英

c-program and segments

#include<stdio.h>
int main()
{
   char *p="mystring";
   return 0;
}

String literal "mystring", where will be stored( in which segment) ? I am assuming that address of "mystring" is stored in 'p','p' will be in data segment and "mystring" will be stored in code segment. If my assumption is write can i say 'p' is a far pointer ? Please correct me if i am wrong.

C itself has no concept of segments (nor far pointers), this will be a feature of the underlying implementation or architecture (which you haven't specified). Segmented architectures and near/far/tiny pointers are ancient things from the 8086 days - most code nowadays (with the possible exception of embedded stuff) gives you a flat memory model where you don't have to worry about that.

All the standard states is that the actual characters of the string will be characters that you are not allowed to modify.

For what it's worth (which isn't much). my implementation stores the string itself in memory marked read-only (this may or may not be a code segment, you can easily have other segments marked read-only) and p (the address of the first of those characters) is placed on the stack at runtime.

If you run your compiler to produce the assembler output:

gcc -S qq.c

you'll see something like (in qq.s in my case):

        .file   "qq.c"
        .def    ___main;        .scl    2;      .type   32;     .endef
        .section .rdata,"dr"
LC0:
        .ascii "mystring\0"
        .text
.globl _main
        .def    _main;  .scl    2;      .type   32;     .endef
_main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        addl    $15, %eax
        addl    $15, %eax
        shrl    $4, %eax
        sall    $4, %eax
        movl    %eax, -8(%ebp)
        movl    -8(%ebp), %eax
        call    __alloca
        call    ___main
        movl    $LC0, -4(%ebp)
        movl    $0, %eax
        leave
        ret

You can see from that, it's in its own section rdata (read-only data), not in the text section.

A possible disadvantage of placing it into text would be that things like DEP (data execute protection) would be much harder.

You want both code and read-only data to be read-only, but you also want code to be executable - you don't generally want read-only data to be executable.

The string will probably be stored in the text segment, where it will be read-only.

You can say 'p is a far pointer' if it pleases you to do so, but the term has no real significance any more. In the days of yore (when the mighty 80286 was the in thing in CPUs), then a 'far pointer' had some significance - and basically meant a pointer that did not fit in a single 16-bit address register. You needed an address segment register as well as the address register to deal with the incredible 1 MB of addressable space. These days, in most system (other than (some) embedded systems), that is no longer of relevance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM