Questions tagged [static-memory-allocation]

In computer programming, a static variable is a variable that has been allocated statically so that its lifetime or "extent" extends across the entire run of the program. This is in contrast to the more ephemeral automatic variables (local variables are generally automatic), whose storage is allocated and deallocated on the call stack; and in contrast to objects whose storage is dynamically allocated in heap memory. Static memory allocation in general is the allocation of memory at compile time before the associated program is executed, unlike dynamic memory allocation or automatic memory allocation where memory is allocated as required at run time.

The absolute address addressing mode can only be used with static variables, because those are the only kinds of variables whose location is known by the compiler at compile time. When the program (executable or library) is loaded into memory, static variables are stored in the data segment of the program's address space (if initialized), or the BSS segment (if uninitialized), and are stored in corresponding sections of object files prior to loading.

The static keyword is used in C and related languages both for static variables and other concepts.

Source: Wikipedia

92 questions
117
votes
7 answers

Difference between static memory allocation and dynamic memory allocation

I would like to know what is the difference between static memory allocation and dynamic memory allocation? Could you explain this with any example?
108
votes
13 answers

What is the difference between Static and Dynamic arrays in C++?

I have to do an assignment for my class and it says not to use Static arrays, only Dynamic arrays. I've looked in the book and online, but I don't seem to understand. I thought Static was created at compile time and Dynamic at runtime, but I might…
user69514
  • 26,935
  • 59
  • 154
  • 188
70
votes
7 answers

Are 'new' and 'delete' getting deprecated in C++?

I stumbled upon a quiz that involved array declaration with different sizes. The first thing that came to my mind is that I would need to use dynamic allocation with the new command, like this: while(T--) { int N; cin >> N; int *array = new…
13
votes
3 answers

STL within embedded system with very limited memory

I'm currently in the process of building an embedded system, using an ARM Cortex M3 processor, with 64 KB of SRAM. At the moment, I'm looking for a way to ensure deterministic performance with STL containers, which includes ensuring that I cannot…
9
votes
2 answers

QT - Main Widget - Stack or Heap?

I am a little confused on whether I should prefer to initialize my main widgets on the stack or on the heap. In "C++ GUI Programming with QT 4," main widgets are initialized on the stack. Before I say more, I will explain what I mean: int main(int…
Serodis
  • 2,092
  • 4
  • 25
  • 34
7
votes
4 answers

Memory allocation for array on Stack or Heap (C)

Consider the following C code: #include #include int main() { int arrSize; scanf("%d", &arrSize); printf("%d\n",arrSize); int *dynArr = (int *)malloc(sizeof(int)*arrSize); …
6
votes
2 answers

Why do I need dynamic memory allocation if I can just create an array?

I was reading about dynamic memory allocation and static memory allocation and found the following about dynamic memory allocation: In the programs seen in previous chapters, all memory needs were determined before program execution by defining the…
Vasi Marin
  • 453
  • 1
  • 3
  • 9
5
votes
3 answers

Is this is an example of static memory allocation or dynamic memory allocation?

I researched a lot of static and dynamic memory allocation but still, there is a confusion that: int n, i, j; printf("Please enter the number of elements you want to enter:\t"); scanf("%d", &n); int a[n]; for (i = 0; i < n; i++) { printf("a[%d]…
5
votes
3 answers

Why shouldn't we have dynamic allocated memory with different size in embedded system

I have heard in embedded system, we should use some preallocated fixed-size memory chunks(like buddy memory system?). Could somebody give me a detailed explanation why? Thanks,
4
votes
2 answers

If a static variable is declared out side of a function, will the memory address be the same as if it's declared in a function

I was asked these 2 questions during an interview. I guess the address of a static variable will be the same no matter where it is declared. It will also have the same address from run to run. Correct me if I am wrong. If a static variable is…
kingnesay
  • 43
  • 3
4
votes
1 answer

memory allocation for array in c

#include int main(void) { int a=17; scanf("%d",&a); int arr[a]; printf("%lu", sizeof(arr)); } Memory for array "arr" should be allocated at compile time but in this case it takes the value of "a" from the user(run-time) and…
Raj
  • 51
  • 1
4
votes
2 answers

Static and Dynamic Memory Addresses in C

printf("address of literal: %p \n", "abc"); char alpha[] = "abcdef"; printf("address of alpha: %p \n", alpha); Above, literal is stored in static memory, alpha is stored in dynamic memory. I read in a book that some compilers show these two…
user4039871
4
votes
1 answer

What happens when there is not enough memory for a static allocation in C?

When you dynamically allocate memory e.g. malloc(1024 * sizeof(char)) the resulting pointer is set to NULL if there is not enough memory available to fulfill the request. What happens when there is not enough memory to satisfy a static allocation…
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
4
votes
5 answers

How to detect if memory is dynamic or static, from a callee perspective?

Note: when I say "static string" here I mean memory that can not be handled by realloc. Hi, I have written a procedure that takes a char * argument and I would like to create a duplicate IF the memory is not relocatable/resizable via realloc. As is,…
user735796
3
votes
0 answers

Spyder IDE : Restarting kernel.. , ipython console in spyder

I am encountering a problem where my Kernal automatically restarts when i run ML model on Spyder IDE I have checked other questions concerning the same topic and none of the answers could solve my situation, any solution for this problem would be…
DevanDev
  • 161
  • 1
  • 2
  • 17
1
2 3 4 5 6 7