Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3727408 • Letter: P

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 memory violation. (Remember, element [9] is the last element of a 10- element array!). This can be as simple as having a <= when you meant < in a ‘for loop’.

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', ''}, 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)

Explanation / Answer

#include<stdlib.h>

#include<stdio.h>

#define size 100000

/* it is better to declare the largest number here..so that the conflict can not occur if

the user have typed more than the given size..! if we defined the size as 100000 and if

we give only 5 inputs..then the rest of the memory which we have allocated for the array will be wasted.

and this can be avoided by allocating the memory dynamically and free it after the end of the program. */ or

int main()

{

int arr[size];

int temp;

int i=0;

while(scanf("%d",&temp)>0)

{

arr[i]=temp;

i++;

}

i--;

for(;i>=0;i--)

{

printf("%d ",arr[i]);

}

return 0;

}

/*b. the above code can be modified as*/

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *array[]) {

int *ptr_int = NULL; // ptr_int is the pointer to integer

int num = 0; //number of elements in the array

int index = 0; //iterator

scanf("%d",&num); //Get input for number of elements in the array

ptr_int = (int *)malloc(num * sizeof(int));

/*allocating the memory dynamically..and the dynamically allocated memory is stored in heap

where as the local variables and the non-static variables are stored in stack */

//Check the validity of the memory.. if the memory is not allocated it will return 1

if(ptr_int== NULL)

{

return 1;

}

//copy the index of array 1D

for (index =0 ; index < num ; index++)

{

scanf("%d",&ptr_int[index]);

/*getting the elements of the array from the user in the loop.this loop runs till the num which

denotes the number of elements in the array*/

}

//print the data

for (index =0 ; index< num ;index++)

{

printf("%d ", ptr_int[index]);

/* print all the elements*/

}

free(ptr_int); // free allocated memory

// the allocated memory must be freed by the programmar or else it will lead to the memory leakage

//Memory leak occurs when programmers create a memory in heap and forget to delete it.

return 0;

}

/* allocating memory

for string */

#include<stdio.h>

#include<stdlib.h>

int main()

{

char str[100];

//character string;

char *address;

int size=0,count;

//size is used to find the length of the given string

// the count variable is used to include the null character that is ''

scanf("%[^ ]s",str);

for(int i=0;str[i]!='';i++)

{

size++;

}

/*if the input is "program" then the size is 7 and it does not include the null character

+-+-+-+-+-+-+-+-+

program--->|p|r|o|g|r|a|m|0|

+-+-+-+-+-+-+-+-+

in order to include the null character i used count here (i.e) size =7, count=size+1 (8) */

count=size+1;

address = (char *)malloc(sizeof(char)*count);

/*dynamically allocating the memory (i.e) (sizeof(char) * count ) size of char is 1 and the count is 8 so it is 8 */  

printf("%s",str);

//printf("%d",count);

return 0;

}

/*the another approach is*/

/*C program to input and print text

using Dynamic Memory Allocation.*/

#include <stdio.h>

#include <stdlib.h>

int main()

{

int n;

char *text;

  

scanf("%d",&n);

//enter the limit of the text

/*allocating the memory dynamically*/

text=(char*)malloc(n*sizeof(char));

  

scanf(" "); //clearing the input buffer

gets(text);

printf(" %s",text);

//printing it

/*Free the Memory*/

free(text);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote