Data Structures Flashcards

(12 cards)

1
Q

int

A

Typically 4 bytes (on 32-bit systems like STM32). The standard “whole number” type.

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

char

A

1 byte. Usually holds an ASCII character or a small integer (-128 to 127).

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

float

A

4 bytes. Used for decimal numbers. Note: Some small microcontrollers don’t have a “Floating Point Unit” (FPU), making these slow!

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

double

A

8 bytes. Double-precision decimal. Even “heavier” than a float.

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

bool

A

1 byte. Stores 0 (false) or 1 (true).

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

Array

A

A collection of the same data type stored in contiguous (back-to-back) memory.Interview Tip: Accessing an array is $O(1)$ speed because the CPU just calculates an offset from the start.

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

Struct

A

A custom container that groups different types together.

Example: A struct Sensor { int id; float value; }; takes up 8 bytes of memory (4 for the int + 4 for the float).

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

Union

A

A container where all members share the same starting memory address.

Why use it? To save space. If you have a union with an int and a char, it only takes up 4 bytes (the size of the largest member).

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

Pointer

A

A variable that stores the memory address of another variable.

Size: Usually 4 bytes on a 32-bit system or 8 bytes on a 64-bit system.

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

What happens to the memory address of a pointer p if you perform p++, and how does the data type of the pointer affect this?

A

The pointer p will point to the next memory address of its specific data type. The jump in address is equal to sizeof(type). For example, incrementing a uint32_t* moves the address forward by 4 bytes, while a char* moves by 1 byte.

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

In an embedded system, why would you declare a variable pointing to a hardware status register as volatile?

A

Hardware registers can change their value independently of the program’s execution (e.g., a bit being set by a sensor). Without volatile, the compiler might optimize the code by reading the value into a CPU register once and never checking the actual hardware memory again, causing the program to miss status changes.

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

If you have a struct containing one char followed by one int, why might sizeof(struct) return 8 bytes instead of 5 bytes?

A

This is due to memory alignment or padding. Most 32-bit processors (like an STM32) are optimized to read 4-byte integers from addresses that are multiples of 4. The compiler inserts “padding” bytes after the char to ensure the int starts on a 4-byte boundary for faster CPU access.

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