Question: ` ` ` p = malloc ( n ) reserves enough space, in the heap, for n char values; p points at it . /

```
p = malloc(n) reserves enough space, in the heap, for n char values; p points at it.
// make_str(ch, n) Return a new array containing a int main(void){
// string containing n copies of ch. char *str = make_str('X',4);
// effects: allocates heap memory [caller must free]// TRACE:2
char *make_str(char ch, int n){ assert(0== strcmp(str,"XXXX"));
char *p = malloc(n +1); // allocate n+1 chars. }
for (int i =0; i n; ++i)// write to them.
p[i]= ch;
p[n]='\0'; // null-terminate the string
// TRACE:1
return p;
}
```
Draw memory diagrams at TRACE:1 and TRACE:2.
` ` ` p = malloc ( n ) reserves enough space, in

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 Programming Questions!