Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

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

Step: 3

blur-text-image

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 Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions

Question

6-15 List and describe the limitations to using big data.

Answered: 1 week ago

Question

9. Mohawk Industries Inc.

Answered: 1 week ago

Question

8. Satyam Computer Services Limited

Answered: 1 week ago