The behavior of the program.

S

somenath

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?
 
E

Eric Sosman

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);

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);
if ( temp ) {
printf("\ntemp = %p\n",(void *)temp);
ar = temp;

Note that this assignment changes the value of the parameter
`ar', but changes nothing at all in the function's caller.
printf("\nar after asignment = %p\n",(void *)ar); //print 4
for ( i =0;i < 5; i++ ) {
*(*ar + i ) = i;
}

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."
}

}

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
}


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.
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 0x800482c0 which 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?


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

somenath

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);



Are you sure five bytes is enough memory for an array of

five ints? That is, are you sure sizeof(int)==1? ;-(
Yes . This is my mistake. It should have been 5 *sizof(int).Or
temp = malloc(sizeof *temp);
This is one reason to prefer the oft-recommended pattern



temp = malloc(sizeof *temp);


if ( temp ) {
printf("\ntemp = %p\n",(void *)temp);
ar = temp;



Note that this assignment changes the value of the parameter

`ar', but changes nothing at all in the function's caller.


printf("\nar after asignment = %p\n",(void *)ar); //print 4
for ( i =0;i < 5; i++ ) {
*(*ar + i ) = i;



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


}

int main(void)

int (*arr)[5];
int a[5] ;
arr = &a;
printf("\narr in main = %p\n",(void *)arr); //print 1

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




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.

This the main thing I wanted to understand. Thanks.
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 0x800482c0 which is different than 0x28ac44 passed from main. Then how the valuesassigned 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?




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.


Got it . Thanks.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top