Fix your FAQ

I

Ioannis Vranos

I came across in clc++ this by chance:



"There is no language construct for dynamically allocating
multidimensional arrays. See the comp.lang.c FAQ question 6.16 and
related questions:

http://www.eskimo.com/~scs/C-faq/q6.16.html

(Substitute new for malloc appropriately)
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional. "

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



My answer:




Derrick Coetzee said:
> JKop wrote:
>
>> int (&on_the_heap)[2][3] = *new int[2][3];
>
>
>
> There is no language construct for dynamically allocating
multidimensional arrays.



What?




> See the comp.lang.c FAQ question 6.16 and related questions:
>
> http://www.eskimo.com/~scs/C-faq/q6.16.html



It is C++ here, not C. But I just checked that FAQ. Who wrote that CRAP?

Even the casts it has in malloc are not needed in C!


In any case in C it can be done:


#include <stdlib.h>

/* ... */

int (*p)[3]=malloc(2*sizeof(*p));

/* ... */

free(p);"



Fix your FAQ!






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
I

Ioannis Vranos

Tweaked to display appropriately to all newsreaders:


I came across in clc++ this by chance:



"There is no language construct for dynamically allocating
multidimensional arrays. See the comp.lang.c FAQ question 6.16 and
related questions:

http://www.eskimo.com/~scs/C-faq/q6.16.html

(Substitute new for malloc appropriately)


Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional. "

=======================================================================



My answer:




Derrick Coetzee said:
> JKop wrote:
>
>> int (&on_the_heap)[2][3] = *new int[2][3];
>
>
>
> There is no language construct for dynamically allocating
multidimensional arrays.



What?




> See the comp.lang.c FAQ question 6.16 and related questions:
>
> http://www.eskimo.com/~scs/C-faq/q6.16.html



It is C++ here, not C. But I just checked that FAQ. Who wrote that CRAP?

Even the casts it has in malloc are not needed in C!


In any case in C it can be done:


#include <stdlib.h>

/* ... */

int (*p)[3]=malloc(2*sizeof(*p));

/* ... */

free(p);"



Fix your FAQ!






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
A

Arthur J. O'Dwyer

[big snip; next bit from c.l.c++]
It is C++ here, not C. But I just checked that FAQ. Who wrote that CRAP?

Even the casts it has in malloc are not needed in C!

Yes, the FAQ uses casts a lot. I have no idea why. I think the
question "Why does the FAQ use casts on malloc?" /is/ a Frequently
Asked Question in this newsgroup, and I'm sure Steve Summit has posted
some reason or other for it, but I don't remember what the answer was.
I think if the FAQ isn't going to be updated for C89, it at least
ought to add a new question and answer: "Why does the FAQ use casts
on malloc?"
In any case in C it can be done:

#include <stdlib.h>

int (*p)[3]=malloc(2*sizeof(*p));

Yes, of course. This is exactly the code given in example 4 in
the FAQ (up to a renaming of variables). The problem is that this
method can't be used to dynamically allocate both dimensions of an
array---only the '2' dimension is allowed to vary at runtime in your
example. (The FAQ explains this, too.) I think

int (*p)[y] = malloc(x*sizeof *p);

is still illegal even in C99, but I have no C&V to back that up.
Maybe C99 did change this.
Fix your FAQ!

The only problem is the malloc-casting, and that's a known bug
(feature?) of the FAQ. There's nothing wrong with the content of
that FAQ answer itself.

HTH,
-Arthur
 
M

Malcolm

Arthur J. O'Dwyer said:
int (*p)[3]=malloc(2*sizeof(*p));

Yes, of course. This is exactly the code given in example 4 in
the FAQ (up to a renaming of variables). The problem is that this
method can't be used to dynamically allocate both dimensions of an
array---only the '2' dimension is allowed to vary at runtime in your
example. (The FAQ explains this, too.) I think
The other problem is that the syntax is so convoluted, and the construct so
rarely used, that the average programmer cannot be expected to use it.
 
C

Chris Torek

Yes, the FAQ uses casts a lot. I have no idea why.

The FAQ predates ANSI C89 -- or at least, widespread existence
of same (much of the text in the FAQ dates back to 1991 or so,
I believe, while C89 compilers were not really common until 1994).

Nonetheless:
... and I'm sure Steve Summit has posted
some reason or other for it, but I don't remember what the answer was.

The answer was "already fixed in the text and book version, but
the HTML version is behind". The monthly-posted-version no longer
casts malloc().
 
I

Ioannis Vranos

Arthur said:
Yes, the FAQ uses casts a lot. I have no idea why. I think the
question "Why does the FAQ use casts on malloc?" /is/ a Frequently
Asked Question in this newsgroup, and I'm sure Steve Summit has posted
some reason or other for it, but I don't remember what the answer was.
I think if the FAQ isn't going to be updated for C89, it at least
ought to add a new question and answer: "Why does the FAQ use casts
on malloc?"
In any case in C it can be done:

#include <stdlib.h>

int (*p)[3]=malloc(2*sizeof(*p));


Yes, of course. This is exactly the code given in example 4 in
the FAQ (up to a renaming of variables). The problem is that this
method can't be used to dynamically allocate both dimensions of an
array---only the '2' dimension is allowed to vary at runtime in your
example. (The FAQ explains this, too.) I think



Yes you are right, in this case ** must be used.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
D

Dan Pop

In said:
It is C++ here, not C. But I just checked that FAQ. Who wrote that CRAP?

Someone who, unlike you, knew what he was talking about.
Even the casts it has in malloc are not needed in C!

There are no such casts in the official version of the c.l.c FAQ.
In any case in C it can be done:


#include <stdlib.h>

/* ... */

int (*p)[3]=malloc(2*sizeof(*p));

/* ... */

free(p);"

Fix your FAQ!

As soon as you show us a solution where BOTH dimensions are not known
at compile time. BTW, your method is also described by 6.16:

Yet another option is to use pointers to arrays:

int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

but the syntax starts getting horrific and at most one dimension
may be specified at run time.

So, please read the text *before* commenting it! Yes, I know I'm asking
too much from you...

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top