If possible, can anyone solve these 4 questions for me. New to C programming, so
ID: 3725285 • Letter: I
Question
If possible, can anyone solve these 4 questions for me. New to C programming, so I'm having a tough time solving these.
CSC-292 UNIX & C Programming HOMEWORK -C-string and Pointers What to turn in - this packet with all answers and 2-3 progroms in esc292 1. How many bytes are allocated? Is there anything wrong with this allocation? If so, how would you fix it? int* x = (int*) malloc(12); a) 12 bytes are allocated. The correct allocation should be... int* x - (int*) malloc(sizeof(12); 2. How many bytes are allocated? Is there anything wrong with this allocation? If so, how would you fix it? typedef st ruct chaz+ name; int age; float oving l Record; Record* me- (Record* ) malloc (sizeof ( Record*)); 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. /t pr pt the user for a series of ints. Print them out n reverse. */ # include # includeExplanation / Answer
1.ans
12 bytes are allocated because in malloc function we are passing the size as 12.
2ans.
Since in malloc function we are passing record pointer variable hence it will allocate either 4 or 8 bytes because pointers usually take either 4 or 8 bytes(For 32bit computer pointeer size will be 4 bytes and for 64bit computer it is 8 bytes).
The correct allocation should be like the following:
Record* re = (Record*)malloc(sizeof(*re))
now it will allocate the memory equal to size of structure Record which is 16 bytes.
3 a) ans.
The given code will run infinitely because scanf() will return the number of items successfully read and hence it will be independent from the size.
b) ans.
Modified code is following :
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int main(){
int* arr = (int*)malloc(SIZE*sizeof(int));
int temp,i=0;
while(i<SIZE)
{
scanf("%d",&temp);
arr[i]=temp;
i++;
}
i--;
for(;i>=0;i--)
{
printf("%d ",arr[i]);
}
free(arr);
return 0;
}
c) ans
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int main(){
int* arr = (int*)malloc(SIZE*sizeof(int));
int temp,i=0;
while(i<SIZE)
{
scanf("%d",&temp);
arr[i]=temp;
i++;
}
i--;
for(;i>=0;i--)
{
printf("%d ",arr[i]);
}
free(arr);
return 0;
}
4.a) ans
It will print garbage value because x is not initialized.
b)ans
It will give runtime error.Since memory has not allocated for x and yet we are derefrencing hence it will give segmentation fault.
c)ans
It will give runtime error because we are derefrencing a pointer which has assigned with NULL.
d) ans
It will print 4.
e) ans
Since we have allocated the memory of size 10 bytes and trying to access the data present at 6*sizeof(int)(24th byte) hence there will be two case first if this memory exist then it will print garbage value otherwise it will through runtime error as segmentation error.
f) ans
It will print 4.
g) ans
It will give segmentation fault because we are trying to modify pointer to char which is imutable.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.