C Programming - Memory Allocation - Discussion

Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 7)
7.
How many bytes of memory will the following code reserve?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(256 * 256);
    if(p == NULL)
        printf("Allocation failed");
    return 0;
}
65536
Allocation failed
Error
No output
Answer: Option
Explanation:

Hence 256*256 = 65536 is passed to malloc() function which can allocate upto 65535. So the memory allocation will be failed in 16 bit platform (Turbo C in DOS).

If you compile the same program in 32 bit platform like Linux (GCC Compiler) it may allocate the required memory.

Discussion:
9 comments Page 1 of 1.

Aadesh said:   9 years ago
16 bit is a compiler not memory which basically related to processor that upto which byte the processor can access the memory. Therefore the memory must get allocated.

V1ron said:   10 years ago
Incorrect question. No one uses DOS nowadays. There may be a bunch of platforms with individual limits.

JennT said:   1 decade ago
Yes, with 32 bit memory addresses both [A] and [D] options can be correct, if you're considering machine/platform dependent constraints give us a little context.

Sandi said:   1 decade ago
Please mention it is 16 bit platform, otherwise 4th option can be correct.
(1)

Kalle said:   1 decade ago
Please remove this absurd question! (Or tell us you mean a 16 bit platform).

We already had to assume the memory was available.

Sudheer said:   1 decade ago
So the options should be like compiler dependent or system dependent ?

Otherwise it will allocate the memory (if system is 32 bit or higher) , so it should be A and not B as it is NOT true for all cases.
(2)

Lav said:   1 decade ago
@Cherry, // TurboC++(16-bit).

But for p=(int *)malloc(65534); also o/p: Allocation failed.

Cherry said:   1 decade ago
In 16-bit platform the range is 0 to 65535 but here p gives 65536. So that p=NULL. Then it prints if statement.

I hope you understand.

Ravi said:   1 decade ago
Why this is not producing anything in gcc compiler also ?

#include<stdio.h>
#include<stdlib.h>

int main()
{
int *p;
p = (int *)malloc(256 * 250);
if(p == NULL)
printf("Allocation failed");
return 0;
}

Even output screen vanishes ?

Post your comments here:

Your comments will be displayed after verification.