Question

int *fun (int *x)f int value *xtt return &value; int main) int pl; int **p2; int a; char str [5] printf (Enter a number:) s

Construct a table that shows for each line of code, what may be the memory related problem/error and suggest a correction for

please help me with this question

(in C programming language and Unix)
computer system book 3 edition

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Error in:
scanf("%d", a);
with scanf, you need to pass the address of variable, not the variable itself.. So this may cause crash in program. The correct code will be:
scanf("%d", &a);

Error: gets(str);
This does not give a compile time error, but may result in a runtime error. The gets function, keeps on reading the line from console, until a new line is found. Now str has just memory for 5 characters, so if used enters string of more than 5 characters, then program may crash. The correct code will be:
fgets(str, 4, stdin);  // leaving one character for '\0'

Error: *p1 = p2;
p1 is a pointer to int, while p2 is a pointer to pointer to int. So p2 is basically an address, which should not be kept in integer variable.
Correct code: *p1 = **p2;

Error: p2 = malloc(sizeof(int));
p2 is a pointer to pointer to int, while it allocates memory for only an integer.. which is 
wrong.
Correct code: p2 = malloc(sizeof(int *));

Error: p1 = fun(a);
You are getting some other integer's address from outside, but the int which got allocated in main function, that reference will get miss after this statement.. hence before executing this statement, we should free memory pointed by p1.
Correct: free(p1); p1 = fun(a);

Error: for loop
We are using a integer pointer to traverse the character array which is wrong.. 
Correct Code: Use a character pointer.
Add a comment
Know the answer?
Add Answer to:
Please help me with this question (in C programming language and Unix) computer system book 3 edi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could someone please help me with this C language code I'm confused as to why i'm...

    Could someone please help me with this C language code I'm confused as to why i'm getting an error every time I run it on command line if some could please help me as soon as possible. #include <stdio.h> #include <stdlib.h> int main() { char *ptr_two = (char *)malloc(sizeof(char)*50); printf("%p\n", ptr_two); ptr_two = "A constant string in C"; printf("%p\n", ptr_two); printf("%s\n", ptr_two); free(ptr_two); return 0; }

  • Write a function that can return the length of a string in C language. Please leave...

    Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) {     /write your code here } main(){     int len;     char str[20];     printf("Input string:");     scanf("%s", str);     len = length(str);     printf("The length of string is %d. ", len);     getchar();    ...

  • Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z')...

    Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z') return c-32; return c; } //converting sentance to upper void convertSentence(char *sentence){ int i=0; while(sentence[i]!='\0'){ //convert to upper for each character sentence[i] = toUpper(sentence[i]); i++; } } //converting all sentences into uppercase void convertAll(char **sentenceList, int numOfSentences){ int i=0; while(i<numOfSentences){ //calling convertsentence function to conver uppercase convertSentence(sentenceList[i]); i++; } } sentences.c #include<stdio.h> #include<stdlib.h> #include "convert.c" int main(){ //declaring character array char **mySentences; int noOfSentences;...

  • The following code is a C Program that is written for encrypting and decrypting a string....

    The following code is a C Program that is written for encrypting and decrypting a string. provide a full explanation of the working flow of the program. #include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...

  • C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char...

    C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char firstname[16]; char lastname[16]; printf("please enter your first name:"); scanf("%s",firstname); printf("please enter your last name:"); scanf("%s",lastname); PrintName(firstname,lastname); return 0; } void PrintName(char firstname[16], char lastname[16]){ char fullname[34]; *fullname=*firstname+*lastname; (char*) malloc (sizeof (*fullname)); if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16) fflush(stdin); else printf(" the full name is %s %s \n",firstname,lastname); printf(" the full name is-> %s",fullname);/*why is one not run*/ return 0; }

  • System Programming in C

    Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include  #include  #include  #include  int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • QUESTION 1 In order to print the members of structures in the array, what will be...

    QUESTION 1 In order to print the members of structures in the array, what will be the contents of blanks in the printf statement of the code snippet given below? #include <stdio.h> struct virus {    char signature[25];       char status[20]; } v[2] = {                               "Yankee Doodle", "Deadly",                               "Dark Avenger", "Killer"                   } ; void main( ) {       for (int i = 0; i < 2; i++)                   printf( "\n%s %s", _______________ , _____________ ); } A. signature, status B. v.signature,...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT