-1

I'm trying to understand how const char * foo = "hello"; works under the hood. I interpret it as a string is created, and then a pointer points to it in memory.

So, does this line of code:

"hello";

take any memory?

JobHunter69
  • 1,706
  • 5
  • 25
  • 49

4 Answers4

1

"hello";

This does not take any memory because any modern compiler would optimize this away since it literally does nothing. If it was something more like:

const char* foo = "hello";

As you stated in your question, foo would be a char* to a static string that is baked into the program executable. Therefore the only run-time memory is the pointer to that string location.

Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
  • When an executable is loaded, the static data may be mapped or loaded into memory, e.g. the pointer doesn't actually point at the hard disk. – M.M Nov 02 '16 at 21:35
1

Interesting question.

a string literal must take space in the static storage (the "data segment"), so yes, that string literal does take a place somewhere in the process memory.

but a good optimize will recognize that this literal is not reference nowhere, and will probably remove that string away. so I highly doubt that a program compiled with optimization on will retain that string.

David Haim
  • 25,446
  • 3
  • 44
  • 78
1

If you simply execute "hello"; as an expression, almost any compiler will (probably even with optimization disabled) optimize it away to nothing.

That doesn't necessarily relate well to your starting point though. Given:

const char * foo = "hello";

You have another issue to consider: is that at namespace scope (i.e., outside any function)? If it's at global scope, the compiler probably can't optimize it away, even if it's not used. The problem is pretty simple: if it's outside any function, then some other source file could contain something like extern char const *foo;, and by doing so get access to foo, even though it's not used in the current file. Since the compiler only looks at one source file at a time, it has no way of detecting such a situation.

Depending on how smart it is, the linker (which does look at all the object files at once) may be able to detect that foo is never used, and based on that it could optimize it away.

If this definition is inside a function, then the compiler can detect whether it's used based solely on the content of that function, so we're back to a situation where it can be optimized away much more dependably.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
-2

I suggest looking into this link: http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/

foo points to a location on the stack in your case. So, yes, it takes up memory.

Edit: Sorry misread your code. You aren't assigning "hello" to anything. So in your case, no.

a_caban
  • 58
  • 4
  • `foo` may live on a stack in typical implementations, but in those same implementations it almost certainly won't point anywhere in the stack. – GManNickG Nov 02 '16 at 21:24