summaryrefslogtreecommitdiff
path: root/md/notes
diff options
context:
space:
mode:
Diffstat (limited to 'md/notes')
-rw-r--r--md/notes/undefined_c/titles.md126
1 files changed, 121 insertions, 5 deletions
diff --git a/md/notes/undefined_c/titles.md b/md/notes/undefined_c/titles.md
index a09f0a7..014035b 100644
--- a/md/notes/undefined_c/titles.md
+++ b/md/notes/undefined_c/titles.md
@@ -393,20 +393,136 @@ Run pointer in while loop incrementing pointer. It will stop only when segfaults
Dont initialize pointer and it will have random value.
+
+
+### Allocate memory
+
+From programs perspective memory allocation is adding address range to executable that can be addressed.
+
+malloc should be accompanied with free statement, otherwise it will have memory leaks.
+
+```c
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main() {
+ char *c = malloc(16);
+ memset(c,0,16);
+ int *arr = malloc(16*sizeof(int));
+ memset(arr,0,16*sizeof(int));
+ free(c);
+ free(arr);
+}
+```
+
+### Signed/Unsigned
+
+Signed and unsigned variables differ just in one bit interpretation. But they have different behavior on minimal and maximal values.
+
+
+```c
+#include <stdio.h>
+#include <limits.h>
+int main()
+{
+ int i=INT_MAX;
+ unsigned int u=UINT_MAX;
+
+ printf("i=%d\n",i);
+ printf("u=%u\n",u);
+
+ i++;
+ u++;
+ printf("i=%d\n",i);
+ printf("u=%u\n",u);
+ i=0;
+ u=0;
+ i--;
+ u--;
+ printf("i=%d\n",i);
+ printf("u=%u\n",u);
+
+}
+```
+
### Endianess
+
+
+```c
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int main() {
+ int arr[4] = {0x00112233,0x44556677,0x8899AABB, 0xCCDDEEFF};
+ printf("%08x\n",arr[0]);
+ printf("%08x\n",arr[1]);
+ printf("%08x\n",arr[2]);
+ printf("%08x\n",arr[3]);
+
+ FILE *f = fopen("int.hex","w+");
+ fprintf(f,"%08x",arr[0]);
+ fprintf(f,"%08x",arr[1]);
+ fprintf(f,"%08x",arr[2]);
+ fprintf(f,"%08x",arr[3]);
+ fclose(f);
+
+ int fd=open("int.bin",O_CREAT|O_RDWR,S_IWUSR|S_IRUSR|S_IRGRP|S_IRWXO);
+ write(fd,arr,sizeof(arr));
+ close(fd);
+
+ int i;
+ fd = open("int.bin2",O_CREAT|O_RDWR,S_IWUSR|S_IRUSR|S_IRGRP|S_IRWXO);
+ for (i=0;i<4;i++) {
+ uint32_t val = (arr[i]>>16) &0x0000ffff;
+ val += (arr[i]<<16)&0xffff0000;
+ write(fd,&val,sizeof(uint32_t));
+ }
+ close(fd);
+}
+```
+
+While saving formated values to file you will get what you expect
+```
+$ cat int.hex
+00112233445566778899aabbccddeeff
+```
+
+Saving just memory dump of all values, will give you different result
+```
+$ hexdump int.bin
+0000000 2233 0011 6677 4455 aabb 8899 eeff ccdd
+0000010
+```
+
+Need to swap 16bit pairs to look same as value memory dump
+```
+$ hexdump int.bin2
+0000000 0011 2233 4455 6677 8899 aabb ccdd eeff
+0000010
+```
+
+### Compiler flags
+
+Compiler have whole list of command line arguments that you can enable for different purposes, lets look into some of them
+https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html
+
+
+
+
### Static binary
### Dynamic binary
-### Styles
-### Compiler flags
-### Allocate memory
### stdin,stdout,stderr
-### Signed/Unsigned
+### Styles
+
## Basic usage
-### File manipulation
+### File manipulation with libc
### File manipulation with syscalls
## Base usage