Question:
malloc(sizeof(0)) will return — valid pointer
Answer:
sizeof(0) returns the size of integer whose value is 0. Hence malloc would allocate sizeof(int) bytes and return a valid pointer.
Submitted by Kokila21 ([email protected]) Source: CoolInterview.com
It is equivalent to free() and it returns valid pointer. Source: CoolInterview.com
Answered by: Umesh | Date: 12/25/2008
| Contact Umesh
malloc() Function always returns Void *(void pointer),we've to explicitly Typecast ..... Source: CoolInterview.com
Answered by: manik | Date: 3/19/2009
| Contact manik
And here is an example: int *p; p = (int*)malloc(sizeof(0)); if ( p == NULL ) printf("Invalid Pointer"); else printf("Valid Pointer"); Source: CoolInterview.com
Answered by: manish | Date: 5/9/2009
| Contact manish
umesh, i think it is partially correct.
malloc always returns (void *).
thats y, when u want to dynamically allocate memory in heap,
int *p = (int *)malloc(sizeof(int) *10); Here TYPECAST "int *" is must. So answer is malloc always return "void *". Source: CoolInterview.com
Answered by: Murali Prasath | Date: 9/4/2009
| Contact Murali Prasath
int *p = (int *)malloc(sizeof(int) *10);
here inside malloc sizeof means reserve 4 bytes of memory for each data as it is of int type and *10 means resserve 10 such locations... also p here is the pointer to the starting address of the 40 byte of space that been reserved in the Heap memory. Source: CoolInterview.com
Answered by: Rohit Salecha | Date: 4/15/2010
| Contact Rohit Salecha
int *p = (int *)malloc(sizeof(int) *10);
here sizeof(int) captures 4 bytes of int data types and *10 means 10 such memory blocks in all 40 bytes to be allocated in the memory whose starting address is to be given to p. Source: CoolInterview.com
Answered by: Rohit Salecha | Date: 4/15/2010
| Contact Rohit Salecha
int *p = (int *)malloc(sizeof(int) *10);
here sizeof(int) captures 4 bytes of int data types and *10 means 10 such memory blocks in all 40 bytes to be allocated in the memory whose starting address is to be given to p. Source: CoolInterview.com
Answered by: Rohit Salecha | Date: 4/15/2010
| Contact Rohit Salecha
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|