Memory classes Flashcards

1
Q

What is static allocation?

A

When memory is allocated statically when the program is run and freed when the program is terminated. They are declared with static allocation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are static variables created and what are their properties?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Automatic memory and when is it freed?

What is scope?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is dynamic memory allocation?

A

This is memory that is allocated at runtime. There is no type associated with this so anything can be stored in this block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What should you watch out for when using dynamic memory?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When does malloc return null, what does it return normally?

A

malloc returns null when there is insufficient memory in the OS. Otherwise it return a pointer to the beginning of the newly created block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which header file does malloc, calloc and realloc belong to?

A

They are all present in the stdlib..h header file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is free and how do you use it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is calloc?

A

Calloc is used to dynamically assign memory just like malloc but in addition to that, it also sets all of the bits to 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is the size_t operator used for ?

What is the ptrdiff_t used for?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are 2D arrays the same as a pointer to a pointer? How do they differ?

A

Apart from the difference in size, a **pointer cannot be dereferenced and used like an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you declare a pointer to pointer array?

A

in order to do this, you need to have one array that contains pointers to other arrays?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is realloc?

How does it increase or decrease the size?

what happens when the size is increased?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the return value of realloc and what should you watch out for?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the prototype of memcpy and how does it work/

A

void * memcpy ( void*dst, void* src, size_t n );

It copies n bytes from the src to the dest.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a drawback of memcpy?

A

The problem with memcpy is that if the 2 memory blocks overlap then the copying might not work correctly.

17
Q

What is memmove?

A

Memmove is identical to memcpy but it promises to move the block regardless of whether they overlap or not.

18
Q

How does memcpy and memmove differ from strcpy?

A

memmove and memcpy copy fixed blocks and does not stop at the null character like strcpy

19
Q

What is the prototype of memcmp and what does it do?

A

int memcmp(const void* mem1, const void* mem2, size_t n);

It compares the first n bits of mem1 and mem2

20
Q

What does memchr and memset do and what are the prototypes?

A

Prototype: void* memchr ( const void* mem1, int c, size_t n );

checks to find the first occurence of c in the first n bytes of mem1

memset sets the first n bytes of mem1 to c.

21
Q

Why should memcmp never be used on structs?

A

Memcmp should never be used on structs because structs have padding issues and the padding can be of any value.

22
Q

In what order should you free a struct which has pointers?

A

Freeing should be done in the reverse order of mallocing.

So, first free all of the pointers inside the struct and then free the struct.

23
Q

What is the use of <limits.h></limits.h>

A

Limits gives the maximum and minimum values of types.

Each type has an _MAX and an _MIN except the unsigned type which only has an _MIN.

24
Q

float.h?

A

Similar to limit.h but gives information for floating point numbers

FLT_MIN, FLT_MAX, DBL_MIN, DBL_MAX ….

25
Q

What is rand() used for and what is the header file for it?

What is the range for RAND_MAX

A

rand() is a function in the stdlib.h header file and it is used to generate a random number in the range between 0 and RAND_MAX

The value for RAND_MAX is 32767

26
Q

What is the use of srand() and what is its default value?

A

Rand() generates a number based on the seed given to it. srand() is used to give a seed to rand() based off of which, it creates random numbers.

27
Q
A