Dynamic Memory Allocation in C Flashcards

1
Q

dynamic allocation

A

include stdlib.h

you can allocate exactly the amount of memory you need,
when you need it, at runtime, without using dangerous automatically-allocated variable-length arrays

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

static vs dynamic

A

static allocation happens at compile-time,
dynamic memory allocation happens at runtime.

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

dynamic vs automatic

A

Dynamic allocation occurs from the heap
automatic happens on the stack

dynamic - needs use of specific library function calls to allocate and de-allocate (free) the memory
space.

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

How is dynamically allocated memory freed?

A

Dynamically allocated memory is not freed unless the programmer uses a function call
that explicitly frees it.

Differs from automatic allocation where memory is freed when the automatically-allocated variable goes out of scope. This automatic freeing of memory is possible
only because automatic memory allocations happen on the stack.

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

Dynamic Storage Allocation with malloc()

A

The malloc() function
   
void * malloc ( size_t size );

it returns a pointer to the first byte of the allocated memory space of the specified size in
bytes.

you can typecast void* to another data type

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

allocating memory malloc

A

1) used sizeof to get the size of an integer
2) passed this number of bytes to malloc and allocates memory
3) malloc returns value of type int, because we want to store an integer in the memory.
4) we store the address of our newly allocated memory to this to int
dynamic_int because :
I)to be able to store an integer in the memory we just allocated
ii)so that we can free the memory later.

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

De-allocating Memory with free

A

malloc() is allocated from the heap which means that it will stay allocated until either the program terminates or the program explicitly frees it.

void free ( void * ptr );

free ( dynamic_int );

takes one parameter, address of the first byte of the memory to be freed.
of type void* which means any pointer type can be passed as an argument, and the
argument will be implicitly typecast to void*.

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

allocation of arrays

A

We already know that to
store an array of N elements of type T, you need N  sizeof(T) bytes

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

Initializing Memory to a Specific Value with memset()

A

can initialize array
elements.
defined in string.h

void * memset ( void * b, int c, size_t len );
every byte of the memory pointed to by b to the value c, but c
is first converted to an unsigned char value.
r len must be the number of bytes ofmemory pointed to by b and memset() always returns b

can allocate an array of forty-two 64-bit integers
and initialize them all to zero thusly:

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

Allocating Initialized Memory with calloc()

A

The calloc() function is defined in stdlib.h and its prototype is:
   
void * calloc ( size_t count , size_t size );

allocates enough memory to store count data elements of a given size and initializes all of the allocated memory 0

malloc() that allocates
count * size bytes, followed by a memset() of each byte of the allocated memory to 0.

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