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

Please read carefully Off-by-one errors. If you have an array of 10

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. # include # include #de fine SIZE 5 int main ) int arr [SIZE1 int temp int i-0; while (scanf"d", tep) > 0) /i now points past the last element. / Point to last / printf("%d ", arr [1]); return 0: c) Type the program and correct it as stated in b). Save your program as question3FirstlnitialLastName.c (this is the program 1 to be submitted to csc292)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!