allocate a multidimensional array dynamically

Y

Yvad

Hi all,

I am reading this link http://www.c-faq.com/aryptr/dynmuldimary.html.

And copy the first example to VC2003, but the compiler said "Point must
be cast explictly".
So I change the code to
//---
int **array1;
// the footnote said malloc function should be self-contained, so I
use sizeof(* array), is it right to use sizeof??
array1 = (int **) malloc(nrows * sizeof(* array1));

for(i = 0; i < nrows; i++)
array1 = (int *) malloc(ncolumns * sizeof(int));
//---
Is it right?

Best regards,
Davy
 
F

Flash Gordon

Yvad said:
Hi all,

I am reading this link http://www.c-faq.com/aryptr/dynmuldimary.html.

And copy the first example to VC2003, but the compiler said "Point must
be cast explictly".
So I change the code to
//---
int **array1;
// the footnote said malloc function should be self-contained, so I
use sizeof(* array), is it right to use sizeof??

Please don't use // style comments. They were added to C with the C99
standard, but no version of MS VC supports this standard, and as you can
see they don't survive line wrapping when posted to the group.
array1 = (int **) malloc(nrows * sizeof(* array1));

for(i = 0; i < nrows; i++)
array1 = (int *) malloc(ncolumns * sizeof(int));
//---
Is it right?


No it isn't, the code shown in the FAQ is correct. In C you don't need
to cast the return value of malloc. If MS VC++ is complaining then this
means one of two things:
1) You are compiling the code as C++
2) You have not included stdlib.h

For 1 you need to ask in a VC group, for 2 you need to include the
header as shown in the example.
 
V

Vladimir Oka

Yvad said:
Hi all,

I am reading this link http://www.c-faq.com/aryptr/dynmuldimary.html.

And copy the first example to VC2003, but the compiler said "Point must
be cast explictly".
So I change the code to
//---
int **array1;
// the footnote said malloc function should be self-contained, so I
use sizeof(* array), is it right to use sizeof??
array1 = (int **) malloc(nrows * sizeof(* array1));

for(i = 0; i < nrows; i++)
array1 = (int *) malloc(ncolumns * sizeof(int));
//---
Is it right?


Yes, and no. You have a few problems.

You're obviously using VC2003 in C++ mode. In C, you do not need to
cast return value of `malloc()`. As a matter of fact, you should not,
as it will mask the non-inclusion of <stdlib.h>, which in turn invokes
the wrath of Undefined Behaviour.

The beefed up example below compiles cleanly with paranoid warning
levels:

#include <stdlib.h>

int main(void)
{
int nrows = 10;
int ncolumns = 10;
int i;

int **array1 = malloc(nrows * sizeof(int *));
for(i = 0; i < nrows; i++)
array1 = malloc(ncolumns * sizeof(int));

return 0;
}
 
R

Robert Latest

On 23 May 2006 02:56:24 -0700,
in Msg. <[email protected]>

There's an a bit more canonical way to use malloc() with sizeof. This
way, if the type of the array cells is changed it needs to be changed in
only one place:
int **array1 = malloc(nrows * sizeof(int *));

int **array1 = malloc(nrows * sizeof *array1);
array1 = malloc(ncolumns * sizeof(int));


array1 = malloc(ncolumns * sizeof **array1);

robert
 
V

Vladimir Oka

Robert said:
On 23 May 2006 02:56:24 -0700,
in Msg. <[email protected]>

There's an a bit more canonical way to use malloc() with sizeof. This
way, if the type of the array cells is changed it needs to be changed in
only one place:
int **array1 = malloc(nrows * sizeof(int *));

int **array1 = malloc(nrows * sizeof *array1);
array1 = malloc(ncolumns * sizeof(int));


array1 = malloc(ncolumns * sizeof **array1);


Yes, of course. I was just to lazy to modify the example from the FAQ.
I just knew someone will come to my rescue. ;-)

Thanks.
 
P

pete

Vladimir Oka wrote:
Yes, of course. I was just to lazy to modify the example from the FAQ.
I just knew someone will come to my rescue. ;-)

If you were really lazy,
you'd appreciate that it's easier
not to have to post the explanation.

x = malloc(1);
/*
** Checking the return value of malloc for NULL here,
** has been omitted for brevity.
*/
 
R

Richard Heathfield

Yvad said:
Hi all,

I am reading this link http://www.c-faq.com/aryptr/dynmuldimary.html.

And copy the first example to VC2003, but the compiler said "Point must
be cast explictly".

Then use a C compiler instead of a C++ compiler, and remember to:

So I change the code to
//---
int **array1;
// the footnote said malloc function should be self-contained, so I
use sizeof(* array), is it right to use sizeof??
array1 = (int **) malloc(nrows * sizeof(* array1));

array1 = malloc(nrows * sizeof *array1);
if(array1 != NULL)
{
for(i = 0; i < nrows; i++)
{

array1 = (int *) malloc(ncolumns * sizeof(int));


array1 = malloc(ncolumns * sizeof *array1);
if(array1 == NULL)
{
take remedial action
 
V

Vladimir Oka

pete opined:
If you were really lazy,
you'd appreciate that it's easier
not to have to post the explanation.

x = malloc(1);
/*
** Checking the return value of malloc for NULL here,
** has been omitted for brevity.
*/

Well, my laziness waxes and wanes. ;-)

Also, AFAIR, OP did already know about that particular idiom, so I
didn't feel it was necessary to remind him again.

--
"Besides, I think [Slackware] sounds better than 'Microsoft,' don't
you?"
(By Patrick Volkerding)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top