Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > dynamic 2 D array with malloc??

Reply
Thread Tools

dynamic 2 D array with malloc??

 
 
VijaKhara@gmail.com
Guest
Posts: n/a
 
      03-15-2007
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??

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      03-15-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      03-15-2007
(E-Mail Removed) 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?

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      03-15-2007

"Nick Keighley" <(E-Mail Removed)> wrote in message
> (E-Mail Removed) 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


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-15-2007
Malcolm McLean said:

>
> "Nick Keighley" <(E-Mail Removed)> wrote in message
>> (E-Mail Removed) 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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-15-2007
Malcolm McLean wrote:
>
> "Nick Keighley" <(E-Mail Removed)> wrote in message
>
>> (E-Mail Removed) 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.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      03-15-2007
(E-Mail Removed) 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 ---
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      03-15-2007
"Malcolm McLean" <(E-Mail Removed)> writes:

> "Nick Keighley" <(E-Mail Removed)> wrote in message
>> (E-Mail Removed) 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;}
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-15-2007
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.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      03-15-2007
In article <(E-Mail Removed)>,
Richard Heathfield <(E-Mail Removed)> 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.


>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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Array question - How dynamic is dynamic? Ruby Student Ruby 4 04-09-2009 12:59 PM
access object in array of pointers to dynamic array of class complex jccorreu@gmail.com C++ 2 05-20-2006 05:08 AM
Convert Static Array to Dynamic Array ? raxitsheth@gmail.com C Programming 2 07-14-2005 01:25 AM
Can a static array contain a dynamic array of pointers? Peter B. Steiger C Programming 8 04-26-2004 03:07 AM



Advertisments