2

This code outputs a random memory address can anyone explain why and how ?

#include<iostream>
using std::cout;

int main()
{
    cout<<&"hello";
    return 0;
}

output:

0x560d6984e048

...Program finished with exit code 0
Press ENTER to exit console.
7

3 Answers 3

3

A literal strings in C++ are really arrays of constant characters (including the null-terminator).

By using the pointer-to operator you get a pointer to that array.

It's somewhat equivalent to something like this:

#include <iostream>

char const hello_str[] = "hello";

int main()
{
    std::cout << &hello_str;
}
Sign up to request clarification or add additional context in comments.

7 Comments

so "hello" is stored somewhere in memory ?
@JohnMellow Yes, otherwise it would not be able to get its individual characters.
@JohnMellow of course, it has to be if you want to use it in your program. Now, because it is a compile time constant, e.g., in Unix ELF executables, they are stored in the .rodata (read-only data) section - but they still must be mapped to memory, just like the actual program instructions. Otherwise the computer can't use it, right?
@JohnMellow this is not a C++ detail and not part of the C++ standard. This is operating system dependent. You can read up on the Unix ELF format if you want, or the Windows PE format. If you want to go really deep, you need to learn about operating systems and compilers.
@JohnMellow Exactly where the string is stored (in the executable file or in memory) is OS-dependent , but the C++ specification guarantees that it's available to the program as a null-terminated array of constant characters.
|
3

In C++, a string literal has the type const char[N] where N is the length of the string plus one for the null terminator. That array is an lvalue so when you do &"some string", you are getting the address of the array that represents the string literal.

This does not work with other literals like integer literals because they are prvalues, and the address operator (&) requires an lvalue.

2 Comments

but where is that array ? or every string will be implicitly represented as an array ?
@JohnMellow Every string literal is represented as an array, but we don't know where they are stored. That's left up to the implementation to decide. It could be it doesn't even exist once the compiler is done optimizing the code. For instance, duplicate string literals are allowed to all reference the same array, instead of having a unique array per literal.
0

Like the other answers have already stated, in C and C++, a string is basically a pointer to an array of characters.

According to cppreference.com:

String literals have static storage duration, and thus exist in memory for the life of the program.

The C++ standard doesn't describe how exactly executables are supposed to look like, or where and how things get stored in memory. It, e.g., describes storage duration and behaviors etc., but it is a platform independent standard. So this is implementation specific.

So why does this still work and what address do you see? It depends on your compiler and platform, but generally, your compiler will create an executable of your program[^1], e.g., an ELF (Executable and Linkable Format) on Linux, or a PE (portable executable) on Windows.

In an ELF binary, literals are stored in the .rodata section (read-only data) and the machine instructions in the .text section. You can look at the binary the compiler spits out with certain compiler options, or online on Matt Godbolt's compiler explorer.

Let's look at the example you gave: https://gcc.godbolt.org/z/4s5Y66nY5

We see a label on top, .LC0 where your string is! It is part of the executable file. And this file has to be mapped into memory to be executed. Line 5 loads the address of that label into a register (%esi) before the call to the stream operation.

So what you see is the location of the string in the read-only section mapped to memory (more precisely, the address in the virtual address space of your process).

This as all rather Linux specific, because that is what I know best, but it is very similar on Windows and Mac.

Here is a nice (student??) paper that goes into more details about how GCC deals with string literals on Unix.

[1]: The compiler could generate other things, of course. An object file. Maybe you want to create a static library, or a dynamic library. But let's keep it simple and talk about executables.

5 Comments

String literals are lvalues.
:O Look at that, it actually is: en.cppreference.com/w/cpp/language/value_category
Mhm. OP's code is fully legal.
Taking the address is guaranteed to work.
I see, I have updated my question accordingly @StoryTeller-UnslanderMonica

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.