472,807 Members | 1,828 Online

# dynamic 2 D array with malloc??

hi all,

i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

N=30000;
xyz=malloc(3*sizeof(int*));
for (i=0; i<N;i++)
xyz[i]=malloc(sizeof(int)*N);
the program ran too slow and finally error appeared when i increased
to 6288:
"Unhandled exception in proj1.exe :0xC0000005:Access violation.

it seems a "run out off memory error" . my pc has 600Mb RAM and around
1Gb free in harddisk.

Iam wondering if my array is too large that my pc's RAM can't handle?
it is amazing.

plz help??

Mar 15 '07 #1
24 3044
Vi*******@gmail.com wrote:
i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

N=30000;
xyz=malloc(3*sizeof(int*));
for (i=0; i<N;i++)
Um. Shouldn't that N be 3?
xyz[i]=malloc(sizeof(int)*N);
Yes, it should. BOOM today.

PS usual remarks about preferred style of mallocation.

--
Chris "lucky man" Dollin
"Anything can happen in the next half-hour." /Stingray/

Mar 15 '07 #2
Vi*******@gmail.com wrote:
i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;
by convention uppercase is reserved for macros.

N=30000;
xyz=malloc(3*sizeof(int*));
since 3 is fixed you could do

int *xyz[3];

if you use malloc() then test the return value

for (i=0; i<N;i++)
xyz[i]=malloc(sizeof(int)*N);
um. You allocate 3 int*s then you try to index 30,000 items in
this block of memory.

You meant:

for (i = 0; i < 3; i++)
{
xyz[i] = malloc(sizeof (int) * N);
if (xyz[0] == NULL)
handle_error();
}

note how judicious use of whitespace imprves readability.

the program ran too slow and finally error appeared when i increased
to 6288:
"Unhandled exception in proj1.exe :0xC0000005:Access violation.

it seems a "run out off memory error" . my pc has 600Mb RAM and around
1Gb free in harddisk.

Iam wondering if my array is too large that my pc's RAM can't handle?
it is amazing.
just because your PC has 600M doesn't mean the OS will let you have it
all.

try this

p = malloc(3 * N * sizeof(int*));

