Re: 2-dimensional arrays and functions
From: Jumbo ((nospam)_at_12freeukisp.co.uk)
Date: 10/30/03
- Next message: Jumbo: "Re: 0.1 = 0 ???"
- Previous message: Gene Wirchenko: "Re: basic_string::npos"
- In reply to: Micah Cowan: "Re: 2-dimensional arrays and functions"
- Next in thread: Richard Heathfield: "Re: 2-dimensional arrays and functions"
- Reply: Richard Heathfield: "Re: 2-dimensional arrays and functions"
- Reply: Karl Heinz Buchegger: "Re: 2-dimensional arrays and functions"
- Reply: Mike Wahler: "Re: 2-dimensional arrays and functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 30 Oct 2003 22:25:33 -0000
At least I have posted 3 good examples of how to do it which I don't see any
comments about so I assume they are OK. The only other person who tried to
help the OP was Mike I think but he went into function pointer land and I
was looking for a simpler answer.
Here are my examples again incase you missed it in this pointer pandemonium:
EXAMPLE 1: (this can be done in C or C++, for C change news to callocs)
If you return the pointer with the return statement you can create a memory
leak.
Here is an example of how NOT to do it:
int*** return_Array(int dim1, int dim2){
int** pArray = new int*[dim1];
for(int i=0; i<dim1; i++){
pArray[i] = new int[dim2];
}
return &pArray;
}
int main(){
int*** pArray = return_Array ( 5, 4);
return_Array ( 5, 4); /*this creates a memory leak */
return 0;
}
Reason you don't do it like this is that you can call the function without
assigning the return value to a pointer, and the compiler will not complain,
but you'd have just created a memory leak.
With pointers the function should be as follows:
This is the correct way to do it ( I think):
void return_Array(int*** ppArray, int dim1, int dim2){
int** pArray = new int*[dim1];
for(int i=0; i<dim1; i++){
pArray[i] = new int[dim2];
}
*ppArray = pArray;
}
int main(){
int** ppArray=0; /*declare a pointer to a 2DArray */
return_Array(&ppArray, 5, 4); /* Function assigns a pointer to a
dynamically created 2D array in ppArray (same effect as returning but safer)
*/
ppArray[0][2] = 202; /* Example assignment */
return 0;
}
You should probably make this function test for mem alloc and return a bool
value to reflect the mem alloc result.
Hope this helps.
EXAMPLE 2:
#include <stdio.h>
#define DIM1 4
#define DIM2 4
typedef int Array[DIM1][DIM2];
void pArrayFunction(Array* param1, Array* param2);
void printArray(Array* param);
int main()
{
Array Array1;
Array Array2;
Array* const pArray1 = &Array1;
Array* const pArray2 = &Array2;
for(int i=1; i<=DIM1; ++i){
for(int j=1; j<=DIM2; ++j){
(*pArray1)[i-1][j-1] = i * j;
}
}
pArrayFunction(pArray1, pArray2);
printf("Array1:");
printArray(pArray1);
printf("\n\nArray2:");
printArray(pArray2);
}
void pArrayFunction(Array* param1, Array* param2)
{
for(int i=1; i<=DIM1; ++i)
for(int j=1; j<=DIM2; ++j)
(*param2)[i-1][j-1] = (*param1)[i-1][j-1]+5;
}
void printArray(Array* param)
{
for(int i=1; i<=DIM1; ++i){
printf("\n");
for(int j=1; j<=DIM2; ++j)
printf("%d ",(*param)[i-1][j-1]);
}
}
EXAMPLE 3: (C++ only)
#include <iostream>
template<typename A> A retArray(A Array){
std::cout<<(*Array)[0][0] <<"\n";
return Array;
}
int main()
{
int Array1[5][5];
int Array2[6][6];
Array1[0][0] = 11;
Array2[0][0] = 22;
retArray(&Array1);
retArray(&Array2);
return 0;
}
Note I am passing by reference and returning by reference and the return
value is ignored but there is no memory leak.
And the one function can handle different sizes of array.
END EXAMPLES
I have put alot of effort into resolving the OP's problem and that's my
examples.
If you want to keep looking at the function I marked as NOT a good way to do
it then fine because who am I to say whats good and bad maybe that is a good
way to do it. But I will disagree until I can be convinced otherwise, so
please don't direct any posts regarding that towards me because I'm getting
fed up being told that it's bad, and told to go learn more etc as if I'd
made some kind of mistake, when I already ******* know it is bad.
And if you were wondering if I had noticed the bug. I did before you.
or maybe you assumed I hadn't noticed it ? If so all I can say is - never
assume.
Best regards.
:-)
LOL
- Next message: Jumbo: "Re: 0.1 = 0 ???"
- Previous message: Gene Wirchenko: "Re: basic_string::npos"
- In reply to: Micah Cowan: "Re: 2-dimensional arrays and functions"
- Next in thread: Richard Heathfield: "Re: 2-dimensional arrays and functions"
- Reply: Richard Heathfield: "Re: 2-dimensional arrays and functions"
- Reply: Karl Heinz Buchegger: "Re: 2-dimensional arrays and functions"
- Reply: Mike Wahler: "Re: 2-dimensional arrays and functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|