Question: #include struct ArrayList { int capacity; int size; char* elements; }; int size(struct ArrayList *this) { return this->size; } void resize(struct ArrayList *this) { int

#include

struct ArrayList { int capacity; int size; char* elements; };

int size(struct ArrayList *this) { return this->size; }

void resize(struct ArrayList *this) { int newcap = 2 * this->capacity; char *newElements = malloc(newcap * sizeof(char)); for (int i=0; icapacity; i++) { newElements[i] = this->elements[i]; } this->capacity = newcap; this->elements = newElements; free(this->elements); }

void addLast(struct ArrayList *this, char elem) { if (this->size == this->capacity) { resize(this); } this->elements[this->size] = elem; this->size += 1; }

char get(struct ArrayList *this, int i) { return this->elements[i]; }

struct ArrayList* newArrayList(int startCap) { struct ArrayList *t = malloc(sizeof(struct ArrayList)); t->capacity = startCap; t->size = 0; t->elements = malloc(startCap * sizeof(char)); return t; }

void freeList(struct ArrayList *this) { free(this->elements); free(this); }

int main() { struct ArrayList* ls = newArrayList(1); addLast(ls, 'a'); addLast(ls, 'b'); addLast(ls, 'c'); freeList(ls); }

As the program executes, what will be the FIRST problem that will occur?

choices:

A.memory leak: a char array allocated in newArrayList is lost

B.memory leak: a char array allocated in resize is lost

C.memory leak: a char array allocated in main is lost

D.memory leak: a struct ArrayList is lost

E.memory error: in resize when dereferencing this

F.memory error: in resize when dereferencing a char array/pointer

G.memory error: in addLast when dereferencing this

H.memory error: in addLast when dereferencing a char array/pointer

I.no memory errors or leaks

J.invalid free: freeing an invalid pointer in resize

K.invalid free: freeing an invalid pointer in freeList

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!