Question
Please read carefully Off-by-one errors. If you have an array of 10 elements, and you write to elements 0 through to 10, you've caused a
Please read carefully
Off-by-one errors. If you have an array of 10 elements, and you write to elements 0 through to 10, you've caused a memory violation. (Remember, element [9] is the last element of a 10- element array!). This can be as simple as having a
A special case of an off-by-one error - forgetting to allocate an extra byte in a string, for the null byte. For example, if you allocate 5 bytes, then strcpy the string "hello" into an array, it will write six bytes, {'h', 'e', 'l', 'l', 'o', '\0'}, causing a memory bug.
Allocating too little memory. A common mistake using malloc is to forget to multiply by sizeof). For instance, if you allocate an array of 12 ints, using int* x = (int*) malloc(12), you will allocate 12 bytes. Writing to elements 0, 1 and 2 will be fine, but any more past that and you're in trouble. This can be a triple cause-effect chain - the bug is caused by your malloc, the actual memory violation doesn't happen till you write to the array, and the actual crash or effect may not be felt until much later!
Casting a pointer to the wrong type. For example, what happens if you, for some reason, cast an int* over to a List* (List being a linked list structure you declared)? Since the List is bigger than an int, accessing its elements will be writing to bad memory locations.
Treating a pointer-to-one-object as an array. Which of these code snippets cause segmentation faults? Which ones don't? (What do they do instead?) Which ones can gcc detect?
Look at the following complete C program a) b) 3. What happens if the user types more than 5 numbers? Modify the code to use malloc/realloc to create an expanding array as necessary. Free the array only when you're done Prampt the user for a series of ints Print them out in reverse. # includeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started