0

What is Static & Dynamic Memory? C++

Hi everyone. I'm currently doing the C++ course and I'm on the "Data Types, Arrays, Pointers" section on part 11 called "Dynamic Memory". I've read through the descriptions and everything and even googled online for about an hour on what on earth it means to be allocating ram to the Stack or Heap. Please, can somebody explain what the course is trying to teach me but in more simple terms? I'm very new to this so I would appreciate it if somebody could dumb things down for me :)

29th Apr 2018, 8:04 AM
CheatingPenguin
4 odpowiedzi
+ 2
Static memory: It is the space in memory that is created when declaring variables of any type of data (primitives [int, char ...] or derivatives [struct, matrices, pointers ...]). The memory that these variables can be taken into account in the execution and can not be released manually. Example: int a = 1; (Reserve space in static memory, it's the usual) Dynamic memory: It is memory that is reserved at run time. Its main advantage over static, is that its size may vary during the execution of the program. (In C, the programmer is responsible for releasing this memory when he does not use it anymore). The use of dynamic memory is necessary when you do not know the exact number of data / elements to be treated. Example: int *a ; (Reserve space in static memory) a = new int; (You dynamically assign space to contain an int value)
29th Apr 2018, 8:11 AM
David Escalante
David Escalante - avatar
0
Thanks David, that helps a lot. So for static, I don't have to do anything special? Have I been using it all along while I've been creating simple variables? Also, the memory is being created from the Stack, right? And for Dynamic memory, is *a the same variable as a? Or are they separate as *a is the pointer to a? Just a bit confused there. Also, do you know how I could test to see whether or not I'm reserving memory properly?
29th Apr 2018, 6:22 PM
CheatingPenguin
0
CheatingPenguin You are talking about the contents of (dereference) operator 👉 * We generally declare a pointer to a specific data type by typing asterisk(*) sign following the data type example: int , double etc. Consider the following example 👇 int p = 8; //assigned a value to the variable int *r ; // declared a pointer which contains memory address r = &p; //contains the memory address of variable p which contains the value 8 Like the normal variable declaration you don't need to again specify it's data type after you declared the type for the first time . Like that you don't need to add * sign before the variable name. Remember -> int p; //a variable named p int *s; //pointer named s s = &p; //As we should not again add * sign after first declaration same like normal variable declaration Again if we put a asterisk(*) sign after that will so called dereference operator . You can think it of like a switch to a light bulb . If switch a bulb you go to memory location .
10th Dec 2018, 5:32 PM
Atik🇧🇩
Atik🇧🇩 - avatar
0
And again if you do so you come back to your initial place or get the value . You can also think it of logic gate not operation . So full summary👇 int p = 3; // declare variable and assign value int *s ; // declaring a pointer of type integer s = & p; //pointer containing p variable's memory address value cout<<s; //print memory address example: 0x2e34f cout<<*s; //print value stored in that location
10th Dec 2018, 5:39 PM
Atik🇧🇩
Atik🇧🇩 - avatar