5

In C, We can use char * to point at a string. Just like

char *s = "Hello";

.

As it be seen, Neither the variable is located dynamically on heap because there is no any dynamical functions like malloc, nor it is defined to point a certain other variable.

So my question is, Where is the literal string which variable [char *s] points to stored logically?

Is it stored in stack like any normal local variables? or, something like stack?


Actually, I am a graduate of Computer engineering department, but I haven't found and have been too much curious about how [char * string] works logically. It is a really great honor to ask right this one now.

  • 1
    It is stored in text segment – Raghu Srikanth Reddy Aug 03 '15 at 06:40
  • 1
    @RaghuSrikanthReddy Not necessarily. Many platforms put string literals in the `rodata` section. – fuz Aug 03 '15 at 06:40
  • 2
    possible duplicate of [where in memory are string literals ? stack / heap?](http://stackoverflow.com/questions/4970823/where-in-memory-are-string-literals-stack-heap) – juanchopanza Aug 03 '15 at 06:44
  • @MaggiePhillips You really need to clarify your question body. Where `s` is stored and where what it *points to* is stored can be radically different, and you're likely to get a smattering of answers to both, most of which will be speculative regarding `s`, as you've not shown the context in which it exists. (global, local, static-local??). – WhozCraig Aug 03 '15 at 06:50
  • @WhozCraig You're right, I certainly need to divide the variable s and the string position. What I am really curious about is the position of the literal string "Hello". Thx. – KoreanXcodeWorker Aug 03 '15 at 06:58
  • What is "logically" supposed to mean? – OrangeDog Aug 04 '15 at 08:24

3 Answers3

13

The variable char* s is stored on the stack, assuming it's declared in a function body. If it is declared in a class, then it is stored wherever the object for the class is stored. If it is declared as a global, then it is stored in global memory.

In fact, any non-static and non-thread_local variable you declare in these three positions behave the same way, regardless of whether it is a primitive (i.e. int), an object (i.e. vector<int>), or a pointer (i.e. const char*). If a variable is static, it is always stored in global space. If a variable is thread_local, each thread gets its own copy, and that copy will usually stored at the base of the stack for the corresponding thread.

The actual string "Hello", which s points to, is stored in a constant global space somewhere, usually the .data segment.

IanPudney
  • 5,941
  • 1
  • 24
  • 39
  • Where `s` is stored depends on where it's defined. It's usually stored on the stack if it's a local (inside a function or block), but not if it's global variable. Besides, it doesn't *have* to be stored on the stack, the C specification doesn't actually specify where local variables should be stored, it only specifies the variables scope and lifetime. – Some programmer dude Aug 03 '15 at 06:47
  • Well, I am now a little bit to remember the basics on 'Operating System' study in my college. I guess I have ever scribbled the "text" segment once, then I seem to check that book again. Thx. – KoreanXcodeWorker Aug 03 '15 at 07:07
  • 1
    This answer is very implementation-specific. Some implentations don't have text segments, stacks, and so on. – M.M Aug 03 '15 at 07:42
  • Notice that many platforms have a special `rodata` segment for read-only global variables. It differs from `text` in that the content of `rodata` cannot be executed on machines that support marking some memory areaas as non-executable. – fuz Sep 24 '15 at 11:43
3

String literals have static storage duration. That means they exist for the whole lifetime of your program. They may be stored in a non-writable area, and they may overlap with other string literals. Two different instances of the same literal may or may not coincide.

It is up to your implementation (compiler/linker/etc). to make a decision that complies with those requirements.

M.M
  • 138,810
  • 21
  • 208
  • 365
2

There is nothing special about s, it's a pointer, it points somewhere. It has automatic storage duration just like any other local variable not declared static. What's “special” though is the string literal you are pointing to.

You can think of a string literal like "foo" as an unnamed global variable with some special constraints. These constraints are:

  • You cannot write to the string literal, as if it was declared as const
  • Two string literals might share the same address or overlap
fuz
  • 88,405
  • 25
  • 200
  • 352