The behavior of the program.

Discussion in 'C Programming' started by somenath, Feb 21, 2014.

  1. somenath

    somenath Guest

    The output of the following program is not clear to me.

    #include<stdio.h>
    #include<stdlib.h>

    void Get_Array_Memory(int (*ar)[5])
    {
    int i =0;
    int (*temp)[5];
    printf("\nar before assignment = %p\n",(void *)ar); //print 3
    temp= malloc(5);
    if ( temp ) {
    printf("\ntemp = %p\n",(void *)temp);
    ar = temp;
    printf("\nar after asignment = %p\n",(void *)ar); //print 4
    for ( i =0;i < 5; i++ ) {
    *(*ar + i ) = i;
    }
    }

    }

    int main(void)
    {

    int (*arr)[5];
    int a[5] ;
    int i=0;
    arr = &a;
    printf("\narr in main = %p\n",(void *)arr); //print 1
    Get_Array_Memory(arr);
    printf("\narr in main after function call = %p\n",(void *)arr); //print 2
    for ( i =0;i < 5; i++ ) {
    printf(" val of ar = %d\n",i ,*(*arr + i)); //print 5
    }

    return 0;
    }

    The output
    ++++++++++++++++++++++
    arr in main = 0x28ac44

    ar before assignment = 0x28ac44

    temp = 0x800482c0

    ar after asignment = 0x800482c0

    arr in main after function call = 0x28ac44
    val of ar = 0
    val of ar = 1
    val of ar = 2
    val of ar = 3
    val of ar = 4
    ++++++++++++++++++++++++++++++
    It is clear that ar in Get_Array_Memory assigned to new address 0x800482c0which is different than 0x28ac44 passed from main. Then how the values assigned in Get_Array_Memory is retained and the same values are getting printed in main? Is it just by chance I am getting the output in main?
     
    somenath, Feb 21, 2014
    #1
    1. Advertisements

  2. somenath

    Eric Sosman Guest

    Are you sure five bytes is enough memory for an array of
    five ints? That is, are you sure sizeof(int)==1? ;-(

    This is one reason to prefer the oft-recommended pattern

    temp = malloc(sizeof *temp);
    Note that this assignment changes the value of the parameter
    `ar', but changes nothing at all in the function's caller.
    Unless sizeof(int)==1, this loop will attempt to store outside
    the bounds of the memory obtained from malloc(). At that point the
    C language no longer governs what happens; "the behavior is undefined."


    Get_Array_Memory() has undefined behavior if sizeof(int)>1.
    If sizeof(int)==1 Get_Array_Memory is all right but the undefined
    behavior begins with this loop, which attempts to use the values
    of the elements of `arr'. Those elements have automatic storage
    duration and have never been stored to, so their values are
    "indeterminate" (often called "garbage"), and attempting to use
    the values causes undefined behavior.


    Maybe not "by chance," since the outcome may well be the same
    each time you run the program. However, since the program's
    behavior is undefined (one way or another), "chance" is a fairly
    good description of anything it happens to do.
     
    Eric Sosman, Feb 21, 2014
    #2
    1. Advertisements

  3. somenath

    somenath Guest

    Yes . This is my mistake. It should have been 5 *sizof(int).Or
    temp = malloc(sizeof *temp);

    This the main thing I wanted to understand. Thanks.


    Got it . Thanks.
     
    somenath, Feb 21, 2014
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.