0
Why this code returns 10 as an output
#include <stdio.h> int * assignval (int *x, int val) { *x = val; return x; } int main() { int *x = malloc(sizeof(int)); if (NULL == x) return; x = assignval(x, 0); if(x) { x = (int*) malloc(sizeof (int)); if (NULL == x) return; x = assignval (x, 10); } printf("%d\n", *x); free(x); }
3 Réponses
+ 1
Thanks ~ swim ~
0
my doubt is that in line number 6 there is no type conversion (void* to int*) then answer should be compiler error as the return of malloc is not typecast appropriately. or maybe in last line , after free up the space(pointed by space) there is no initialization to Null. This might be result in dagling(this reson lead to dangling pointer ). and next in line 6 call the malloc function ND without free up it again call the malloc function in line number 11.(this reson might be lead to correct compiles successfully but execution may result in memory leak.) . where I m wrong. ND why this give me 10 as output any proper explanation of the snippets.
compiler error because the comparison should be made as x==NULL and not as shown.
so why this code running and returning 10
can anyone explain what is going on every line here