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; i
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
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