diff options
| author | FreeArtMan <dos21h@gmail.com> | 2022-08-11 07:12:18 +0100 | 
|---|---|---|
| committer | FreeArtMan <dos21h@gmail.com> | 2022-08-11 07:12:18 +0100 | 
| commit | b04fada740507cd0d3542556476ad0bb66bbec32 (patch) | |
| tree | e0c8922a1d73ccae304de61f5e489a6ee45f035e /md | |
| parent | e2077b596ac230beb1d69d9141ad02df196abc91 (diff) | |
| download | md-content-b04fada740507cd0d3542556476ad0bb66bbec32.tar.gz md-content-b04fada740507cd0d3542556476ad0bb66bbec32.zip  | |
Add pointer chapter
Diffstat (limited to 'md')
| -rw-r--r-- | md/notes/undefined_c/titles.md | 80 | 
1 files changed, 77 insertions, 3 deletions
diff --git a/md/notes/undefined_c/titles.md b/md/notes/undefined_c/titles.md index 0108449..a09f0a7 100644 --- a/md/notes/undefined_c/titles.md +++ b/md/notes/undefined_c/titles.md @@ -3,8 +3,9 @@ keywords:c,linux,asm  # Undefined C -There is possible to piece of code inside online c compiler like https://www.onlinegdb.com/online_c_compiler -Or run locally. With base check is done with gcc compiler. +There is possible to run piece of code inside online c compiler like https://www.onlinegdb.com/online_c_compiler +Or run locally. With base check is done with gcc compiler. There are many small tricks around running C code +in practice that aren't covered in any generic tutorials.  ## Compile @@ -318,8 +319,80 @@ http://main.lv/writeup/c_macro_tricks.md  https://jadlevesque.github.io/PPMP-Iceberg/ -### Signed/Unsigned +  ### Pointers + +One the C most loved feature is pointers, they allow to access addresses without any sanity check +and they dont have any lifetime, so anything is possible with those. + +Pointer contains address which is interpreted according of pointer type + +```c +int c; +int ptr=&c; +``` + +Go over array of chars +```c +#include <stdio.h> +#include <stdlib.h> + +int main() { +	char s[]="asd"; +	char *c=&s; +	while (*c != 0) { +	    printf("NExt char %c addr %016x\n",*c,c); +	    c++; +	} +} +``` +Go over array of ints +```c +    int i=0; +    int arr[] = {9,7,5,3,1}; +	int *ptr = arr; +	while (i<5) { +	    printf("Number value %d addr %016x\n",*ptr, ptr); +	    ptr++; +	    i++; +	}  +``` + +Pointer arithmetics like +1 will move to next address that is offset of type size. +As example below structure size is 12, and increment of pointer to that structure +increment address to sizeof structure. And yes address is pointing to not mapped memory, so it  +will segfault if accessed.  + +```c +struct size12 { +    int a,b,c; +} + +int main() { +    struct size12 *s=0; +    s++; +    printf("%016x\n",s); +    s++; +    printf("%016x\n",s); +} +``` + +Double pointers are pointers to pointers + +```c +#include <stdio.h> + +int main(int argc, char **argv) { +	char *arg = argv[0]; +    printf("Program name %s\n",arg); +} +``` + +#### How to shoot the leg +Run pointer in while loop incrementing pointer. It will stop only when segfaults. + +Dont initialize pointer and it will have random value. +  ### Endianess   ### Static binary  ### Dynamic binary @@ -327,6 +400,7 @@ https://jadlevesque.github.io/PPMP-Iceberg/  ### Compiler flags  ### Allocate memory  ### stdin,stdout,stderr +### Signed/Unsigned  | 
