Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.


Please wait...

C Programming :: Dynamic Memory Allocation

Home > C Programming > Dynamic Memory Allocation > General Questions

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace


Tags:  No Tags on this question yet!

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace


Tags:  No Tags on this question yet!

13. What will be result of the following program?

#include
#include
int myalloc(char *x, int n){
x= (char *)malloc(n*sizeof(char));
memset(x,\0,n*sizeof(char));
}
int main(){
char *g="String";
myalloc(g,20);
printf("The string is %s",g);
return 0;
}

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option B

Explanation:

It will print The string is: String .

in this program char pointer g is passed instead of address of it. so that memset in myalloc will not impact.

Because of it, it will print The string is String.

Workspace


Tags:  TCS

14. What will the following program do?

#include
#include
#include
int main(){
int i;
char a[]="String";
char *p="New Sring";
char *Temp;
Temp=a;
a=malloc(strlen(p) + 1);
strcpy(a,p); //Line no:9//
p = malloc(strlen(Temp) + 1);
strcpy(p,Temp);
printf("(%s, %s)",a,p);
free(p);
free(a);
return 0;
}

Chose correct option

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option B

Explanation:


malloc allocation need to be assigned to char pointer instead of char array. a=malloc(strlen(p) + 1);


Workspace


Tags:  TCS

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option C

Explanation:

malloc() function allocates size bytes and returns a pointer to the allocated memory.  The memory is not initialized.  If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc(), or realloc().  Otherwise, or if free(ptr) has already been called before, undefined behavior occurs.  If ptr is NULL, no operation is performed.


Workspace


Tags:  No Tags on this question yet!

16. alloca() allocates memory from

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option C

Explanation:

The alloca() function allocates size bytes of space in the stack frame of the caller.  This temporary space is automatically freed when the function that called alloca() returns to its caller. The alloca() function returns a pointer to the beginning of the allocated space.  If the allocation causes stack overflow, program behavior is undefined.

If you are using alloca function, then you need to include below library in your program.

       #include <alloca.h>


Prototype of alloca:
       void *alloca(size_t size);

Workspace


Tags:  No Tags on this question yet!

17. Output of the code?
FUNC (int *p)
{
p = (int *)malloc(100);
printf("p:%x",p);
}

int main( )
{
int *ptr;
FUNC(ptr);
printf("Ptr:%x",ptr);
return 0;
}

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace


Tags:  Global Edge

18. What should the program below print?

void myfunc(char** param){
++param;
}
int main(){
char* string = (char*)malloc(64);
strcpy(string, "hello_World");
myfunc(&string);
myfunc(&string);
printf("%s\n", string);
return 0;
}

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option A

Explanation:

In this program first we are allocating 64 byte dynamically and assigned to string.

in second statement we are calling myfunc where we are passing the address of string.

in function myfunc we are receiving in double pointer (**param).

i.e param will point to the address of string. if you print address of string

printf("%u\n",&string) and variable param like printf("%u\n", param), both address will be same

in this function statement ++param will increment the address containing by param,

when you try to print string in main function it will print hello_world instead lo_World. if you change the statement in myfunc like ++(*param), it will print lo_World.



Workspace


Tags:  TCS