1
void main()
{
    char *p = "hello";
}

What is the storage type of P and where points in memory(stack/data segment)? Where the string "helllo" stored?

1
  • For example, under gcc+linux environment, "hello" is in the .rodata segment. Commented Aug 24, 2011 at 8:26

4 Answers 4

5

p is a local variable and typically resides on the stack.

The string is stored wherever the compiler decides to store it. Typically it will be in neither stack nor heap but rather in a read only area of the data segment of the executable image.

Sign up to request clarification or add additional context in comments.

Comments

2

The string is stored in read-only memory. The pointer itself is stored on the stack of main.

1 Comment

Architectures that don't have read-only memory cannot have C compilers made for them? :-)
2

Unless your compiler documentation explicitly says that void main() is a legal signature, use int main(void) instead:

int main(void)
{
  char *p = "hello";
  return 0;
}

Exactly where the memory for p and the string "hello" are allocated will vary with the implementation. For both ELF and PE/COFF formats, the memory for p will be inside of the stack frame for main and the memory for "hello" will be in a read-only data segment (.rdata for PE/COFF, .rodata for ELF).

Comments

0

Your string sotred in the memory, and the pointer refer to the memory address wehere the string is stored. If you call this pointer it's return whit the memory address, and you can use this.

1 Comment

Doesn't answer the OP's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.