Wednesday 10 April 2013

Returning an array

Returning an array allocated by malloc gives an expected result.We can check it by two experiments:-

(1)By malloc in a function f() and printing it in a main
(2)By malloc in main and printing it in function f1

We use both method in a program to check if we return an array allocated by malloc gives correct result or not.Here is the program:-


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include<math.h>
#define size 50

int* f();
int* f1(int*);

void main()
{
int valueforb = 10;
int *aa=(int*)f();
int *bb = (int*)malloc(size*sizeof(int));
for(long unsigned int iterator = 0 ;iterator < size;iterator++)
{
bb[iterator]= valueforb ;
valueforb+=2;
}
f1(bb);
printf("\n");
//print aa in loop;
for(long unsigned int iterator = 0;  iterator < size ; iterator++)
{
printf("%d  ",aa[iterator]);
}
_getch();
}

int* f()
{
int * a= (int*)malloc(size*sizeof(int));
int value=0;
//initialise a with even numbers
for(long unsigned int iterator = 0 ;iterator < size;iterator++)
{
a[iterator]= value ;
value+=2;
}
return a;
}
int* f1(int* b)
{
for(long unsigned int iterator = 0;  iterator < size ; iterator++)
{
printf("%d  ",b[iterator]);
}
return b;
}
OUTPUT:-
10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48
50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84  86  88
90  92  94  96  98  100  102  104  106  108
0  2  4  6  8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  4
2  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  8
2  84  86  88  90  92  94  96  98

Here we allocated memory by malloc for a in function f and call function  then print its value in main we get the same value which is initialized in function f().In case of bb the value is initialized in main same printed in f1().Thus we must say that the array allocated by malloc is safe to return.

Now we can discuss returning an array allocated statically .There are two cases occur :-

1)If we declare an array with static duration
2)if we declare an array with automatic duration

1)If we declare an array with static duration:-If we declare an array in function f1 with static duration the value will be the remain safe in the main we can check it ny printing it.

2)if we declare an array with automatic duration:-If we declare an array in f() with automatic duration which is default duration .Then there the value will be unsafe . It means the auto variables are destroyed after there use and memory is cleaned up.

NOTE:-It may be possible if we returning an array from a function f which is allocated staticaly and have static duration the value of array in function main is same because sometimes memory is not cleaned up and in that case value will be same.

Here is the program for the issues related with returning an array having static allocation:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> 
#include<math.h>
#define size 50
int* f();
int* staticFun();

void main()
{
int *aa=f();
int* bb=staticFun();
//print aa in loop;
for(long unsigned int iterator = 0;  iterator < size ; iterator++)
{
printf("%d  ",aa[iterator]);
}

printf("\n"); 

for(long unsigned int iterator = 0;  iterator < size ; iterator++)
{
printf("%d  ",bb[iterator]);
}
_getch();
}
int* f() 
{
int  a[size];
int value = 0;
//initialise a with even numbers
for(long unsigned int iterator = 0 ;iterator < size;iterator++)
{
a[iterator]= value ;
value  += 2;
}
return a;
}
int* staticFun()
{
static int b[size];
int value=10;
for(long unsigned int iterator = 0 ;iterator < size;iterator++)
{
b[iterator]= value ;
value  += 2;
}
return b;
}
OUTPUT:-

-858993460  7801500  4585992  1547167480  1  1547167469  2094676724  4586380  45
86128  2130567168  1  1  1  3  7806216  7800832  4586096  1547215168  547612116
 -2  4586024  1546466082  4586380  2130567168  4586128  4586024  1546360630  154
7513552  4586036  1546458873  17  4586112  1546485755  1  1547498680  1546485737
  2094676604  4586380  4586128  2130567168  1  1  3  4586124  -858993460  -85899
3460  4586444  1547215168  547632892  -2
10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48
50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84  86  88
90  92  94  96  98  100  102  104  106  108

We can see here from the output that the array declared as auto duration prints the other value in the main than intialized in f() while the array declared as a static prints the same value in the main as initialized in f1().

No comments:

Post a Comment