string literals
char *c = "hello"; // lives in read only memory, works
char c[] = "hello"; // lives on stack, can be changed
char c[] = {'h', 'i', '\0'}; // arrays are constant and can have size specified
“hello” is in read-only data
char *c = "hello";cannot change
sizeof vs strlen
sizeof returns number of chars + 1 for the \0 for strings strlen returns number of chars
strcpy into small buffer (small memory area)
Buffer overflow (inserting way too much data)
typedef
typedef struct TA {
// fields
} TA;
typedef struct TA TA;
typedef struct {
} TA;
Malloc
Common errors
- Forgetting to free (memory leak)
- Using after freeing
- double freeing the same pointer