Question.7
A firmware engineer needs to create/destroy network packet objects frequently. Which approach avoids fragmentation?
Option A -- Heap:
Packet* p = new Packet();
// ... use ...
delete p;Option B -- Fixed pool:
static Packet pool[MAX_PACKETS];
static bool used[MAX_PACKETS] = {false};
Packet* alloc() {
for (int i=0; i<MAX_PACKETS; i++)
if (!used[i]) { used[i]=true; return &pool[i]; }
return nullptr;
}