Problem how to calculate size of "Array"

From different languages I am used to arrays, here in C/C++ it is a bit harder. Can you help? I am writing C code now. I have file which uses openCL to generate program for GPU. Now, there is a part of code which detects some informations about how many devices there is in the machine (I dont know how this exactly works. The complete code is long so I paste the code here:
http://paste.ofcode.org/U5g2Z3Cj5ZuRBnJf2b5cqv
and here the key part - this is how they use it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 size_t nDevices;
 size_t * binary_sizes;
 char ** binaries;
 ...
    err = clGetProgramInfo( program, CL_PROGRAM_NUM_DEVICES, sizeof(nDevices), &nDevices, NULL );
 binary_sizes = (size_t *)malloc( sizeof(size_t)*nDevices );
 ...
binaries = (char **)malloc( sizeof(char *)*nDevices );
 ...

 int i;
    for( i = 0; i < nDevices; i++ )
    {
        if( binary_sizes[i] != 0 )
        {
            binaries[i] = (char *)malloc( sizeof(char)*binary_sizes[i] );
        }
        else
        {
            binaries[i] = NULL;
        }
    }


Now I am writing the code to write the binaries data, and this is where I need to find out how to calculate the count or "how many times the loop should be repeated". So if you have two GPU devices in machine (as far as I understand openCL) the binaries data should contain two sets of data for each GPU device. So I have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
void write(char ** binaries, size_t nDevices){
    char str[1000];
    int n;
    for( n = 0; n < nDevices; n++ )
    {
    FILE* f = fopen("input.bin", "wb");
    fseek(f, 0, SEEK_SET);
    int i;
    for (i = 0; i < jel_size; i++)
            fwrite(&foo[i], sizeof(struct foo), 1, f);
    fclose(f);
    }
}


Also I need advice how to turn the n to valid filename for fopen.
Thanks for article, very good, but can you explain me why in my code, there is:

1
2
char ** binaries; // Why two pointers here? Why pointer to pointer?
binaries = (char **)malloc( sizeof(char *)*nDevices );


Any time you see type* you are looking at a pointer to type, which can be treated as if it were an array of type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> 

void print5( int* xs )  // xs is a pointer to an integer (which we'll treat like an array)
{
  for (int n = 0; n < 5; n++)
    std::cout << xs[ n ] << "  ";
  std::cout << "\n";
}

int main()
{
  int a[] = { 1, 2, 3, 4, 5 };

  print5( &(a[0]) );  // pass as argument the address to the first element of a[]
}


Any time you see type** you are looking at a pointer to a pointer to type.

Now, the array thing again: you may have an array of pointers to type, or a pointer to an array of type, or an array of arrays of type.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void print2( int** xs )  // xs here is a pointer to pointer to int, or an array of pointers to int...
{
  for (int n = 0; n < 2; n++)
    print5( xs[ n ] );
}

int main()
{
  int a[] = { 1, 2, 3, 4, 5 };
  int b[] = { 6, 7, 8, 9, 10 };

  int* xs[] = { &(a[0]), &(a[1]) };

  print2( &(xs[0]) );  // the address of the first element of a[] is a pointer to a pointer to int.
}

Hope this helps.
Topic archived. No new replies allowed.