Help needed for a pointer program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Help needed for a pointer program

  1. #1
    Join Date
    Jun 2012
    Posts
    9

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,244

    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().
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2012
    Posts
    9

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,244

    Re: Help needed for a pointer program

    Yes, I can, but you should post your own best attempt explanation first.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2012
    Posts
    9

    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,

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,244

    Re: Help needed for a pointer program

    Quote 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.

    Quote 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.

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Help needed for a pointer program

    Quote Originally Posted by madhu542 View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center