C Strings Examples char txt 1 text char
![C Strings Examples char txt 1[] = "text"; char* txt 2 = "text"; int C Strings Examples char txt 1[] = "text"; char* txt 2 = "text"; int](https://slidetodoc.com/presentation_image_h2/c4abc6976c5758eaae7098412fbffbc4/image-1.jpg)


































- Slides: 35
![C Strings Examples char txt 1 text char txt 2 text int C Strings Examples char txt 1[] = "text"; char* txt 2 = "text"; int](https://slidetodoc.com/presentation_image_h2/c4abc6976c5758eaae7098412fbffbc4/image-1.jpg)
C Strings Examples char txt 1[] = "text"; char* txt 2 = "text"; int i = strlen(txt 1); // i = 4, same for strlen(txt 2) txt 1[0] = ’n’; *txt 2 = ’n’; txt 2 = txt 1; txt 1 = txt 2; // // // now txt 1="next“ illegal! “text” is in the code segment legal. now txt 2 points to the same string. illegal! if (! (strcmp(txt 2, "next")) //This condition is now true { // compare two strings. . 1 // when str 1 < str 2 lexicographically return < 0 // when str 1 > str 2 lexicographically return > 0 // when identical return 0

Tirgul 4 - Agenda �Dynamic allocation! �Data structures management: �Dynamic array �Linked list �Memory errors (or common bugs) 2

Dynamic array � In static arrays we need to know the array size at compilation time: int arr[5] = {1, 2, 3, 4, 5}; � But, this size may not be known in advance, or might be changed during the program execution. � The solution: use dynamic array: int *arr = (int*)malloc(sizeof(int)*array. Size); 3

Array reallocation – wrong solution int *arr = (int*)malloc(sizeof(int)*array. Size); //put some values in arr int* new. Arr = (int*)malloc(sizeof(int)*(array. Size+1)); //copy values from arr to new. Arr arr = new. Arr; // BAD: lost address of first allocation! 4

Array reallocation �Allocate array on the heap and assign a pointer to it arr 1 5 2 3

Array reallocation �Use temporary pointer to point to the old array. arr temp 1 6 2 3

Array reallocation �Use temporary pointer to point to the old array. �Reallocate new array. arr 7 temp 1 2 3 ? ? ?

Array reallocation �Use temporary pointer to point to the old array. �Reallocate new array. �Copy values from the old array to the new one. arr 8 temp 1 2 3 ? ?

Array reallocation �Use temporary pointer to point to the old array. �Reallocate new array. �Copy values from the old array to the new one. �Free the old array. arr 9 temp 1 2 3 ? ?

Array reallocation – version 1 void reallocate. Memory(int **arr, unsigned int old. Size, unsigned int new. Size) { //partial example – do not forget initialization and checking alloc. int *temp = *arr; *arr = (int*)malloc(sizeof(int)*new. Size); int i = 0; while( i < old. Size) { (*arr)[i] = temp[i]; i++; } free(temp); } int main() { int *arr = (int*)malloc(sizeof(int)*arr. Size); //do some stuff reallocate. Memory(&arr, arr. Size, new. Size); 10 free(arr)

Array reallocation – version 2 int* reallocate. Memory(int *arr, unsigned int old. Size, unsigned int new. Size) { //Partial example due to space limitations int *temp = arr; arr = (int*)malloc(sizeof(int)*new. Size); int i = 0; while( i < old. Size) { arr[i] = arr[i]; i++; } free(temp); return arr; } int main() { int *arr = (int*)malloc(sizeof(int)*arr. Size); //do some stuff arr = reallocate. Memor(arr, arr. Size, new. Size); free(arr); 11

Array reallocation – using realloc int * arr = (int*)malloc(sizeof(int)*old. Size); arr = (int*)realloc(arr, sizeof(int)*new. Size); � realloc tries to reallocate the new memory in place, if fails, tries elsewhere � The old data is preserved � The new cells contents is undefined � If arr=NULL, behaves like alloc 12

Linked List 13

Example - List Cloning List* List_clone(const List* list) { List* ret= List_alloc(); const Node* old= list->_head; Node** copy= &(ret->_head); ret->_size= list->_size; while(old) { *copy= Node_alloc(old->_data, NULL); old= old->_next; copy= &((*copy)->_next); } return ret; } 14

List* ret=List_alloc(); 15

List* ret=List_alloc(); 16

const Node* old=list->_head; 17

Node** copy=&(ret->_head); 18

ret->_size=list->_size; 19

while(old) { … 20

*copy=Node_alloc(old->_data, NULL); 21

*copy=Node_alloc(old->_data, NULL); 22

old=old->_next; 23

copy=&((*copy)->_next); 24

copy=&((*copy)->_next); 25

copy=&((*copy)->_next); 26

} return ret; } 27

Linked List �Take a look at the code 28

Memory errors (or common bugs) �Data structures management: �Dynamic array �Linked list 29

Bug 1 (1) typedef struct _Student (2) { (2) int id; (3) char * name; (4) } Student; (5) Student * stud = (Student *) malloc( sizeof(Student) ); (6) stud->id = 123456; (7) stud->name = (char *) malloc(100*sizeof(char)); … (8) free(stud); Memory leak of ‘name’! 30

Bug 2 void my. Func() { int * x = random. Num. Ptr(); int result = *x; // unexpected! *x = 17; // accessing unallocated space! } int * random. Num. Ptr() { int j= srand( time(0) ); return &j; } Never return a address of a stack-variable ! 31

Bug 3 void my. Func(char * input) { char * name; if (input != NULL ) { name = (char*)malloc(MAX_SIZE); strcpy(name, input); } … free(name); } if input is NULL => free on an address that was notallocated using malloc. 32

No bug 3 void my. Func(char * input) { char * name=NULL; if (input != NULL ) { name = (char*)malloc(MAX_SIZE); strcpy(name, input); } … free(name); } always initialize pointers to NULL! 33

Memory bugs �Use valgrind! �Self study �See the tutorial at the course website (under TA lectures). 34

Self Reading �Last slides of Ex 3 (from “enum” until the end) �Makefile �Valgrind 35