C Programming :: Dynamic Memory Allocation

NA
SHSTTON
1
Solv. Corr.
0
Solv. In. Corr.
1
Attempted
0 M:0 S
Avg. Time

11 / 18

Which of the following is not an advantage of secondary memory


AIt is cost-effective

BIt has large storage capacity

CIt has highest speed

DIt is easily portable

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
1
Solv. In. Corr.
1
Attempted
0 M:0 S
Avg. Time

12 / 18

What happens when a pointer is deleted twice?


AIt can abort the program

BIt can cause a failure

CIt can cause an error

DIt can cause a trap

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

13 / 18

What will be result of the following program?
#include<stdio.h>
#include<malloc.h>
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;
}

ARun time error/Core dump

BThe string is: String

CThe string is: Oldstring

DSyntax error during compilation

ENone of these

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

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

14 / 18

What will the following program do?
#include<stdio.h>
#include<string.h>
#include<malloc.h>
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

ASwap contents of p and a and print

BGenerate compilation error in line number 8

CGenerate compilation error in line number 5

DGenerate compilation error in line number 7

EGenerate compilation error in line number 1

Answer: Option B

Explanation:


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


Workspace

NA
SHSTTON
0
Solv. Corr.
1
Solv. In. Corr.
1
Attempted
0 M:0 S
Avg. Time

15 / 18

Which of the following statements is correct.
x = malloc (y).

Ax is the size of the memory allocated

By points to the memory allocated

Cx points to the memory allocated

DNone of the above

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

NA
SHSTTON
0
Solv. Corr.
1
Solv. In. Corr.
1
Attempted
0 M:0 S
Avg. Time

16 / 18

Choose the correct option.

alloca() allocates memory from


AHeap

BReserved memory

CStack

DData Segement

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.

Workspace

NA
SHSTTON
5
Solv. Corr.
6
Solv. In. Corr.
11
Attempted
0 M:0 S
Avg. Time

17 / 18

What will be output of the following "c" code?
FUNC (int *p)
 {
  p = (int *)malloc(100);
  printf("p:%x",p);
 }

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

ABoth printf statements prints same values

BBoth print different values

CGives compile time error

DGives run time error

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

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
1
Solv. In. Corr.
1
Attempted
0 M:0 S
Avg. Time

18 / 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;
}

Ahello_World

Bello_World

Clo_World

Dllo_World

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