C: pointers libraries structs Flashcards

1
Q

what is static lifetime vs auto lifetime?

A

Variables declared outside of a function have storage class static. Lifetime begins when a program is loaded, ends when program ens.

Variables declared inside of a function by default have storage class auto. Dies when function call returns.

Variables declared inside of a function may be explicitly declared static. Such variables have local scope, but the global lifetime of static variables.

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

Where in the structure of a program do auto variables live? Can you trust the values of initialized variables here?

A

The stack. Values of uninitialized variables are random.

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

What data goes into the heap?

A

Long term malloc()’d data

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

Where do uninitialized static variables live? Can you trust the values stored here by default?

A

BSS. Always initialized to 0.

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

Where do initalized static variables live?

A

.data

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

are C functions call by value or address?

A

value. bypass w/ pointers

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

$ a.out 1 23 “four five”

what is argc, argv?

A

argc: 4 (includes program name)

argv: [1, 23, “four five”]

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

Is this legal?

char *makeBig(char *s) {
s[0] = toupper(s[0]);
return s;
}

makeBig(“a cat”);

A

No. this code will likely fail because compilers typically place literal strings in a read-only area of the address space

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

Why is strncpy safer than strcpy?

char *strncpy(char *dest, char *source, int num)

A

stops copying after num chars, and appends a /0
less likely to go beyond bounds

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

is there a better way to declare this struct to save memory?

struct {
char x;
int y;
char z;
} s1;

A

Yes. If the chars are placed next to each other in memory, there probably wont be as much padding around the chars for alignment.

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

typedef struct {
unsigned int version:2;
unsigned int p:1;
unsigned int cc:4;
unsigned int m:1;
unsigned int pt:7;
u_int16 seq;
u_int32 ts;
} rtp_hdr_t;

What is this an example of?

A

Bit fields. version, for example, is declared to be 2 bits long, and will only take up 1 byte of memory.

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

union u_tag {
int ival;
float fval;
char *sval;
} u;

what is sizeof(u)? in terms of its attributes.

A

sizeof(float)

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

where does getchar() read from?

A

stdin

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

what is a buffer?

A

“buffer” typically refers to a memory area used for temporary storage. To declare a buffer, you can use an array. For example:

char buffer[256];

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

In C, the fflush function is used to flush (clear) the output buffer. It ensures that any data that was buffered for output is actually written to the file or console. Here’s a concise example:

```c
fflush(stdout); // Flushes the standard output buffer.
~~~

This is particularly useful when you want to make sure that data is immediately written to a file or displayed on the screen instead of being held in a buffer.

A

the fflush function is used to flush (clear) the output buffer. It ensures that any data that was buffered for output is actually written to the file or console. Here’s a concise example:

fflush(stdout); // Flushes the standard output buffer.

Essentially, “save” progress, and get ready for whats next.

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