Some questions on c99, gcc, and multi-dimensional dynamic arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I have a hard time trying to figure out if the
following code works as expected on every
platforms available today.

I want to make a function that creates a
multi-dimensional array of a given type.
The function has to be generic.

A simple solution is to allocate an array
of the wanted size for the base type
and another array that we fill with pointers,
so we can write: array[i][j][k][l][m] = xx;

As you can see array[i] has not the same
type as array[i][j]. This is the problem
I face.

So we would have something like:
-----
void *make_matrix(int ndims, int *dims[], size_t elem_size)
{
  void *pointers;
  void *data;
  int nb_pointers = get_nb_pointers(ndims, dims);
  int matrix_size = get_matrix_size(ndims, dims);

  pointers = malloc(nb_pointers * sizeof(void *)); //and we check
  data = malloc(matrix_size * elem_size);          //and we check

  //and we populate pointers in loops doing the following
  //pointers' manipulations:
  {
     void **cur = pointers;
     void **head = cur + something; //something is an integer
     *cur = head;
  }

  //and for the last dimension, we have also a loop manipulating
  //pointers as in:
  {
    char *p = data;
    void **cur = pointers;
    *cur = p + something; //something is an int, multiple of elem_size
  }

  return pointers;
}
-----
So we treat every pointers in the pointers'
array as "void *" and we pretend that "void *"
is like "void **" and like "void ***" and so
on. And also like "base_type *" which we don't
know but simply pass its size to make_matrix as
sizeof(base_type).

I tried to read C99 (with n1336.pdf) but I can't
figure out why this is not c99. I found a
message titled "Multi-Dimensional Dynamic Arrays"
from Oct 20 1998 on comp.lang.c that says something
like "pointers of type double * may not be the
same beast as pointers of type int * just like
a double is not the same in memory as an int."

What does it refer to in the c99 document? (or
the draft I have, n1336.pdf)

Sure I could ask on comp.lang.c, but people
there are more interested in abstract c99
policies. I care about writing a function.
And I care about how generic it would be.
It works on my x86 hosts and using gcc.
My real concern is: are there platforms
where the code miserably fails? That
would help my poor intuitive brain.

So, are there platforms out there where different
pointers' types are indeed different, so that
you can't use the address of a double * to
point to an int? I know there are alignment
issues, but apart from that? Are there some
pointers that are, I don't know, 32 bits for
a double * and 48 bits or whatever for an
int *?

Since gcc is a C compiler available for
many many platforms, you are probably the
best qualified to answer my questions.

And some of you surely know very well dark
corners of the c99 specifications, which
is very weird in some cases. And they don't
explain why they go so convoluted. There
must be reasons, no? Why do something almost
impossible to understand if some platforms
out there don't behave weird?

So if you have some time to answer me, that
would be hackingly cool. (Or any URL where
all this is crystal clear...)

As a last question, if we move away from c99
per se and focus on real implementations, how
does gcc behaves with my code above? Is it doing
the expected thing? on every backend you have?

Thanks,
and sorry if this is the wrong mailing list for
such questions. People writing gcc (you) just
seem qualified enough people to enlight a poor
lost brain like mine (without removing my skull
if possible).

Cédric.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux