Memory classes Flashcards
What is static allocation?
When memory is allocated statically when the program is run and freed when the program is terminated. They are declared with static allocation.
How are static variables created and what are their properties?
They are
- They are created by using the static keyword within a function
- Global variables are static
- Global variables declared using the static keyword are unique to the file
- They are automatically zeroed.
What is Automatic memory and when is it freed?
What is scope?
Memory that is allocated when the function is executed at runtime.
These are fixed.
They are freed when the function exists.
Local variables of a function are allocated automatic memory, the size of these variables must be known at compile time.
What is dynamic memory allocation?
This is memory that is allocated at runtime. There is no type associated with this so anything can be stored in this block.
What should you watch out for when using dynamic memory?
- Memory is not automatically managed, meaning that the program should free the memory using free after they’re done with it.
- You cannot free something that has already been freed
- You cannot write into a memory block that has already been freed.
When does malloc return null, what does it return normally?
malloc returns null when there is insufficient memory in the OS. Otherwise it return a pointer to the beginning of the newly created block.
Which header file does malloc, calloc and realloc belong to?
They are all present in the stdlib..h header file
What is free and how do you use it?
free is a function that is used to free up memory that is allocated dynamically.
It accepts a pointer to a block of memory and then it frees it.
Free can only be called on a block of memory once.
NOTE: Do not use free on any pointers apart from the ones that you got from mallocing.
What is calloc?
Calloc is used to dynamically assign memory just like malloc but in addition to that, it also sets all of the bits to 0.
what is the size_t operator used for ?
What is the ptrdiff_t used for?
The compiler has a type called size_t which is an unsigned integer big enough to fit a pointer into.
It is a data type that is used to hold the difference between 2 pointers.
Are 2D arrays the same as a pointer to a pointer? How do they differ?
Apart from the difference in size, a **pointer cannot be dereferenced and used like an array.
How do you declare a pointer to pointer array?
in order to do this, you need to have one array that contains pointers to other arrays?
What is realloc?
How does it increase or decrease the size?
what happens when the size is increased?
Realloc is a function that is used to change the size of a buffer that was previously allocated.
If the size is decreased, then the buffer is just truncated.
if it is increased then the buffer is extended if there is available space. If there is insufficient space, then the buffer is moved elsewhere which might invalidate the previous pointers.
What is the return value of realloc and what should you watch out for?
The return value of realloc is the pointer to the new memory address
NOTE: The original pointer that was realloced could be lost in case the buffer is moved elsewhere.
What is the prototype of memcpy and how does it work/
void * memcpy ( void*dst, void* src, size_t n );
It copies n bytes from the src to the dest.