Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Normally variables are placed on the ( stack / heap ) by the compiler. Dynami

ID: 3709581 • Letter: 1

Question

1) Normally variables are placed on the ( stack / heap ) by the compiler. Dynamic variables are instead placed on the ( stack / heap ) by the                                     at the programmer’s request. In C++ this dynamic allocation is done via the operator                              (or the                    form for a dynamically allocated array). When we are done with dynamic memory, we must release it. In C++ this is done via the operator                         (or its array form                            ).

2) Given the following code, explain what it does.

const char * mysterious(const char * s, char f, long & w)

{

const char * p = s;

while (*p != f && *p != ’’)

{

p++;

}

w = (*p != ’’ ? p-s : -1);

p = (*p != ’’ ? p : NULL);

return p;

}

A) calculates the length of the C-string — s — returning this in w and a pointer to the null character in p

B) (linear) searches a C-string — s — for a certain character — f — returning (via w) the offset if found or -1

if not and the address (via p) if found or NULL if not

C) (binary) searches a C-string — s — for a certain character — f — returning (via w) the offset if found or -1

if not and the address (via p) if found or NULL if not

D) sorts the C-string — s — into descending or ascending order based on whether w is -1 or not

E) validates the theories espoused by various religious sects of the Disc World that no good deed goes unpun-

ished and no good punishment is truly needed — most of the code is actually syntactic sugar, as such

Explanation / Answer

Hi Dear,

Please find my answer.


1) Normally variables are placed on the stack by the compiler. Dynamic variables are instead placed on the heap by the operating system at the programmer’s request. In C++ this dynamic allocation is done via the operator new (or the form for a dynamically allocated array). When we are done with dynamic memory, we must release it. In C++ this is done via the operator delete (or its array form

2)
B) (linear) searches a C-string — s — for a certain character — f — returning (via w) the offset if found or -1 if not and the address (via p) if found or NULL if not).