0x0A. C - malloc, free

Ace your homework & exams now with Quizwiz!

What is Valgrind?

A programming tool used for memory debugging, mem leak detection and profiling.

What is the most important thing to remember when allocating memory with malloc()?

Because the memory space has been allocated manually, it will always be unavailable unless it is manually freed with the function free(). Therefore it is important to track all variables whose memory was allocated with malloc(). A larger program unmanaged may run out of memory if you haven't freed a malloced variable after it is done being used.

I'm what ways are initializing a string with char *str = "Holberton" and char *str = malloc( 10 * sizeof(char)) the same and how are they diff?

Both must have their memory freed manually when finished. While char *str = "Holberton" is not mutable, *str = malloc(10 * sizeof(char)) is mutable.

Why is it better practice to use sizeof() when allocating memory with malloc?

Diff machines may have diff byte sizes for data types. While an int is normally 4 bytes, it is safer to use sizeof() in case the program is running on a machine where int is a size other than 4.

What is good practice to ensure your program doesn't hit any seg faults?

When there's an error, malloc's value is returned as NULL. To avoid future problems, always check to see what value your malloc returns so you know it isn't NULL.

What is size_t?

a data type that stores only positive int values (unsigned) used to allocate a variable with a specific block of memory. e.g. str = malloc(56).

What is the diff. between declaring a string with char *s = "Holberton" and char s[10] = "Holberton\0"?

char *s = "Holberton" is a pointer to a memory location. It isn't an array. The array is in the memory location that it points to. Therefore the value that it points to is read only and can't be changed. Changing a character will compile, but will return a seg fault. char s[10] = "Holberton\0" creates a copy of itself. While the original is immutable, the copy is mutable.

Show an example of how to error handle malloc so that a memory overflow is reported through standard output instead of having the program hit a seg fault?

char *s; int i = 0; while(1) { s = malloc(INT_MAX); if (s = NULL) { printf("Cant't allocate %d bytes (after %d calls)\n", INT_MAX, i); } s[0] = 'H'; i++; }

How would you error handle the value *str if it's value is NULL?

char *str; if (str == NULL) { printf("Not enough memory left!\n"); }

How would you allocate memory space for the string "Hello"?

either: char *str = malloc(6); or char *str malloc(6 * sizeof(char)). The second method is preferable.

What does using the free function on a pointer *ptr do and what is its syntax?

free: returns allocated memory back to the os once it isn't needed anymore. free(ptr) freed the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc (), or realloc ()

Give an example of how to set malloc for a char array (string) with a length of 5. Also explain how many bytes in memory the variable and string are before and after malloc. Finally, how many bytes in memory does the array itself (not its pointer) use and why?

int n = 5; char *ar; ar = malloc (n * sizeof(char); Memory Before: n: 4 bytes ar: 8 bytes After: n: 4 bytes ar: 80 bytes If the string is 4 chars long, the output will use exactly 5 bytes (4 + 1 for the null terminator)

What does malloc do and what is its syntax?

malloc(size_t) allocates size bytes and returns a pointer to the allocated (in-initialized) memory.

in the code: char *str; char = "Holberton"; what memory locations, sizes and variables are created? After this code, what happens when you add an '!' To the end? Why? What happens to the value stores in the location that *str points to?

str size: 8 because it is a pointer. bar name: str value: 40 (the men loc it points to) since str is only the pointer and it's size is 8 (all pointers are 8 bytes), the actual memory location that str points to (40) is stored as an array named "Holberton" of size 10 with a value of mem loc 40: H mem loc 41: o mem loc 42: l mem loc 43: b mem loc 44: e mem loc 45: r mem loc 46: t mem loc 47: o mem loc 48: n mem loc 49: \0 Adding another character to this string won't work because it is read only. After the pointers memory is released, the value that is stored where the pointer pointed is not released and remains in memory. It must be freed using the free() function.


Related study sets

Intro into sports science Final Exam

View Set

SHERPATH: Acute Coronary Syndrome

View Set

N151 Final Exam NCLEX Questions (No Rationale)

View Set