Give an explanation, in complete English sentences, of the following concepts in
ID: 3723484 • Letter: G
Question
Give an explanation, in complete English sentences, of the following concepts in C code. The explanation should be sufficient for a classmate to understand the concept, even if they had not previously been exposed to it.
3) Suppose i is an int variable, and p and q are type pointer to int variables. For each expression, either explain what the given statement accomplishes, or indicate that the given statement is illegal, and why.
(a) p = &i;
(b) *p = i;
(c) *q = &i;
(d) p = q;
4) Explain what is wrong with the following function.
//This function takes in two integer pointers as parameters, and //swaps them.
void swap(int *x, int *y) {
int *t = x;
x = y;
y = t;
}
5) At the end of the following snippet of code, indicate how many bytes of dynamically-allocated memory are allocated to the program, and which pointers can safely be dereferenced. Assume that sizeof(int) is 4 bytes, and no allocation failures occur.
int size = 10;
int *a = malloc(size * sizeof(int));
int *b = realloc(a, size * 2 * sizeof(int));
6) Given the following struct:
struct data {
char *s;
double *t;
};
And assuming:
Pointers and doubles take 8 bytes each
Chars take 1 byte each
The struct is not "padded"
Describe how much memory is leaked by the following code, and suggest code to insert (including where it should be inserted) to fix the memory leak(s).
struct data *d = (struct data*)malloc(sizeof(struct data));
d->s = (char*)malloc(4 * sizeof(char) + 1);
d->t = (double*)malloc(4 * sizeof(double));
// (code using d)
free(d);
Explanation / Answer
Hi... Please check below.
3) Suppose i is an int variable, and p and q are type pointer to int variables. For each expression, either explain what the given statement accomplishes, or indicate that the given statement is illegal, and why.
(a) p = &i;
(b) *p = i;
(c) *q = &i;
(d) p = q;
Answer: b
*p=i; here the pointer directly refers to an integer variable. so it gives correct results.
p = &i; &i gives the address of the integer i and stored in p;
*q = &i; Here also &i gives the address of integer i to the pointer q.
p = q assigning q value to p.
4) Explain what is wrong with the following function.
//This function takes in two integer pointers as parameters, and //swaps them.
void swap(int *x, int *y) {
int *t = x;
x = y;
y = t;
}
Answer:
void swap(int *x, int *y) {
int *t = x;
x = *y;
y = *t;
}
We need to change y to *y in the x = *y; otherwise it takes the address of y to x.
5) At the end of the following snippet of code, indicate how many bytes of dynamically-allocated memory are allocated to the program, and which pointers can safely be dereferenced. Assume that sizeof(int) is 4 bytes, and no allocation failures occur.
int size = 10;
int *a = malloc(size * sizeof(int));
int *b = realloc(a, size * 2 * sizeof(int));
Answer:
int *a = malloc(size * sizeof(int));
For the above line pointer a has 40 bytes got assigned.
int *b = realloc(a, size * 2 * sizeof(int));
pointer b is dereferenced to a and 80 bytes got memory allocated.
6) Given the following struct:
struct data {
char *s;
double *t;
};
And assuming:
Pointers and doubles take 8 bytes each
Chars take 1 byte each
The struct is not "padded"
Describe how much memory is leaked by the following code, and suggest code to insert (including where it should be inserted) to fix the memory leak(s).
struct data *d = (struct data*)malloc(sizeof(struct data));
d->s = (char*)malloc(4 * sizeof(char) + 1);
d->t = (double*)malloc(4 * sizeof(double));
// (code using d)
free(d);
Answer: There was totally 32+33 bytes memory gor allocated to the struct data pointer d.
struct data *d;
d->s = (char*)malloc(4 * sizeof(char) + 1);
d->t = (double*)malloc(4 * sizeof(double));
We can directly assign memory to its sub variables.
Please check the above and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.