Question

Suppose I have the following:

char *a = "Learning CPP";
char *b = "Learning CPP";

Can I say that the total memory used was sizeof (a) and not 2*sizeof (stringliteral) ?

Cause my understanding of String literals is that one copy of the string is stored. But however,

Isn't it stored in between a, a+1, a+2 ..... a + 12 address in memory and also b, b + 1, b + 2 ... b + 12 in memory (12 is sizeof string)?

Was it helpful?

Solution

How can I find the address of a string literal ?

You have found the address of a string literal. The value of a string literal is the address of its first character. You assigned that value to a and b.

Can I say that the total memory used was sizeof (a) and not 2*sizeof (a) ?

First off the question is malformed. sizeof(a) is the size of the pointer.

You intended to ask:

Can I say that the total memory used for the string literals is the size required for one copy of the string, and not the size required for two copies of the string?

No. That's an implementation detail of the compiler. It may choose to intern the strings or not at its whim. If you modify one of the strings, it is implementation-defined whether the other string is observed to be modified.

This is a consequence of the more general fact that if you modify one of the strings, it is implementation-defined what happens, period. Anything can happen.

my understanding of string literals is that one copy of the string is stored.

Your understanding is mistaken. That is not a guarantee of the language. That's an optimization that a compiler can choose to make or not.

Isn't it stored in between a, a+1, a+2 ..... a + 12 address in memory and also b, b + 1, b + 2 ... b + 12 in memory (12 is sizeof string)?

I cannot understand this question. What is the "it" that isn't being stored?

OTHER TIPS

The following code will show you where the variables are and where the strings are

printf( "variable a is at address %p\n", &a );
printf( "variable b is at address %p\n", &b );
printf( "the string that a points to is at address %p\n", a );
printf( "the string that b points to is at address %p\n", b );

Note that the last two lines may or may not print the same address, depending on the compiler. There is no requirement that the compiler create only one string.

You can check it yourself. For example

if ( a == b ) std::cout << "The string literals are coincide" << std::endl;
else std::cout << "The string literals occupy different areas of memory" << std::endl;

Usually compilers have options that allow to control whether two identical string literals will occupy the same memory or each literal will be allocated in its own area of memory.

Can I say that the total memory used was sizeof (a) and not 2*sizeof (a) ? --> No, sizeof(a) is sizeof(char *) = 4

Isn't it stored in between a, a+1, a+2 ..... a + 12 address in memory and also b, b + 1, b + 2 ... b + 12 in memory (12 is sizeof string)? --> Using the gcc compiler, when the strings are equal, a and b are the same address. When the strings are different, I find different a and b are different addresses. (hey, this was a fun exercise..I learned something new)

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
char const* p ="Hello"; 
printf("%d",p); //prints the address of the first byte. ie of 'H' 
cout<<p[0] // prints H
cout<<p[1] //prints e
}

You can do this in C or C++ to determine the address of a string literal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top