Returning a 2D array from a function

G

Guha

I have a problem with returning a 2D array using a function which is
called in main(). The piece of the code is given below. This is a test
code only.

#include"stdio.h"
#include"alloc.h"

void main()
{

long double **mat;
int i,j,n;

long double **test(int m);


clrscr();

printf("\nEnter The Value of n\n");
scanf("%d",&n);


/* allocating the memory of mat */
mat=malloc(sizeof(long double *)*n);

for(i=0;i<n;i++)
mat=malloc(sizeof(long double )*n);

/* base address of **a is stored in mat */
mat=test(n);

/* Printing the values of 2D array */
for(i=0;i<n;i++)
{
printf("");
for(j=0;j<n;j++)
{
printf("\n%3.1Lf",mat[j]);
}
}


for(i=0;i<n;i++)
free(mat);

free(mat);


getch();
}

long double **test(int m)
{
int i,j;

long double **a;

a=malloc(m*sizeof(long double *));

for(i=0;i<m;i++)
a=malloc(m*sizeof(long double));



for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[j]=i; /* assiging some values */


return(a);

}


1) using the function **test() I'm able to return the base address of
the 2D array **a and it also prints the results correctly while n<56
but the problem is when the value of n goes beyond 56 it prints ascii
characters in the output.

2) another problem is that I'm not able to free the memeory allocated
for the variable **a because if I add any line after the return()
command the lines are unreachable and if I had to free the memory
before returning the varibale address it would return nothing.

Please suggest what is to be done.
Thnaks.
 
B

Barry Schwarz

I have a problem with returning a 2D array using a function which is
called in main(). The piece of the code is given below. This is a test
code only.

#include"stdio.h"

This is a standard header. Use said:
#include"alloc.h"

No such header. Use said:
void main()

int main(void) please
{

long double **mat;
int i,j,n;

long double **test(int m);


clrscr();

A nonstandard function you don't need.
printf("\nEnter The Value of n\n");
scanf("%d",&n);


/* allocating the memory of mat */
mat=malloc(sizeof(long double *)*n);

You should always check malloc for success.
for(i=0;i<n;i++)
mat=malloc(sizeof(long double )*n);

/* base address of **a is stored in mat */
mat=test(n);


This creates a memory leak. You no longer know the addresses returned
by any of the previous n+1 calls to malloc.

On the other hand, since you want to allocate the memory in test, you
should not be doing any calls to malloc in main.
/* Printing the values of 2D array */
for(i=0;i<n;i++)
{
printf("");

And this accomplishes what?
for(j=0;j<n;j++)
{
printf("\n%3.1Lf",mat[j]);
}
}


for(i=0;i<n;i++)
free(mat);


This is freeing the memory allocated in test. The memory allocated in
main is permanently leaked.
free(mat);


getch();

Another nonstandard function you don't need. Use getchar().
}

long double **test(int m)
{
int i,j;

long double **a;

a=malloc(m*sizeof(long double *));

for(i=0;i<m;i++)
a=malloc(m*sizeof(long double));



for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[j]=i; /* assiging some values */


return(a);


return is not a function but a statement. You don't need the
parentheses.
}


1) using the function **test() I'm able to return the base address of

The function name is test. The ** is part of the return type, not
part of the name.
the 2D array **a and it also prints the results correctly while n<56

The pointer (not array) name is a. The ** is part of the type...
but the problem is when the value of n goes beyond 56 it prints ascii
characters in the output.

Sounds like malloc is failing and you then invoke undefined behavior.

All of your output is ascii characters. However, it is really strange
for a %Lf format to produce recognizable "character" output as opposed
to "numerical" output.
2) another problem is that I'm not able to free the memeory allocated
for the variable **a because if I add any line after the return()
command the lines are unreachable and if I had to free the memory
before returning the varibale address it would return nothing.

No you don't. You can free the memory in main since main now knows
where the memory allocated by test is.
Please suggest what is to be done.
Thnaks.


<<Remove the del for email>>
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top