HELP! Multidimensional array won't work 
Author Message
 HELP! Multidimensional array won't work

Hey there,

I'm having trouble with multidimensional arrays. I want to use a float
array of size 50x50 in a program, but when I've done this and run it,
my computer screen goes blue. The largestest mulidimensional array I've
been able to use is one that was 42x10(doesn't always work at that
size). How can I create a multidimensional array of the size I need?

* Sent from RemarQ http://www.*-*-*.com/ The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Fri, 28 Jun 2002 03:00:00 GMT  
 HELP! Multidimensional array won't work


Quote:

> I'm having trouble with multidimensional arrays. I want to use a float
> array of size 50x50 in a program, but when I've done this and run it,
> my computer screen goes blue. The largestest mulidimensional array
I've
> been able to use is one that was 42x10(doesn't always work at that
> size). How can I create a multidimensional array of the size I need?

Hmm.  Guessing that a float is at least 4 bytes and perhaps more,
a 50x50 array of floats would occupy 10000 bytes or more.  Are you
allocating this array as a local variable?  It's possible that your
implementation only allocates a fairly small stack for all of the
auto variables.  Does the behavior change if you create the array
as a global variable or using malloc?  Can you allocate a 2500
element single dimensional array of float?  Can you allocate an
array of 50 pointers to arrays of 50 floats?  And what system and
compiler are you using, anyway?  FAQ question 19.23 ("How can I
allocate arrays or structures bigger than 64K?") might be vaguely
relevant.

--
MJSR

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 28 Jun 2002 03:00:00 GMT  
 HELP! Multidimensional array won't work

Quote:
> Hey there,

> I'm having trouble with multidimensional arrays. I want to use a float
> array of size 50x50 in a program, but when I've done this and run it,
> my computer screen goes blue. The largestest mulidimensional array I've
> been able to use is one that was 42x10(doesn't always work at that
> size). How can I create a multidimensional array of the size I need?

It depends how you define it, what OS you use, and what compiler.
In general, all local variables are kept on the stack, which is not
big enough (in your case). Try to define that array as a global variable,
or as a static variable. It'll be kept in the data segment instead.
e.g. static int array[50][50];

Or use dynamic allocation (malloc()) and one dimension only:
e.g. float *array = malloc (50 * 50 * sizeof (float));

gonzo

==================================
Posted via http://nodevice.com
Linux Programmer's Site



Fri, 28 Jun 2002 03:00:00 GMT  
 HELP! Multidimensional array won't work
to make a[50][60], try this...

int i;
float **a;

a = (float **) calloc( 50, sizeof(float *));
if( a==NULL ) error();

for(i=0; i<50; i++){
    a[i] = (float *) calloc( 60, sizeof(float));
    if( a[i] == NULL ) error();

Quote:
}

if want to keep between successive calls, remember to make static, such as:

static float **a = NULL;

if( a==NULL ){
    //allocate here

Quote:
} else {

    //re-initialize matrix here

Quote:
}

to free, repeat above in opposite order, (free all a[i] first, then free (a),
else you have memory leak )

the 2d matrix here is an array of pointers, each pointing to a vector.  Can
scale to whatever dimension you need.

if dont need to store matrix between successive calls, remember that calloc is
relatively slow operation, so keeping
as static speeds execution, if matrix is same size or smaller, and not too
large, keep it.

brian

Quote:

> Hey there,

> I'm having trouble with multidimensional arrays. I want to use a float
> array of size 50x50 in a program, but when I've done this and run it,
> my computer screen goes blue. The largestest mulidimensional array I've
> been able to use is one that was 42x10(doesn't always work at that
> size). How can I create a multidimensional array of the size I need?

> * Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
> The fastest and easiest way to search and participate in Usenet - Free!



Fri, 28 Jun 2002 03:00:00 GMT  
 HELP! Multidimensional array won't work
On Mon, 10 Jan 2000 11:49:56 -0800, Bob

Quote:

>Hey there,

>I'm having trouble with multidimensional arrays. I want to use a float
>array of size 50x50 in a program, but when I've done this and run it,
>my computer screen goes blue. The largestest mulidimensional array I've
>been able to use is one that was 42x10(doesn't always work at that
>size). How can I create a multidimensional array of the size I need?

It would help if you enter your sample code so we can diagnose it.

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Fri, 28 Jun 2002 03:00:00 GMT  
 HELP! Multidimensional array won't work
On Mon, 10 Jan 2000 15:32:12 -0800, "Dann Corbit"

Quote:

>Calling calloc() is not terribly useful for float.  You have no guarantees
>that the floats will be zero.  They could represent anything including a
>trap representation.  Better to call malloc() and initialize them
>onesy-twosy.

Interesting. I'd never though ot that before. Can you cite an example
of where this would fail? Presumably a ones-complement machine??

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Fri, 28 Jun 2002 03:00:00 GMT  
 HELP! Multidimensional array won't work

Quote:

>Or use dynamic allocation (malloc()) and one dimension only:
>e.g. float *array = malloc (50 * 50 * sizeof (float));

Why limit it to 1 dimension?

  float (*array)[50] = malloc (50 * sizeof *array);

  array[23][49] = 42.0;

Also see section 6.16 of the FAQ.

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


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



Sat, 29 Jun 2002 03:00:00 GMT  
 HELP! Multidimensional array won't work


Quote:
>On Mon, 10 Jan 2000 15:32:12 -0800, "Dann Corbit"

>>Calling calloc() is not terribly useful for float.  You have no guarantees
>>that the floats will be zero.  They could represent anything including a
>>trap representation.  Better to call malloc() and initialize them
>>onesy-twosy.
>Interesting. I'd never though ot that before. Can you cite an example
>of where this would fail? Presumably a ones-complement machine??

Any non-IEEE-754 implementation is a candidate. I wouldn't be
surprised if one or more of the formats used by VAXen or IBM mainframes
did this.

Consider a format where the exponent is stored as a signed integer rather
than an excess format, and 0 is represented using the lowest (most
negative) exponent. The "sign bit" of the exponent at the very least would
be set. It seems to me that it would be a sensible idea for a
floating point implementation to make all-bits-zero a NaN representation.

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


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



Sat, 29 Jun 2002 03:00:00 GMT  
 HELP! Multidimensional array won't work
On Tue, 11 Jan 2000 00:17:45 -0800, "user923005"

Quote:



>> On Mon, 10 Jan 2000 15:32:12 -0800, "Dann Corbit"

>> >Calling calloc() is not terribly useful for float.  You have no guarantees
>> >that the floats will be zero.  They could represent anything including a
>> >trap representation.  Better to call malloc() and initialize them
>> >onesy-twosy.
>> Interesting. I'd never though ot that before. Can you cite an example
>> of where this would fail? Presumably a ones-complement machine??

>Ones complement is a function of integral types, not floating point types.

Call me an idiot for not remembering. The "onsey-twosey" but confused
me.
Quote:
>The internal format of floating point types is not defined by the C
>standard.  Just as you do not know that a memset of zero for a pointer will
>cause it to be NULL, similarly, memset of a float, double, or long double
>does not produce any guaranteed representation.

IOW magic bit patterns.

Interestingly Ive never come across a problem of this type despite
working on BBC-model B, RSM Z80, x86 (MS and Sun OSes), Sparc, Vax,
Alpha, HP, 680x0 and several other architectures which escape my
memory at present (oh and a Convex) .

Mind you I'm not entirely sure how I'd detect a problem! I guess it
depends how you use the memory.
Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Sun, 30 Jun 2002 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Allocating multidimensional arrays per FAQ 2.14 doesnt work!

2. PLEASE HELP - program won't work - Grades.c.txt [01/01]

3. C for loop won't work HELP!

4. rewind won't work--HELP

5. fscanf won't work -- help!!

6. Release build won't work, please help.

7. Help - Combo box won't work

8. Multidimensional Array's

9. Multidimensional arrays - what's it good for?

10. malloc'ing multidimensional arrays

11. TERMWAIT Win 3.1 sample doesn't work under '95

12. Multidimensional Array Help

 

 
Powered by phpBB® Forum Software