Use for questions where the storage duration of objects is relevant.
Questions tagged [storage-duration]
73 questions
25
votes
2 answers
Does a reference have a storage location?
Does a reference have a storage location or is it just an alias for another location? Does this differ by C++ revision or is it consistent with all versions of C++? And if a reference has a storage location, does it then just allow value semantics…

Tarick Welling
- 3,119
- 3
- 19
- 44
12
votes
2 answers
A question about returning local pointer variable in function
I know the variables in function are using stack space. When function exit, the space are freed. That's why we should declare the pointer variable as static in function. However, I found that the code below works well.
The gcc version is: gcc…

waltermitty
- 453
- 3
- 12
9
votes
1 answer
Does C++ default-initialization preserve prior zero-initialization?
If a C++ constructor for an object with static-storage duration does not initialize a member, is that required to preserve the prior zero-initialization, or does it leave the member with an indeterminate value?
My reading of the C++ spec is that it…

user1998586
- 762
- 7
- 13
9
votes
1 answer
storage duration of underlying character data with user-defined string literal
Quick setup: I want to pass strings around in my program as a pointer and a size. I have a String class and a user-defined literal for constructing literal Strings:
struct String { const char *ptr; size_t sz; };
inline constexpr String operator ""…

Always Confused
- 470
- 4
- 7
8
votes
3 answers
How can I initialize a flexible array in rodata and create a pointer to it?
In C, the code
char *c = "Hello world!";
stores Hello world!\0 in rodata and initializes c with a pointer to it.
How can I do this with something other than a string?
Specifically, I am trying to define my own string type
typedef struct {
size_t…

Gavin S. Yancey
- 1,216
- 1
- 13
- 34
6
votes
3 answers
how does a static variable not get reassigned when inside the function
I have a question regarding the answer in this question but I can not comment on it because I have less than 50 rep.
I was wondering in the answer foo() is being called multiple times and the static variable is being assigned the same number of…

J.Doe
- 73
- 6
5
votes
3 answers
How can static local variable shared along different translation unit?
How can static local variable shared along different translation unit?
I know that "static" specifies internal linkage.
In case of "static" member function having same address in different translation units, each translation unit has a copy of the…

YoonSeok OH
- 647
- 2
- 7
- 15
5
votes
2 answers
Meaning of "Minimum storage duration" of Google Cloud storage
I know that Google Cloud storage has 4 options of storage and each options has a different of "Minimum storage duration"
https://cloud.google.com/storage/docs/lifecycle?hl=vi
Standard Storage: None
Nearline Storage: 30 days
Coldline Storage: 90…

user2085644
- 69
- 1
- 6
4
votes
1 answer
How to avoid deferring the initialization of static storage duration variables?
I have classes with side effects in their constructors, and objects of these classes are global objects that have static storage duration. During the initialization these objects register their classes in a special map, and it is important for these…

Dmitry Kuzminov
- 6,180
- 6
- 18
- 40
4
votes
3 answers
Why is every value not initialized to 0?
#include
int main(void) {
int memo[1000];
for (int i = 0; i < 1000; i++) {
printf("%d\t", memo[i]);
}
return 0;
}
I thought everything should be initialized to 0 but it is not.
Is there any reason for this?
Thank…

Kang
- 49
- 4
3
votes
2 answers
What is the storage duration of a temporary object: automatic, thread, static, or dynamic?
What is the storage duration of a temporary object: automatic, thread, static, or dynamic?
I know that the lifetime of a temporary object ends at or before the full expression where it was created, unless it is bound to a reference, in which case…

Géry Ogam
- 6,336
- 4
- 38
- 67
3
votes
1 answer
Why are objects with automatic storage not zeroed when static objects are?
In C, objects with automatic storage have an indeterminate value if they are not initialized, but static objects does not. From the standard:
If an object that has automatic storage duration is not initialized explicitly, its value is…

klutt
- 30,332
- 17
- 55
- 95
3
votes
0 answers
How to ensure arguments point to objects in static storage duration only?
I would like to make sure that an argument given to a function points to (or references to) an object which has static storage duration class.
The solution needs to work with C++11 without compiler specific extensions.
The most similar question I…

user5534993
- 518
- 2
- 17
3
votes
3 answers
Initialize array size in C at File Scope
I'd like to initialize an array based on a calculation, but the compiler gives me an error when I try this (I'm using GCC version 6.3.0):
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
char…

dvanaria
- 6,593
- 22
- 62
- 82
3
votes
2 answers
Storage Duration and Member Initialization of a Struct in C++
#include
using namespace std;
struct A {
// Some Other Code
int x;
};
A a1;
int main(){
A a2;
cout << "a1.x = " << a1.x << endl;
cout << "a2.x = " << a2.x << endl;
return 0;
}
C++14 Standard (ISO/IEC…

J L
- 563
- 3
- 17