Where are compound / string literals written in memory?

I read it;

A composite literal is a C99 function that can be used to create an array without a name. Consider an example:

int *p = (int []){3, 0, 3, 4, 1}; 

p points to the first element of an array of five elements containing 3, 0, 3, 4 and 1 .

Actually, I want to know if this array will be stored in memory or not, since it has no name?
In other words, in the case of

 char* str = "hello" 

Where will the string "hello" be stored in memory?

+7
c pointers
source share
4 answers

Using pointer arithmetic. So

 p[0], p[1], ... 

or

 *p, *(p + 1), ... 

Here is the thing. In C, you have good literals for primitive types such as int and char , and even string literals. So we can easily say things like

 int length(char *s); int len = length("Hello, World!"); 

In C99, the concept of complex literals was added to handle the "array literal" and the "string literal". So now we can say things like:

 int sum(int a[], int n); int total = sum((int []){ 17, 42 }, 2); 

It uses a composite literal to represent an "array literal".

Actually, I want to know whether this array will be stored in memory or not, since it has no name?

Yes, in the memory.

I think your confusion stems from this. p has a name. (int []){3, 0, 3, 4, 1} no. It so happened that the value p is the address (int []){3, 0, 3, 4, 1} . Of course, (int []){3, 0, 3, 4, 1} is in memory; it will be in the data segment for your executable file. You simply do not have a name to link to it.

+9
source share

Conceptually, how do you assign a string to a char pointer in C, in the same way you assign an array of integers p type int* :
When you declare: int *p = (int []){3, 0, 3, 4, 1}; , we can assume that it is stored in memory, for example:

  p 23 27 31 35 36 37 +----+ +----+----+----+----+----+----+ | 23 | | 3 | 0 | 3 | 4 | 1 | ? | +----+ +----+----+----+----+----+----+ β–² β–² β–² β–² β–² β–² | | | | | // garbage value p p+1 p+2 p+3 p+4 

Thus, basically an array allocates memory. And you can access the elements of the array as follows:

 p[0] == 3 p[1] == 0 p[2] == 3 p[3] == 4 p[4] == 1 

Note:

We do char* str = "hello"; So far, the type of string literals in C is char[N] not char* . But the point in most char[N] expressions can fall into char* .

Point :

When you declare an array, for example:

 int p[] = {3, 0, 3, 4, 1}; 

then here p type int[5] and &p pointer to an array = types: int(*)[5]

While in the declaration:

 int *p = (int []){3, 0, 3, 4, 1}; 

p pointer to the first element and type p is int* and &p type is int** . (this is like assigning a string to a char pointer).

In the first case, p[i] = 10; is legal for 0 <= i <= 4 , but in the second case, you write a read error on a read-only memory operation.

paragraph:

The following announcement also should not be:

 int *p = (int *){3, 0, 3, 4, 1}; 

Q Actually, I want to know whether this array will be stored in memory or not, since it has no name?

A massive array stored in memory, but its newly designated p ( p not the name of the array); there is no other name for it. Suppose if you do:

 int *p = (int []){3, 0, 3, 4, 1}; int i = 10; p = &i; 

Now you have lost the address of the array, it looks exactly like:

 char* s = "hello"; char c = 'A'; s = &c; 

and now you are losing the address "hello" .

The memory for the constant comes from the static segment when you declare. Any char string literal int array gets storage there. When your ad runs the address assigned to pointer variables. But the constant does not have a name, but a value. In both cases, the array and the string "hello" become part of the executable in the data section. (you can parse to find the values ​​there).

+4
source share

C 2011 (N1570) 6.5.2.5 5 says:

If a composite literal goes beyond the function body, the object has a static storage duration; otherwise, it has an automatic storage duration associated with the closing unit.

So the composite literal that you show has an automatic storage duration. The C standard does not specify where such objects reside in memory. C implementation for organizing memory. Typically, an object with automatic storage duration is created on the stack, but there are other methods by which the implementation can manage such objects.

In particular, suppose you write the value of p elsewhere and recursively call a procedure containing this compound literal. When the procedure initializes p again, there is a second instance of the composite literal. They are actually different objects, but standard C requires their addresses to be different. So, if you print two pointers, they will have different meanings. However, if the optimizer can determine that you are not doing this, and that two pointers to different instances of the composite literal are never compared, and there is no other observable behavior (as defined by the C standard) that can distinguish them, then the C implementation can use one actual an instance of a composite literal instead of creating a new one each time. In this case, the compiler can save the compound literal in the data section, and not on the stack.

Here is the code that shows that two instances of the same composite literal have different addresses:

 #include <math.h> #include <stdio.h> void foo(int *q) { int *p = (int []) { 2, 3 }; if (!q) foo(p); else printf("p = %p, q = %p.\n", (void *) p, (void *) q); } int main(void) { foo(0); return 0; } 

String literals are different. C 2011 (N1570) 6.4.5 6 says:

In phase 7 of the translation, a byte or null code is added to each multibyte character sequence that arises from a string literal or literals. 78) A multibyte character sequence is then used to initialize an array of static storage duration and enough length to contain the sequence.

Thus, a string literal denotes an object with a static storage duration. There is only one instance, even if the routine containing it is called recursively.

+3
source share

Two ways:

 &((int[]){3,0,3,4,1})[3] 

and

 ((int[]){3,0,3,4,1})+3 

Keep in mind that if a literal is inside a function, pointers to it are invalid when exiting the closing block.

0
source share

All Articles