Data Structures Flashcards

1
Q

What does the function realloc do?

A

Allocates a requested amount of memory on the heap and returns an address to the start of the reserved memory.

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

What happens when you realloc
a variable old_ptr that is null?

A

Realloc will work like malloc

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

What happens when you realloc
a variable old_ptr that is NOT null?

A

If old_ptr is not null, will allocate enough memory for the new length using malloc and copy the old from old_ptr to new memory and then will free old_ptr.

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

How do you define a multi-dimensional array? (e.g 2d int array)

A

int days[2][13] = {
{0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31}
};

days[1][2];

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

What is an alternative approach of storing a multidimensional array?

A

Using pointer arrays.

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

How is memory allocated for multi-dimensional dynamic arrays?

A

With the use of an additional array containing pointers. Each array is not necessarily stored contiguously in memory.

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

What is the advantage of storing arrays in memory with one pointer?

A
  • Simpler with memory management
  • All allocated in heap memory in one go
    -Each array is stored contiguously
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the disadvantage of storing arrays in memory with one pointer?

A

If using big data, a large free contiguous block of memory to be free is required.

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

What are the advantages of storing arrays contiguously?

A
  • When accessing consecutively, we can be efficient with memory access
  • Allows for pointer arithmetic
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a struct?

A

Data structure/Mechanism for packing multiple variables into a single block.

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

How do structs differ to objects?

A

Structs don’t have functions.

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

How can we access member variables of struct data structure?

A

Dot-syntax to access remember variables.

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

What are the advantages of struct?

A
  • Helps keep correlated variables together
  • Logical ordering and structure to data representation
  • Pass multiple variables packed together as a single parameter to a function
  • A struct allows you to return multiple variables from a function
  • A struct can embed structs within it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is arrow syntax equivalent to?

A
  • dereferencing a point, and
  • accessing a member variables
    at the same time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What will the sizeof(struct x) give at compile time?

A

Gives the byte size of the variables.

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

What are unions in C?

A

Unions in C are a data container that can be defined to store a single item of one or more types. An instance of a union can only contain one item at any time but you can predefine all possible types of the item stored inside a union.

17
Q

What types can be stored in a union?

A

Any existing type (including a struct or union)

18
Q

What is the size of a union?

A

The size of the biggest type within the union.