On 2010-01-06, John <> wrote:
> The reason you don't agree with memset is memset set the elements to
> integer?
No. It sets the raw underlying bit patterns byte by byte, and that's not
guaranteed to be meaningful in non-integer types.
>> * * * * int *rows;
>> * * * * int *data;
>> * * * * data = malloc(rows * columns * sizeof(int));
>> * * * * rows = malloc(rows * sizeof(int *);
> 2 malloc functions nearby means the space is continus?
No.
There is no reason for the table of indexes and the table of rows to be
contiguous. ("Contiguous" -- having a shared boundary. Probably a better
word than "continuous" here.)
> You code don't solve the multiple data type issue either, I think.
You're right, it doesn't. You can't solve that completely generically
in C, because pointers to different types are not necessarily interchangeable.
You can come surprisingly close with a macro, though.
#define MAKE_2D_ARRAY(ptr, r, c, t) do { \
t *data; \
t **rows; \
/* fill this part in with the logic from the previous program */ \
ptr = rows[0]; \
} while(0);
#define FREE_2D_ARRAY(r) do { \
if (r) { free(r[0]); } free r;
} while(0);
(The "do... while(0)" idiom is something you can look up in the FAQ.)
-s
--
Copyright 2010, all wrongs reversed. Peter Seebach /
usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!