Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How Do I Use A Database Research Tools You Can Use

Authors: Laura La Bella

1st Edition

1622753763, 978-1622753765

More Books

Students also viewed these Databases questions

Question

Define Scientific Management

Answered: 1 week ago

Question

Explain budgetary Control

Answered: 1 week ago

Question

Solve the integral:

Answered: 1 week ago

Question

What is meant by Non-programmed decision?

Answered: 1 week ago

Question

Analyze the impact of labor unions on health care.

Answered: 1 week ago

Question

Assess three motivational theories as they apply to health care.

Answered: 1 week ago

Question

Discuss the history of U.S. labor unions.

Answered: 1 week ago