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
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);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started