You are on page 1of 2

CS50 Notes #include<cs50.h, stdio.h,string.

h #define Q 2

Segmentation fault: #include <stdio.h>intmain(void){int *p;printf(``%d'', *p);}Interestingly, we could modify this program as follows to avoid a segmen-tation fault:#include <stdio.h>intmain(void){int x = 1;int *p = &x;printf(``%d'', *p);}What we're doing is initializing p with the address of x which we know tobe valid. Now, printing *p and printing x are equivalent: they both give1. In fact, printing *&x is equivalent to printing x as well. The * and &operators invert each other *s1 only gives first character of string strcmp{ #include <cs50.h>#include <stdio.h>#include <string.h>intmain(void){// get line of textprintf("Say something: ");char *s1 = GetString(); // get another line of textprintf("Say something: ");char *s2 = GetString();// try to compare stringsif (s1 != NULL && s2 != NULL){if (strcmp(s1, s2) == 0)printf("You typed the same thing!\n");elseprintf("You typed different things!\n");}}} malloc realloc buffer overrun attack: filling a stack frame with more data than it can handle stack storage:

8 bits = 1 byte A=65=01000001 int %d= 4 bytes, can store 2^32 (4 bil) numbers long long %lld= 64-bits, float (4 bytes) can store decimals (but imprecise) char %c 1 single character, 1 byte of storage The %.10f tells printf to substitute in a floating-point value and to print 10 digits after the decimal point Typecasting: can cast 13 to float if we write (float) 13 Must type function prototype Text>initialized data>uninitialized data>heap -> <-stack> environment variables Caesars cipher: rotate characters; Vigeneres cipher: keys are words, each letter represents number to rotate 26^n keys O is worst-case scenario Selection sort, bubble sort- O(n^2) Selection sort Omega(n^2), bubble sort with memory Omega(n) Gdb debugger (gdb) (break(fn)) (next) (step) (backtrace) (continue) Merge sort O(nlog n) O(1) \constant"O(log n) \logarithmic"O(n) \linear"O(n log n) \supralinear"O(n2) \quadratic"O(nc) \polynomial"O(n!) \factorial" Hexadecimal base 16 Strings actually 32-bit pointers %p Strcmp(char*,char*) Toupper converts lower to upper case Malloc(,sizeof()) * is dereferencing operator (&x is pointer of int*) a pointer, *a integer, x integer, &x pointer The * has two meanings: when declaring a variable, it denotes it as a pointer and when assigning to a variable, it dereferences the variable. The & is the \address of " operator. So &x gives us the address of x, which might look like 0xb 50c if we printed it out in GDB.

char *buffer = malloc(10);In order to use malloc, we need to include the stdlib.h library, which thus far the CS50 Library has been doing for us. Scanf wants memory address (or array) as argument writing s[i] is actually equivalent to writing *(s+i) // structure representing a studenttypedef struct{int id;char *name;char *house;}student;

Whereas the #include% (a% preTprocessor% directive)% informs% gcc% that% a% program% might% call%functions%declared%in%cs50.h,%the%-lcs50% (a% linker% flag)% tells%gcc% to% incorporate% the% bits% thatresult%from%compiling%cs50.c%(wherever%they%are) %into%the%program Whereas% \n% moves% a% cursor% down% to% a% new% line,% \r% moves% a% cursor% to% the% beginning% of% the%current%line.%%In%the%context%of%files,%Linux%u ses%\n% to% end% lines,% Mac% OS% uses%\r,%and%Windows%uses%\r\n You%can%trigger%a%segfault%by%indexing%into%an%arr ay%far%beyond%its%bounds,%by%dereferencing%an%inval id%(e.g.,%garbage%or%NULL)%pointer,%and%even%by%c alling%a%function%recursively%too%many%times%(the%re sult%of%which%is%that%the%stack%overruns%the%heap). %%

C PHP names

The student failed to include#include <cs50.h>atop his or her code; The student failed to compile with -lcs50, which tells GCC to link the students code against(i.e., borrow bits from) CS 50s library

./a.out foo bar baz

calloc() allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero.malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.

Buffer%overflow%attacks%can%be%prevented%by%ensurin g%that%arrays%boundaries%cannot%be%exceeded%by% writes

a string could be no more than255 chars long, as a byte can only store 256 different values

You might also like