freeing a pointer frees the memory the pointer points to. allocating a pointer allocate a memory in heap and return the address to the pointer
int *A = xmalloc(sizeof(int) * 5);
allocates on heap
arrays and pointers are the same thing
*A is the first element
pointer arithmetic: A[i] = *(A+i)
where A is elements, not bytes (translated by compiler)
xcalloc(5, sizeof(int))
can clean array memory for you
memory tracking know the length of memory, but not C.
array-out-of-bound is allowed
valgrind can detect out-of-bound error
C99 standard: out-of-bound are undefined behavior
so different compilers can do different things
often just carry on (make code run faster), unless accessing a restricted segment
Why undefined:
fast
it will break legacy code (that relies undefined behavior)
int *B = A+2
We are not allowed to free B
it is undefined behavior
because it is not returned by malloc
B[-1] is also undefined
In C, you can cast any pointer to any other pointer type, never triggers an error.
But different computers stores int differently
Make sure castings are aligned: otherwise out-of-bound or undefined behavior
In C, void* is first element of any array.
no way to infer the size of elements
no way to know number of elements
two pointer with different type can be equal since pointers only store address, without element size.
Freeing an alias casting is allowed.
Char has 8 bits (hhd), the smallest chunk of memory other than void*
it is more likely to align
when print, it will only print the first 8 bits after casted
char may not be restricted by ASCII
Traditional Data Structure can only be heap-allocated
int E[8]
allocates on the stack
int F[] = {2, 4, 6, 8, 10}
They still have type int*
struct point p;
p.x = 9;
p.y = 7;
where p is not a pointer (there is no convenient way to initialize a struct in C99)
&
operation can get address of
functions
local variables
fields of structs
array elements
Therefore:
change *(A+1)
to A[i]
change A+i
to &A[i]
You cannot do:
&(int+2): since i+2=7 is not legal
&(A+3): since A+3 = xcalloc() is not legal
&&i: &i = xmalloc() is not legal
char* s1 = "hello";
it is an char pointer (or char array if you like)
\0
whose value is 0)strlen: counts up to NUL, not included
strcpy(dst, src): copy up to NUL included
char *s1 = "hello"
as TEXT segment
read-only: otherwise undefined
no need to free: undefined
if they have the same contents, then the pointers will be the same (maybe for some compilers)
initialized on string literals
char *s2 = xcalloc(sizeof(char), strlen(char) + 1)
as HEAP
remember to allocate NUL termination
need to free it
char s3[] = "world"; char s4[] = {'s', '\0'}; char s5[5]
as STACK
in stack
no need to free
when inside a struct char s4[2]
, it is in heap(the same place as struct as you free it), assignments to {'s', '\0'} is not allowed unless assign each index individually.
initialized either in string literals or other (https://www.diderot.one/courses/42/post-office/30196) String in C: https://www.youtube.com/watch?v=90gFFdzuZMw&ab_channel=EngineerMan
Other undefined: (can be used to do security hacks)
accessing array out-of-bound
de-referencing NULL
reading uninitialized memory (even if allocated)
using freed memory
double free
freeing memory not returned by malloc
writing to read-only
https://www.diderot.one/courses/42/post-office/30141 Freeing a casted non-void pointer is often undefined if they misaligned
due to misalignment of bytes
but free() on non-void-pointer have same representation regardless of type, so it is allowed.
Table of Content