Dear All,

I am a C code to calculate prices in stock market.

I pass a two dimensional array to a function as

#define DIM 3000

void grid(double tree[][DIM]);

int main(){

double tree[DIM][DIM];

grid(tree );

return 0;
}


void grid(double tree[DIM][DIM]){

/* and do sutff  on tree *

}

Since tree is a large 2D array, 3000*3000, should be afraid about memory duplication when tree[][] is being passed as an argument to function grid().

I actually was afraid of that, and I wrote a reference & to the argument, but the compiler gave an error for that and did not accept that.

Thanks for your help.
Yours
Habib.

Recommended Answers

All 13 Replies

Don't worry about duplication because arrays are always passed by reference never by value. The & operator is unnecessary and sometimes may pass the wrong thing. The way you posted is correct.

I would be more concerned about the 72,000,000 bytes (about 70 meg) of memory that the array will occupy. Accessing such an array can be very very slow especially if the os swaps some of it to disk.

Declaring such a big array on stack (by declaring it inside a function) would probably cause stack overflow issues.

If you know pointers you should declare a double **tree inside main function (where you are defining double tree[DIM][DIM] ) and allocate memory dynamically using malloc() or something similar. This way you can allocate large chunks without worrying about the stack overflowing. And you will still be able to pass the tree to the grid() function without modifying its prototype (leave it the way it is defined right now).

Declaring such a big array on stack (by declaring it inside a function) would probably cause stack overflow issues.

If you know pointers you should declare a double **tree inside main function (where you are defining double tree[DIM][DIM] ) and allocate memory dynamically using malloc() or something similar. This way you can allocate large chunks without worrying about the stack overflowing. And you will still be able to pass the tree to the grid() function without modifying its prototype (leave it the way it is defined right now).

Yes I do know how to work with pointers but I do not know how to declare a pointer to pointer as a two dimensional array.
Should be something like this:

double **tree;

tree = (double*) malloc(DIM * DIM * sizeof(double));

is it enough?
Can I use it then as a two dimenional array like tree[j]?

Thanks indeed for your help!

Yes I do know how to work with pointers but I do not know how to declare a pointer to pointer as a two dimensional array.
Should be something like this:

double **tree;

tree = (double*) malloc(DIM * DIM * sizeof(double));

is it enough?
Can I use it then as a two dimenional array like tree[j]?

Thanks indeed for your help!

Yes except you need to cast it to (double **).

And yes you can use it like statically allocated array, that is tree[j].

First off, using a ** pointer is a waste of time in this case.
The size of the minor dimension is a constant, so use it.

> double tree[DIM][DIM];
Can be turned into this VERY EASILY.

double (*tree)[DIM] = malloc( DIM * sizeof(*tree) );

Now here's the real beauty of it all.
You can still call grid( tree ); and not change any other line of code.

When you're done, the whole array is deallocated with free( tree );

commented: great idea :) +26

Thanks.
To check the idea I wrote even a simpler code which is below

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

#define DIM 50

    
int main(){

  void grid(double tree[][DIM]);
  
  double **tree;
  
  tree = (double**)malloc(DIM * DIM * sizeof(double));
 
  tree[0][1] = 12.5;
  
  grid(tree);
  
 return 0;
}

void grid(double tree[DIM][DIM]){
  
  printf("tree[0][1]=%f\n", tree[0][1]);
  

}

-----------------------------------------

I am afraid that it does not work. I had tried before to do such a thing but never found out why it does not work.
I get the following message from compiler:

pointer_to_pointer.c: In function ‘main’:
pointer_to_pointer.c:18: warning: passing argument 1 of ‘grid’ from incompatible pointer type
pointer_to_pointer.c:10: note: expected ‘double (*)[50]’ but argument is of type ‘double **’

Salem,

Thanks for your kind reply.

It seems that your formula works out.
By the way, why your formula does not work when the malloc is used in a separate line like this:

double (*tree)[50];

(*tree)[50] = malloc( 50 * sizeof(*tree) );

Thanks in advance.
Habib.

Well ok, change the prototype to void grid(double **tree) as well, but inside you should be ok in accessing elements like tree[j].

Salem,

Thanks for your kind reply.

It seems that your formula works out.
By the way, why your formula does not work when the malloc is used in a separate line like this:

double (*tree)[50];

(*tree)[50] = malloc( 50 * sizeof(*tree) );

Thanks in advance.
Habib.

Well your lvalue is not correct here. You should write: tree = (doube(*)[50])malloc( 50 * sizeof(*tree) ); tree is still a pointer and can be assigned the returned pointer from malloc.

By the way, why your formula does not work when the malloc is used in a separate line like this:

double (*tree)[50]; 

(*tree)[50] = malloc( 50 * sizeof(*tree) );

As declaration + statement, it would be like this:

double (*tree)[DIM];
   [B]tree [/B]= malloc( DIM * sizeof(*tree) );

(Pay no attention to those showing you casts in C. :P)

(Pay no attention to those showing you casts in C. :P)

Pardon my C++ habits. Just trying to help. I tend to forget which one requires and which one doesn't so I tend to use it anyway. :)

Besides if he is using C++ compiler he won't be able to compile unless he uses the cast.

Besides if he is using C++ compiler he won't be able to compile unless he uses the cast.

Well, this is a C forum, and there was this little hint...

I am a C code to calculate prices in stock market.

:P


Besides if he is using C++ compiler he won't be able to compile unless he uses the cast.

All the c++ compilers I know of will also compile C code. Just name the file with a *.c extension instead of *.cpp or *.cc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.