if (p == NULL)
printf ("can't malloc that!!\n);
--
Nick Keighley
- Yes it works in practice - but does it work in theory?

Mar 15 '07 #3

"Nick Keighley" <ni******************@hotmail.comwrote in message
Vi*******@gmail.com wrote:
>i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

by convention uppercase is reserved for macros.
N is an exception. There is a strong convention for using N to hold a count.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Mar 15 '07 #4
Malcolm McLean said:
>
"Nick Keighley" <ni******************@hotmail.comwrote in message
>Vi*******@gmail.com wrote:
>>i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

by convention uppercase is reserved for macros.
N is an exception. There is a strong convention for using N to hold a
count.
That's the first I've heard of such a convention. When did that happen?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 15 '07 #5
Malcolm McLean wrote:
>
"Nick Keighley" <ni******************@hotmail.comwrote in message
>Vi*******@gmail.com wrote:
>>i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

by convention uppercase is reserved for macros.
N is an exception. There is a strong convention for using N to hold a
count.
Where?

--
Ian Collins.
Mar 15 '07 #6
Vi*******@gmail.com wrote:
hi all,

i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

N=30000;
xyz=malloc(3*sizeof(int*));
for (i=0; i<N;i++)
xyz[i]=malloc(sizeof(int)*N);
the program ran too slow and finally error appeared when i increased
to 6288:
"Unhandled exception in proj1.exe :0xC0000005:Access violation.

it seems a "run out off memory error" . my pc has 600Mb RAM and around
1Gb free in harddisk.

Iam wondering if my array is too large that my pc's RAM can't handle?
it is amazing.

plz help??
You are not programming the problem. Three rows of 30,000 ints should be
doable like this..

int **xyz;
int i, r = 3, c = 30000;

xyz = malloc(r * sizeof *xyz);
for (i = 0; i < r; ++i)
xyz[i] = malloc(c * sizeof **xyz);

What do you think?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Mar 15 '07 #7
Joe Wright said:

<snip>
int **xyz;
int i, r = 3, c = 30000;

xyz = malloc(r * sizeof *xyz);
for (i = 0; i < r; ++i)
xyz[i] = malloc(c * sizeof **xyz);

What do you think?
I think xyz could be NULL at the point where you deref it. Why, what do
/you/ think? :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 15 '07 #8
"Malcolm McLean" <re*******@btinternet.comwrites:
"Nick Keighley" <ni******************@hotmail.comwrote in message
>Vi*******@gmail.com wrote:
>>int i,N;

by convention uppercase is reserved for macros.
N is an exception. There is a strong convention for using N to hold a count.
Really? What base of code is it that makes use of this
convention? I have not encountered it, and I think of myself as
someone who has read a lot of code.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Mar 15 '07 #9
On Mar 16, 10:21 am, "Malcolm McLean" <regniz...@btinternet.com>
wrote:
"Nick Keighley" <nick_keighley_nos...@hotmail.comwrote in message
by convention uppercase is reserved for macros.

N is an exception. There is a strong convention for using N to hold a count.
I've never seen it. I do use lower case 'n' for a count in
throwaway code, but never upper case.
Mar 15 '07 #10
In article <op*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>>int i,N;
>>by convention uppercase is reserved for macros.
>N is an exception. There is a strong convention for using N to hold a
count.
>That's the first I've heard of such a convention. When did that happen?
In my experience it's much more often lowercase. But it raises a good
point: conventions often conflict. If you're using C to implement
some published algorithm there's no particular reason why C's
conventions should trump the algorithm's. You just have to judge
which will be most convenient for readers and maintainers of the
code.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 15 '07 #11
Richard Tobin wrote, On 15/03/07 22:27:
In article <op*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>>>int i,N;
>>>by convention uppercase is reserved for macros.
>>N is an exception. There is a strong convention for using N to hold a
count.
>That's the first I've heard of such a convention. When did that happen?

In my experience it's much more often lowercase. But it raises a good
point: conventions often conflict. If you're using C to implement
some published algorithm there's no particular reason why C's
conventions should trump the algorithm's. You just have to judge
which will be most convenient for readers and maintainers of the
code.
Unless the convention of the algorithm uses both 'N' and 'n' I would say
that the case conventions of C should be used. After all, someone
reading the code who knows the algorithm is unlikely to be confused by a
change of case, and if they know C as well they are likely to know the C
conventions as well and so expect it.
--
Flash Gordon
Mar 16 '07 #12
In article <bm************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>In my experience it's much more often lowercase. But it raises a good
point: conventions often conflict. If you're using C to implement
some published algorithm there's no particular reason why C's
conventions should trump the algorithm's. You just have to judge
which will be most convenient for readers and maintainers of the
code.
>Unless the convention of the algorithm uses both 'N' and 'n'
.... that's just the case I was thinking of. But there are also
conventions like vectors being in capitals where you might want to use
case to distinguish even when there is no name clash.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 16 '07 #13
Richard Tobin wrote, On 16/03/07 01:19:
In article <bm************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>>In my experience it's much more often lowercase. But it raises a good
point: conventions often conflict. If you're using C to implement
some published algorithm there's no particular reason why C's
conventions should trump the algorithm's. You just have to judge
which will be most convenient for readers and maintainers of the
code.
>Unless the convention of the algorithm uses both 'N' and 'n'

... that's just the case I was thinking of.
I would say the algorithm uses a horrible convention in that case, one
that makes it harder to talk (as opposed to write) about it. Talking you
would have to keep saying "capital N", "lower case n" and the like.
But there are also
conventions like vectors being in capitals where you might want to use
case to distinguish even when there is no name clash.
If the distinction is not obvious from the name then I would add vect_
to the start of the names. I still do not see it confusing someone
familiar enough with the algorithm to know the conventions it uses, and
someone not familiar with the algorithm but familiar with C will find it
easier.
--
Flash Gordon
Mar 16 '07 #14
In article <41************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>>Unless the convention of the algorithm uses both 'N' and 'n'
>... that's just the case I was thinking of.
>I would say the algorithm uses a horrible convention in that case, one
that makes it harder to talk (as opposed to write) about it. Talking you
would have to keep saying "capital N", "lower case n" and the like.
Mathematicians (and physicists etc) are used to this. They generally
say "big N" and "little n".

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 16 '07 #15
Thank you very much. I have fixed the problem/ Now I am confused how
to free this 2D array:
If my code is as follows:

int **xyz;
int i, r = 3, c = 30000;
xyz = malloc(r * sizeof(*int));
for (i = 0; i < r; ++i)
xyz[i] = malloc(c * sizeof **xyz);

how to free this memory block:

for (i=0;i<r;i++)
free xyz[i];

does it work?

and one more question what is difference between :

for (i = 0; i < r; ++i)

and

for (i = 0; i < r; i++)

I often use the later.

Thanks

On Mar 16, 10:01 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <41iqc4xbj7....@news.flash-gordon.me.uk>,
Flash Gordon <s...@flash-gordon.me.ukwrote:
>Unless the convention of the algorithm uses both 'N' and 'n'
... that's just the case I was thinking of.
I would say the algorithm uses a horrible convention in that case, one
that makes it harder to talk (as opposed to write) about it. Talking you
would have to keep saying "capital N", "lower case n" and the like.

Mathematicians (and physicists etc) are used to this. They generally
say "big N" and "little n".

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Mar 16 '07 #16
Vi*******@gmail.com wrote:
and one more question what is difference between :

for (i = 0; i < r; ++i)

and

for (i = 0; i < r; i++)
There's no visible difference. (Since the value of `++i` or
`i++` is never accessed, the fact that they're different doesn't
make any difference.)
I often use the later.
I use `i += 1`; as a general rule, I use the ++ forms when the
value is used, and the += form when it isn't.

