in Technology by
What is the return value of malloc (0)?

1 Answer

0 votes
by
If the size of the requested space is zero, the behavior will be implementation-defined. The return value of the malloc could be a null pointer or it shows the behavior like that size is some nonzero value. So you must never use the malloc(0) in your C program. Let see an example C program, where I am allocating memory using the malloc with size 0 #include #include int main (void) { int *piBuffer = NULL; //allocating memory using //the malloc with size 0. piBuffer = malloc(0); //make sure piBuffer is valid or not if (piBuffer == NULL) { // allocation failed, exit from the program fprintf(stderr, "Out of memory!\n"); exit(1); } *piBuffer = 10; printf("%d\n",*piBuffer); free(piBuffer); return 0; } Output: Implementation-dependent.

Related questions

0 votes
    What is the return value of malloc (0)?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    Is it better to use malloc () or calloc ()?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    What is the difference between malloc and calloc?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    Distinguish between malloc() & calloc() memory allocation?...
asked Jan 17, 2021 in Technology by JackTerrance
+1 vote
    Distinguish between malloc() & calloc() memory allocation....
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    I am observing the following behavior in my test program: I am doing malloc() for 1 MB and then ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    What will be the output of the following Python program? def foo(x): x[0] = ['def'] x[1] = ['abc'] return id(x) q = ['abc ... (id(q) == foo(q)) a) Error b) None c) False d) True...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python program? def foo(x): x[0] = ['def'] x[1] = ['abc'] return id(x) q = ['abc ... (id(q) == foo(q)) a) Error b) None c) False d) True...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    FIbonacci Series Program function fibonacciSequence(input) { var a=0;b=1;c=0; var array1=[0,1]; for(var i=2;i...
asked Feb 24, 2021 in Technology by JackTerrance
0 votes
    What is the output of the below program? #include int main() { printf("%u",sizeof(NULL)); return 0; }...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    What does the expression float a = 35 / 0 return? a) 0 b) Not a Number c) Infinity d) ... ;Reference: stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    FIbonacci Series Program function fibonacciSequence(input) { var a=0;b=1;c=0; var array1=[0,1]; for(var i=2;i<= ... [endif]--> a) True b) False Read Loans and Insurance questions...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    int fun(int n) { if(n!=0) { return n – fun(n-5); } else { return n; } } int main(){ int n = 20, z; z = fun(n); printf(“%d”, z); } Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    int find(int i) { if(>1) j=find(j/10)+(j%10) else j=0; return j; void main() int i=1000 int k k=find(i): printf(“%d”, K) Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
...