Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2021 Workshops Biokdd Iwcfs Mlkgraphs Al Cares Protime Alsys 2021 Virtual Event September 27 30 2021 Proceedings

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Atif Mashkoor ,Johannes Sametinger ,Anna Fensel ,Jorge Martinez-Gil ,Lukas Fischer

1st Edition

3030871002, 978-3030871000

More Books

Students also viewed these Databases questions

Question

2. To store it and

Answered: 1 week ago