(Yes, I know that the += form has a value.)

--
Chris "electric hedgehog" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

Mar 16 '07 #17
Richard Tobin wrote, On 16/03/07 16:01:
In article <41************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>>>Unless the convention of the algorithm uses both 'N' and 'n'
>>... that's just the case I was thinking of.
>I would say the algorithm uses a horrible convention in that case, one
that makes it harder to talk (as opposed to write) about it. Talking you
would have to keep saying "capital N", "lower case n" and the like.

Mathematicians (and physicists etc) are used to this. They generally
say "big N" and "little n".
Doesn't mean it isn't horrible and doesn't make it harder ;-)
--
Flash Gordon
Mar 16 '07 #18
>From the C-FAQ:

6.16: How can I dynamically allocate a multidimensional array?

A: The traditional solution is to allocate an array of pointers,
and then initialize each pointer to a dynamically-allocated
"row." Here is a two-dimensional example:

#include <stdlib.h>

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

(In real code, of course, all of malloc's return values would
be checked.)

You can keep the array's contents contiguous, at the cost of
making later reallocation of individual rows more difficult,
with a bit of explicit pointer arithmetic:

int **array2 = malloc(nrows * sizeof(int *));
array2[0] = malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2[i] = array2[0] + i * ncolumns;

In either case, the elements of the dynamic array can be
accessed with normal-looking array subscripts: arrayx[i][j]
(for 0 <= i < nrows and 0 <= j < ncolumns).

If the double indirection implied by the above schemes is for
some reason unacceptable, you can simulate a two-dimensional
array with a single, dynamically-allocated one-dimensional
array:

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

