Lecture 5 - Introduction to C programming Flashcards
What does it mean that C is a procedural language?
Programmers think in terms of operations to be done and supply data for the operation
add(a, b)
In object-oriented languages programmers think of the objects to be operated on and then what operation to be performed
rect.area()
What are the built-in data types in C
char (8 bit)
short (16)
int (16, 32, 64)
long (32, 64)
float (32)
double (64)
What is the syntax when declaring a pointer?
int *p;
How do you put an address in a pointer variable?
p = &a;
&a: gives address of a
a must be the same type as the pointer-type
How to change a value pointed to by a pointer?
p = &a
*p = 5
The last line changes the value of a
What is two’s complement?
Example using negative short:
short a = -65;
65 = 00000000 01000001
Two’s complement:
- flip all the bits
- 11111111 10111110
- Add 1 to the flipped number
- 11111111 10111111
- This is the two complement of a
What is two’s complement used for in C memory mapping
When having negative values (for example a negative short), it is represented as the two’s complement of the positive value.
This way the signed bit is 1, indicating a negative value. We get the magnitude of the number by taking the two’s complement again.
What happens if a short is assigned to an int?
As the short is only 8 bits, to keep the sign-bit the same, the value is sign extended.
Copy the upper bits to the rest of the higher order bits in the int
What happens if an integer (32 bits) is assigned to a short (16 bits)?
If the integer is large enough to take more than 16 bits, only the 16 LSB are copied to the short, and the higher order bits are discarded.
How many bits are used to hold pointer values?
Enough bits to represent the addresses. These are the same across all pointer-types, as the addresses of ints are just as large as addresses of pointers
What does the types of the pointers do?
When writing to a value pointed to by a pointer, the pointer type tells the program how many bits to represent this value in.
short *p;
*p = 65;
Here we know 65 needs to be represented in 16 bits
int *p
*p = 65
Here 65 would be represented by i.e 32 bits
How can you cast pointers to point to different data types?
int a;
short p = (short) &a
What is the difference between these to statements:
int a = 65
short b = a
int a = 65
short b = (short) &a
In the first, the lower bytes of a is copied to b
In the second, the 2 higher order bytes are copied to b
How is structs declared?
struct structName {
int x, y;
} variableName;
struct structName varName;
How do you access struct values?
Struct variable:
s.x
Struct pointer:
s->
How is a struct pointer declared?
struct structName *varName
How are arrays declared in C?
int a[] = {x1, x2, x3}
The amount of elements decides the array size
int a[10];
Here the size is declared directly
What does a represent in this statement
int a[] = {x1, x2, x3}
a is the starting address of the array
a = &a[0]
*arr = a[0]
What does 3 represent in the statement below:
a[3] = 4;
Add 3 bytes*sizeof(element) to the base address a.
Go 3 elements ahead in the array
What does -3 represent in this statement:
a[-3] = 10
Start at base address a and go 3 elements backwards, and write the value 10 there
What is pointer arithmetic?
When you add something to a pointer, you add n times the size of the element that the pointer points to
Explain this expression:
*p++
Take value pointed to by p, then increment pointer to next element
Explain this expression:
*++p
Increment pointer, and take the value of the incremented pointer
Explain this expression:
(*p)++
Increment the value pointed to by p, p is unchanged
What is static memory allocation?
The compiler knows exactly how much memory needs to be allocated
How can we dynamically allocate memory
use malloc, it allocates a chunk of memory at run time and returns the address
What are memory leaks?
When dynamically allocated memory is not freed. This memory will become unavailable to the program. This can result in a program running out of memory and crash
What type of memory is stored on the heap, stack and static
Heap: Dynamically allocated data
Stack: Function/method local variables
Static: For data living during the whole program lifetime
What is the memory layout
Stack: Managed by compiler, grows down
Heap: Managed by programmer, grows up
Static: Initialized on program start
Instruction: Initialized on program start
When stack and heap meet, there are no more available memory