-
October 29th, 2012, 12:07 AM
#1
Help needed for a pointer program
Hi,
Can anyone explain the below outputs of the program (with malloc statement and without malloc statement).
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *p;
p = (char *) malloc(10);
printf("%d\n", p);
printf("%d\n", *p);
printf("%d\n", sizeof(p));
printf("%d\n", sizeof(*p));
printf("%d\n", sizeof(&p));
return 0;
}
The outputs I got (using gcc 4.5 on linux):
without malloc:
----------------
madhu@Ideapad:~/Desktop$ ./a.out
-1217282060
124
4
1
4
With malloc:
---------------
madhu@Ideapad:~/Desktop$ ./a.out
138657800
0
4
1
4
Thanks in advance!
-
October 29th, 2012, 12:19 AM
#2
Re: Help needed for a pointer program
Without malloc, it looked like you did not initialise the pointer at all. As such, your attempt to print the value of *p resulted in undefined behaviour.
For the sizeof results: think about what you already know of sizeof. If you know nothing, then it is time to do some research.
By the way, remember to use free() after you are done with what you allocated with malloc().
-
October 29th, 2012, 12:23 AM
#3
Re: Help needed for a pointer program
Thanks for you reply.
This is an interview question which I faced recently so, that of posting here to get some more information.
can you explain each statement, how it will behave?
-
October 29th, 2012, 01:28 AM
#4
Re: Help needed for a pointer program
Yes, I can, but you should post your own best attempt explanation first.
-
October 29th, 2012, 01:58 AM
#5
Re: Help needed for a pointer program
I understood like below (comments inline):
without malloc:
----------------
madhu@Ideapad:~/Desktop$ ./a.out
-1217282060 // char *p is not pointing to anything so garbage is printing
124 // not sure about this output
4 // sizeof charater pointer is 4 bytes
1 // size of a charater is 1 byte
4 // same as size of charater pointer: 4 bytes
With malloc:
---------------
madhu@Ideapad:~/Desktop$ ./a.out
138657800 // char *p is not pointing to anything so garbage is printing
0 // not sure about this output
4 // sizeof charater pointer is 4 bytes
1 // size of a charater is 1 bytes
4 // same as size of charater pointer: 4 bytes
is it correct?
can you explain the second one, why it is printing 124 and 0 ?
-Thanks,
-
October 29th, 2012, 02:35 AM
#6
Re: Help needed for a pointer program
Originally Posted by madhu542
124 // not sure about this output
I already told you that dereferencing that pointer results in undefined behaviour. The output you get here is presumably whatever was at the "garbage" memory location, or it could be something else since no output was even required to begin with.
Originally Posted by madhu542
138657800 // char *p is not pointing to anything so garbage is printing
This is wrong: the pointer p does point to something since it was assigned the return value of malloc.
Originally Posted by madhu542
0 // not sure about this output
This just means that the first byte of the memory allocated by the use of malloc happened to contain a 0. It could have legitimately been some other value instead.
Other than these, yes, your observations appear correct to me.
-
October 29th, 2012, 04:51 AM
#7
Re: Help needed for a pointer program
Originally Posted by madhu542
I understood like below (comments inline):
without malloc:
----------------
madhu@Ideapad:~/Desktop$ ./a.out
-1217282060 // char *p is not pointing to anything so garbage is printing
For this line:
Code:
printf("%d\n", *p);
Your answer here is wrong if malloc() is not called. There is no guarantee that the program even works when that line is executed. When you dereference an uninitialized pointer, as laserlight stated, the program's behaviour is undefined. The program could blow up as soon as that line is executed.
(I know of one OS where this can happen, and that is the 16-bit MSDOS OS, where dereferencing uninitialized pointers would sometimes cause reboots of the PC to occur).
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|