However, you must now perform subscript calculations manually,
accessing the i,jth element with array3[i * ncolumns + j]. (A
macro could hide the explicit calculation, but invoking it would
require parentheses and commas which wouldn't look exactly like
multidimensional array syntax, and the macro would need access
to at least one of the dimensions, as well. See also question
6.19.)

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.

With all of these techniques, you may of course need to remember
to free the arrays (which may take several steps; see question
7.23) when they are no longer needed, and you cannot necessarily
intermix dynamically-allocated arrays with conventional,
statically-allocated ones (see question 6.20, and also question
6.18).

Finally, in C9X you can use a variable-length array.

All of these techniques can also be extended to three or more
dimensions.

References: C9X Sec. 6.5.5.2.
Mar 16 '07 #19
Vi*******@gmail.com wrote:
>
Thank you very much. I have fixed the problem/ Now I am confused
how to free this 2D array: If my code is as follows:

int **xyz;
int i, r = 3, c = 30000;

xyz = malloc(r * sizeof(*int));
for (i = 0; i < r; ++i)
xyz[i] = malloc(c * sizeof **xyz);

how to free this memory block:

for (i=0;i<r;i++)
free xyz[i];

does it work?
with, the snipped material which you quote.

No, it doesn't work. You have to reverse all the malloc actions.
So the appropriate code would be:

for (i = 0; i < r; i++) free xyz[i];
free(xyz);

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 16 '07 #20
Vi*******@gmail.com wrote:
Thank you very much. I have fixed the problem/ Now I am confused how
to free this 2D array:

with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Mar 16 '07 #21
On 16 Mar 2007 09:47:57 -0700, Vi*******@gmail.com wrote:
>Thank you very much. I have fixed the problem/ Now I am confused how
to free this 2D array:
If my code is as follows:

int **xyz;
int i, r = 3, c = 30000;
xyz = malloc(r * sizeof(*int));
Since *int is a syntax error, I will assume you meant *xyz.
>for (i = 0; i < r; ++i)
xyz[i] = malloc(c * sizeof **xyz);

how to free this memory block:

for (i=0;i<r;i++)
free xyz[i];
free is a function, not a statement. I will assume you meant
free(xyz[i]);

There should be a free for each malloc. You have r+1 calls to malloc
but only r calls to free. You are missing
free(xyz);
>
does it work?

and one more question what is difference between :

for (i = 0; i < r; ++i)

and

for (i = 0; i < r; i++)
In this case, you are not using the value of either ++ operator. You
are only using the side effect (the fact that i is incremented). Since
the side effect is the same, there is no difference.
Remove del for email
Mar 17 '07 #22
Richard Heathfield wrote:
Joe Wright said:

<snip>
>int **xyz;
int i, r = 3, c = 30000;

xyz = malloc(r * sizeof *xyz);
for (i = 0; i < r; ++i)
xyz[i] = malloc(c * sizeof **xyz);

What do you think?

I think xyz could be NULL at the point where you deref it. Why, what do
/you/ think? :-)
I have fixed my machine so that malloc() never fails. :=)

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Mar 17 '07 #23
Groovy hepcat Nick Keighley was jivin' on 15 Mar 2007 08:28:01 -0700
in comp.lang.c.
Re: dynamic 2 D array with malloc??'s a cool scene! Dig it!
>Vi*******@gmail.com wrote:
>i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

by convention uppercase is reserved for macros.

>N=30000;
xyz=malloc(3*sizeof(int*));

since 3 is fixed you could do

int *xyz[3];
Or rather, since it's supposed to be N x 3, not 3 x N

int (*xyz)[3];
....
xyz = malloc(N * sizeof *xyz);
....

Then the whole thing is allocated in one shot, used as a 2D array
(except when the operand of & or sizeof) and, when finished, freed in
one shot.

--

Dig the even newer still, yet more improved, sig!

"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Mar 21 '07 #24
Could be worse - could be allocating 3D arrays. I just happen to have an
example of that...
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Mar 27 '07 #25

This thread has been closed and replies have been disabled. Please start a new discussion.