Thursday, November 26, 2020

Compiling CS things to study

 https://www.youtube.com/watch?v=vNHpsC5ng_E&list=PLF206E906175C7E07
(Design Patterns Video Tutorial) edit: https://www.youtube.com/watch?v=NU_1StN5Tkk (short thanks to fccokc)

https://www.youtube.com/playlist?list=PLBlnK6fEyqRgp46KUv4ZY69yXmpwKOIev
(Finite State Machine)

https://www.youtube.com/watch?v=RBSGKlAvoiM
https://github.com/williamfiset/data-structures
(Data Structures Easy to Advanced Course - Full Tutorial from a Google Engineer)
{Edit:(1/27/2021): New Data Structures Course: Data Structures - Full Course Using C and C++ : https://www.youtube.com/watch?v=B31LgI4Y4DQ }

https://www.youtube.com/watch?v=09_LlHjoEiY
(Algorithms Course - Graph Theory Tutorial from a Google Engineer)


{edit: other links: (1/27/2021)
The Algorithm Design Manual:

https://mimoza.marmara.edu.tr/~msakalli/cse706_12/SkienaTheAlgorithmDesignManual.pdf ,

https://github.com/williamfiset/algorithms ,

https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-design-and-analysis-of-algorithms-spring-2015/index.htm ,


advanced java: https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/course/ ,

Phil Trelford - Write your own compiler in 24 hours - Bristech Conference 2015
https://www.youtube.com/watch?v=iOEHgBL_GmA&app=desktop ,

Parser and Lexer — How to Create a Compiler part 1/5 — Converting text into an Abstract Syntax Tree https://www.youtube.com/watch?v=eF9qWbuQLuw&t=2492s ,

Theory  of Computation: https://www.youtube.com/watch?v=58N2N7zJGrQ&list=PLBlnK6fEyqRgp46KUv4ZY69yXmpwKOIev&index=1 }







Wednesday, November 4, 2020

understanding assembly ... (edit: actually learning about pointers and memory to solve memory issues with a function in C)

this code, which is for the mergeChar function in
/changearray/ifstructures/loopbyFour/example1_2_9f.c

https://github.com/bshambaugh/changearray/blob/master/ifstructures/loopbyFour/mergeCharsFuncation/codingUnit_com/test_final/Copy5useCallocMult4.c

https://www.geeksforgeeks.org/how-to-return-multiple-values-from-a-function-in-c-or-cpp/

Try with function:
http://vertstudios.com/blog/malloc-functions-returning-pointers/

https://overiq.com/c-programming-101/array-of-strings-in-c/

----------------------------

edit:

C Pointer Tutorial
https://www.youtube.com/watch?v=IkyWCOUY8V4
(includes mention of how to pass pointer to function)
----------------------------------------------------------------------------------------

https://www.eskimo.com/~scs/cclass/int/sx9b.html

http://phyweb.physics.nus.edu.sg/~phywjs/CZ1102/lecture20/tsld014.htm

https://stackoverflow.com/questions/1733881/c-correctly-freeing-memory-of-a-multi-dimensional-array

https://joequery.me/notes/double-pointers-c/

--------------


https://www.recurse.com/blog/7-understanding-c-by-learning-assembly   [Understanding C by learning assembly]

http://cs.ecs.baylor.edu/~donahoo/tools/valgrind/messages.html  [Excerpt from Valgrind documentation]

https://valgrind.org/docs/manual/quick-start.html [The Valgrind Quick Start Guide]


Dump of assembler code for function main:
   0x00005555555547f2 <+0>:     push   %rbp
   0x00005555555547f3 <+1>:     mov    %rsp,%rbp
=> 0x00005555555547f6 <+4>:     mov    $0x1,%edi
   0x00005555555547fb <+9>:     callq  0x555555554660 <malloc@plt>
   0x0000555555554800 <+14>:    mov    %rax,0x200821(%rip)        # 0x555555755028 <A>
   0x0000555555554807 <+21>:    mov    $0x1,%edi
   0x000055555555480c <+26>:    callq  0x555555554660 <malloc@plt>
   0x0000555555554811 <+31>:    mov    %rax,0x200800(%rip)        # 0x555555755018 <B>
   0x0000555555554818 <+38>:    mov    0x200809(%rip),%rax        # 0x555555755028 <A>
   0x000055555555481f <+45>:    movb   $0x62,(%rax)
   0x0000555555554822 <+48>:    mov    0x2007ef(%rip),%rax        # 0x555555755018 <B>
   0x0000555555554829 <+55>:    movb   $0x61,(%rax)
   0x000055555555482c <+58>:    mov    0x2007f5(%rip),%rax        # 0x555555755028 <A>
   0x0000555555554833 <+65>:    mov    %rax,%rsi
   0x0000555555554836 <+68>:    lea    0x1c4(%rip),%rdi        # 0x555555554a01
   0x000055555555483d <+75>:    mov    $0x0,%eax
   0x0000555555554842 <+80>:    callq  0x555555554650 <printf@plt>
   0x0000555555554847 <+85>:    mov    0x2007ca(%rip),%rax        # 0x555555755018 <B>
   0x000055555555484e <+92>:    mov    %rax,%rsi
   0x0000555555554851 <+95>:    lea    0x1bd(%rip),%rdi        # 0x555555554a15
   0x0000555555554858 <+102>:   mov    $0x0,%eax
   0x000055555555485d <+107>:   callq  0x555555554650 <printf@plt>
   0x0000555555554862 <+112>:   mov    0x2007bf(%rip),%rax        # 0x555555755028 <A>
   0x0000555555554869 <+119>:   movzbl (%rax),%eax
   0x000055555555486c <+122>:   mov    %al,0x2007ae(%rip)        # 0x555555755020 <words>
   0x0000555555554872 <+128>:   movb   $0x0,0x2007a8(%rip)        # 0x555555755021 <words+1>
   0x0000555555554879 <+135>:   mov    0x200798(%rip),%rax        # 0x555555755018 <B>
   0x0000555555554880 <+142>:   movzbl (%rax),%eax
   0x0000555555554883 <+145>:   mov    %al,0x200799(%rip)        # 0x555555755022
   0x0000555555554889 <+151>:   movb   $0x0,0x200793(%rip)        # 0x555555755023
   0x0000555555554890 <+158>:   lea    0x192(%rip),%rdi        # 0x555555554a29
   0x0000555555554897 <+165>:   callq  0x555555554630 <puts@plt>
   0x000055555555489c <+170>:   lea    0x20077d(%rip),%rsi        # 0x555555755020 <words>
   0x00005555555548a3 <+177>:   lea    0x19d(%rip),%rdi        # 0x555555554a47
   0x00005555555548aa <+184>:   mov    $0x0,%eax
   0x00005555555548af <+189>:   callq  0x555555554650 <printf@plt>
   0x00005555555548b4 <+194>:   lea    0x200767(%rip),%rsi        # 0x555555755022
   0x00005555555548bb <+201>:   lea    0x19e(%rip),%rdi        # 0x555555554a60
   0x00005555555548c2 <+208>:   mov    $0x0,%eax
   0x00005555555548c7 <+213>:   callq  0x555555554650 <printf@plt>
   0x00005555555548cc <+218>:   mov    0x200745(%rip),%rax        # 0x555555755018 <B>
   0x00005555555548d3 <+225>:   movzbl (%rax),%eax
   0x00005555555548d6 <+228>:   movsbl %al,%edx
   0x00005555555548d9 <+231>:   mov    0x200748(%rip),%rax        # 0x555555755028 <A>
   0x00005555555548e0 <+238>:   movzbl (%rax),%eax
   0x00005555555548e3 <+241>:   movsbl %al,%eax
   0x00005555555548e6 <+244>:   mov    %edx,%esi
   0x00005555555548e8 <+246>:   mov    %eax,%edi
   0x00005555555548ea <+248>:   callq  0x55555555478a <mergeChars>
   0x00005555555548ef <+253>:   mov    0x200732(%rip),%rax        # 0x555555755028 <A>
   0x00005555555548f6 <+260>:   mov    %rax,%rdi
   0x00005555555548f9 <+263>:   callq  0x555555554620 <free@plt>
   0x00005555555548fe <+268>:   mov    0x200713(%rip),%rax        # 0x555555755018 <B>
   0x0000555555554905 <+275>:   mov    %rax,%rdi
   0x0000555555554908 <+278>:   callq  0x555555554620 <free@plt>
   0x000055555555490d <+283>:   lea    0x165(%rip),%rdi        # 0x555555554a79
   0x0000555555554914 <+290>:   callq  0x555555554630 <puts@plt>
   0x0000555555554919 <+295>:   lea    0x200700(%rip),%rsi        # 0x555555755020 <words>
   0x0000555555554920 <+302>:   lea    0x120(%rip),%rdi        # 0x555555554a47
   0x0000555555554927 <+309>:   mov    $0x0,%eax
   0x000055555555492c <+314>:   callq  0x555555554650 <printf@plt>
   0x0000555555554931 <+319>:   lea    0x2006ea(%rip),%rsi        # 0x555555755022
   0x0000555555554938 <+326>:   lea    0x121(%rip),%rdi        # 0x555555554a60
   0x000055555555493f <+333>:   mov    $0x0,%eax
   0x0000555555554944 <+338>:   callq  0x555555554650 <printf@plt>
   0x0000555555554949 <+343>:   lea    0x143(%rip),%rdi        # 0x555555554a93
   0x0000555555554950 <+350>:   callq  0x555555554630 <puts@plt>
   0x0000555555554955 <+355>:   mov    $0x0,%eax
   0x000055555555495a <+360>:   pop    %rbp
   0x000055555555495b <+361>:   retq   
End of assembler dump.



 

Tuesday, November 3, 2020

trying to debug code...apparently I cannot printf out from this point... and the memory addresses with assembler do not match valgrind

 https://www.recurse.com/blog/7-understanding-c-by-learning-assembly   [Understanding C by learning assembly]

http://cs.ecs.baylor.edu/~donahoo/tools/valgrind/messages.html  [Excerpt from Valgrind documentation]

https://valgrind.org/docs/manual/quick-start.html [The Valgrind Quick Start Guide]

Dump of assembler code for function main:
   0x00005555555547f2 <+0>:     push   %rbp
   0x00005555555547f3 <+1>:     mov    %rsp,%rbp
=> 0x00005555555547f6 <+4>:     mov    $0x1,%edi
   0x00005555555547fb <+9>:     callq  0x555555554660 <malloc@plt>
   0x0000555555554800 <+14>:    mov    %rax,0x200821(%rip)        # 0x555555755028 <A>
   0x0000555555554807 <+21>:    mov    $0x1,%edi
   0x000055555555480c <+26>:    callq  0x555555554660 <malloc@plt>
   0x0000555555554811 <+31>:    mov    %rax,0x200800(%rip)        # 0x555555755018 <B>
   0x0000555555554818 <+38>:    mov    0x200809(%rip),%rax        # 0x555555755028 <A>
   0x000055555555481f <+45>:    movb   $0x62,(%rax)
   0x0000555555554822 <+48>:    mov    0x2007ef(%rip),%rax        # 0x555555755018 <B>
   0x0000555555554829 <+55>:    movb   $0x61,(%rax)
   0x000055555555482c <+58>:    mov    0x2007f5(%rip),%rax        # 0x555555755028 <A>
   0x0000555555554833 <+65>:    mov    %rax,%rsi
   0x0000555555554836 <+68>:    lea    0x1c4(%rip),%rdi        # 0x555555554a01
   0x000055555555483d <+75>:    mov    $0x0,%eax
   0x0000555555554842 <+80>:    callq  0x555555554650 <printf@plt>
   0x0000555555554847 <+85>:    mov    0x2007ca(%rip),%rax        # 0x555555755018 <B>
   0x000055555555484e <+92>:    mov    %rax,%rsi
   0x0000555555554851 <+95>:    lea    0x1bd(%rip),%rdi        # 0x555555554a15
   0x0000555555554858 <+102>:   mov    $0x0,%eax
   0x000055555555485d <+107>:   callq  0x555555554650 <printf@plt>
   0x0000555555554862 <+112>:   mov    0x2007bf(%rip),%rax        # 0x555555755028 <A>
   0x0000555555554869 <+119>:   movzbl (%rax),%eax
   0x000055555555486c <+122>:   mov    %al,0x2007ae(%rip)        # 0x555555755020 <words>
   0x0000555555554872 <+128>:   movb   $0x0,0x2007a8(%rip)        # 0x555555755021 <words+1>
   0x0000555555554879 <+135>:   mov    0x200798(%rip),%rax        # 0x555555755018 <B>
   0x0000555555554880 <+142>:   movzbl (%rax),%eax
   0x0000555555554883 <+145>:   mov    %al,0x200799(%rip)        # 0x555555755022
   0x0000555555554889 <+151>:   movb   $0x0,0x200793(%rip)        # 0x555555755023
   0x0000555555554890 <+158>:   lea    0x192(%rip),%rdi        # 0x555555554a29
   0x0000555555554897 <+165>:   callq  0x555555554630 <puts@plt>
   0x000055555555489c <+170>:   lea    0x20077d(%rip),%rsi        # 0x555555755020 <words>
   0x00005555555548a3 <+177>:   lea    0x19d(%rip),%rdi        # 0x555555554a47
   0x00005555555548aa <+184>:   mov    $0x0,%eax
   0x00005555555548af <+189>:   callq  0x555555554650 <printf@plt>
   0x00005555555548b4 <+194>:   lea    0x200767(%rip),%rsi        # 0x555555755022
   0x00005555555548bb <+201>:   lea    0x19e(%rip),%rdi        # 0x555555554a60
   0x00005555555548c2 <+208>:   mov    $0x0,%eax
   0x00005555555548c7 <+213>:   callq  0x555555554650 <printf@plt>
   0x00005555555548cc <+218>:   mov    0x200745(%rip),%rax        # 0x555555755018 <B>
   0x00005555555548d3 <+225>:   movzbl (%rax),%eax
   0x00005555555548d6 <+228>:   movsbl %al,%edx
   0x00005555555548d9 <+231>:   mov    0x200748(%rip),%rax        # 0x555555755028 <A>
   0x00005555555548e0 <+238>:   movzbl (%rax),%eax
   0x00005555555548e3 <+241>:   movsbl %al,%eax
   0x00005555555548e6 <+244>:   mov    %edx,%esi
   0x00005555555548e8 <+246>:   mov    %eax,%edi
   0x00005555555548ea <+248>:   callq  0x55555555478a <mergeChars>
   0x00005555555548ef <+253>:   mov    0x200732(%rip),%rax        # 0x555555755028 <A>
   0x00005555555548f6 <+260>:   mov    %rax,%rdi
   0x00005555555548f9 <+263>:   callq  0x555555554620 <free@plt>
   0x00005555555548fe <+268>:   mov    0x200713(%rip),%rax        # 0x555555755018 <B>
   0x0000555555554905 <+275>:   mov    %rax,%rdi
   0x0000555555554908 <+278>:   callq  0x555555554620 <free@plt>
   0x000055555555490d <+283>:   lea    0x165(%rip),%rdi        # 0x555555554a79
   0x0000555555554914 <+290>:   callq  0x555555554630 <puts@plt>
   0x0000555555554919 <+295>:   lea    0x200700(%rip),%rsi        # 0x555555755020 <words>
   0x0000555555554920 <+302>:   lea    0x120(%rip),%rdi        # 0x555555554a47
   0x0000555555554927 <+309>:   mov    $0x0,%eax
   0x000055555555492c <+314>:   callq  0x555555554650 <printf@plt>
   0x0000555555554931 <+319>:   lea    0x2006ea(%rip),%rsi        # 0x555555755022
   0x0000555555554938 <+326>:   lea    0x121(%rip),%rdi        # 0x555555554a60
   0x000055555555493f <+333>:   mov    $0x0,%eax
   0x0000555555554944 <+338>:   callq  0x555555554650 <printf@plt>
   0x0000555555554949 <+343>:   lea    0x143(%rip),%rdi        # 0x555555554a93
   0x0000555555554950 <+350>:   callq  0x555555554630 <puts@plt>
   0x0000555555554955 <+355>:   mov    $0x0,%eax
   0x000055555555495a <+360>:   pop    %rbp
   0x000055555555495b <+361>:   retq   
End of assembler dump. 

--------------------------------------------------------------------------------------------------------------------------


==6155==    by 0x108846: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==  Address 0x522d041 is 0 bytes after a block of size 1 alloc'd
==6155==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6155==    by 0x1087FF: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==
--6155-- REDIR: 0x4fcad20 (libc.so.6:__mempcpy_avx_unaligned_erms) redirected to 0x4c37130 (mempcpy)
A is currently: b
==6155== Invalid read of size 1
==6155==    at 0x4C32D04: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6155==    by 0x4E99562: vfprintf (vfprintf.c:1643)
==6155==    by 0x4EA0FA5: printf (printf.c:33)
==6155==    by 0x108861: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==  Address 0x522d091 is 0 bytes after a block of size 1 alloc'd
==6155==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6155==    by 0x108810: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==
B is currently: a
before the memory is freed:

here is the words[0] b
here is the words[1] a
Here is something with A 98
*** stack smashing detected ***: <unknown> terminated
==6155==
==6155== Process terminating with default action of signal 6 (SIGABRT)
==6155==    at 0x4E7AF47: raise (raise.c:51)
==6155==    by 0x4E7C8B0: abort (abort.c:79)
==6155==    by 0x4EC5906: __libc_message (libc_fatal.c:181)
==6155==    by 0x4F70E80: __fortify_fail_abort (fortify_fail.c:33)
==6155==    by 0x4F70E41: __stack_chk_fail (stack_chk_fail.c:29)
==6155==    by 0x1087EF: mergeChars (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==    by 0x1088EE: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
--6155-- REDIR: 0x4ed39c0 (libc.so.6:free) redirected to 0x4c30cd0 (free)
==6155==
==6155== HEAP SUMMARY:
==6155==     in use at exit: 2 bytes in 2 blocks
==6155==   total heap usage: 3 allocs, 1 frees, 1,026 bytes allocated
==6155==
==6155== Searching for pointers to 2 not-freed blocks
==6155== Checked 74,008 bytes
==6155==
==6155== 1 bytes in 1 blocks are still reachable in loss record 1 of 2
==6155==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6155==    by 0x1087FF: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==
==6155== 1 bytes in 1 blocks are still reachable in loss record 2 of 2
==6155==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6155==    by 0x108810: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==
==6155== LEAK SUMMARY:
==6155==    definitely lost: 0 bytes in 0 blocks
==6155==    indirectly lost: 0 bytes in 0 blocks
==6155==      possibly lost: 0 bytes in 0 blocks
==6155==    still reachable: 2 bytes in 2 blocks
==6155==         suppressed: 0 bytes in 0 blocks
==6155==
==6155== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
==6155==
==6155== 1 errors in context 1 of 2:
==6155== Invalid read of size 1
==6155==    at 0x4C32D04: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6155==    by 0x4E99562: vfprintf (vfprintf.c:1643)
==6155==    by 0x4EA0FA5: printf (printf.c:33)
==6155==    by 0x108861: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==  Address 0x522d091 is 0 bytes after a block of size 1 alloc'd
==6155==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6155==    by 0x108810: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==
==6155==
==6155== 1 errors in context 2 of 2:
==6155== Invalid read of size 1
==6155==    at 0x4C32D04: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6155==    by 0x4E99562: vfprintf (vfprintf.c:1643)
==6155==    by 0x4EA0FA5: printf (printf.c:33)
==6155==    by 0x108846: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==  Address 0x522d041 is 0 bytes after a block of size 1 alloc'd
==6155==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6155==    by 0x1087FF: main (in /home/ubuntu/Downloads/changearray/ifstructures/loopbyFour/mergeCharsFuncation/tryThree)
==6155==
==6155== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Aborted (core dumped)