Questions tagged [automatic-storage]

40 questions
51
votes
6 answers

Why are the terms "automatic" and "dynamic" preferred over the terms "stack" and "heap" in C++ memory management?

Related to a lot of questions and answers on SO, I've learned that it's better to refer to objects whose lifetime is managed as residing in automatic storage rather than the stack. Also, dynamically allocated objects shouldn't be referred to as…
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
44
votes
3 answers

std::vector (ab)uses automatic storage

Consider the following snippet: #include int main() { using huge_type = std::array; huge_type t; } Obviously it would crash on most of platforms, because the default stack size is usually less than 20MB. Now consider…
Igor R.
  • 14,716
  • 2
  • 49
  • 83
39
votes
5 answers

Why is the 'auto' keyword useful for compiler writers in C?

I'm currently reading "Expert C Programming - Deep C Secrets", and just came across this: The storage class specifier auto is never needed. It is mostly meaningful to a compiler-writer making an entry in a symbol table — it says "this storage is…
Chi_Iroh
  • 1,061
  • 5
  • 14
31
votes
3 answers

Is there any way to enforce that instances are only ever on the stack?

I have a C++ class for which I only ever want it to be instantiated on the stack. I am using an api to access content that was developed in another (interpreted) language which comes with its own garbage collection. The mechanisms in this language…
markt1964
  • 2,638
  • 2
  • 22
  • 54
19
votes
3 answers

Why is allocation on the heap faster than allocation on the stack?

As far as my knowledge on resource management goes, allocating something on the heap (operator new) should always be slower than allocating on the stack (automatic storage), because the stack is a LIFO-based structure, thus it requires minimal…
4
votes
2 answers

Automatic storage duration struct initialization

Some of this may be a duplicate, but I am sorry for that. Let's say I have this struct: struct foo { int a; int b; int c; }; 1. If struct foo type object is declared in the way that it has automatic storage duration and without…
4
votes
2 answers

Variable created inside loop changes value during iterations in C

I have code similar to the following in our product. According to me, the output is '0 1 2 3'. But the output of the similar code is '1 1 1 1'. for(i = 0 ;i < 5;i++){ int j; if(i) printf("%d ",j); j = i; } My understanding is…
4
votes
1 answer

C++/CLI reference not initialized to nullptr on subsequent entries into local block

I thought in C++/CLI declaring a local reference variable without an explicit initial value always initialized it to nullptr. I'm finding that this doesn't occur on the second and later entries into a local block. Here's the sample code. void…
JonN
  • 2,498
  • 5
  • 33
  • 49
3
votes
3 answers

structure on the stack - fields initialized?

Consider the following code: void func() { int p; ... if (p > MAX) { struct my_struct s; ... /* here we access the contents 's' as '&s' */ } } In this snippet s is on the stack. Is it guaranteed that the compiler…
Mark
  • 6,052
  • 8
  • 61
  • 129
3
votes
6 answers

Why is automatic object's destructor called twice?

(The answer to my question involves copy constructors, but the copy takes place upon return from a function, not within a method call to another class. I actually saw the referenced possible duplicate, but did not infer from the copy made by…
Stevens Miller
  • 1,387
  • 8
  • 25
2
votes
3 answers

stack space for a vector that its size is given at runtime? (C code)

Supose this C code: int main(){ int n; scanf("%d\n", &n); int a[n]; int i; for (i = 0; i
isma
  • 143
  • 1
  • 6
2
votes
2 answers

batch or vb script to read URL from excel file and download URL attached file to specified location or directory

I am looking for a VB script or batch file to read & Execute multiple URLs from an CSV file containing the URL Data in column B & The mentioned URL directly contains on the downloadable file which need to store at location or directory named on…
2
votes
1 answer

Batch file that runs upon accessing folder

I'm wondering if it's possible to have a batch file that automatically executes when I open a directory. I have no clue as to how this would be done. I want it to do this whether it is accessed through cmd/.exe/.bat/mouse
cmd
  • 563
  • 3
  • 10
  • 26
2
votes
8 answers

Do we really have to free() when we malloc()? What makes it different from an automatic variable then?

The OS will just recover it (after the program exits) right? So what's the use other than good programming style? Or is there something I'm misunderstanding? What makes it different from "automatic" allocation since both can be changed during run…
jantristanmilan
  • 4,188
  • 14
  • 53
  • 69
1
vote
1 answer

C API allowing for both automatic and allocated storage

I'm writing an API that has structs such as struct datast{ int a; int *items; size_t numitems; }; I'm providing functions which free the contents of such structs (in a similar fashion to what C++ destructors do). Constructors are not provided…
cesss
  • 852
  • 1
  • 6
  • 15
1
2 3