#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:
you’ll see something like (in
qq.sin my case):You can see from that, it’s in its own section
rdata(read-only data), not in thetextsection.A possible disadvantage of placing it into
textwould 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.