large array problem? 
Author Message
 large array problem?

hi,

This code works fine with an array size of 100,000 when I try to increase
the size to 1,000,000 it crashes. After debugging it indicates a stack
overflow has occured. Why is this happening? I have 64 meg of memory.P2 300
mhz win95.The compiler is VC++ 5.0.

#include <stdlib.h>
#include <stdio.h>

int compare_long(long *a, long *b)
 {
  if (*a < *b)
   return(-1);
  else if (*a == *b)
   return(0);
  else
   return(1);
 }

void main(void)
 {
   long long_values[100000];   /* when changed to 1000000 it crashes */

 long elements = 100000, i;     /* when changed to 1000000 it crashes */
 printf("Here we go \n");
 getchar();

 for (i = 0; i < elements; i++)
  long_values[i] = i;

 printf("done initializing array\n");
 getchar();

 qsort(long_values, elements, sizeof(long),
      (int (*) (const void *, const void *)) compare_long);

 for (i = 0; i < elements; i++)
     printf("%d ", long_values[i]);

 putchar('\n');

 }

--



Tue, 29 Jan 2002 03:00:00 GMT  
 large array problem?
: hi,
:
: This code works fine with an array size of 100,000 when I try to increase
: the size to 1,000,000 it crashes. After debugging it indicates a stack
: overflow has occured. Why is this happening? I have 64 meg of memory.P2 300
: mhz win95.The compiler is VC++ 5.0.

Maybe something in the WIN32 architecture needs to be done differenetly,
since this works flawlessly (printing all 1M integers to my screen).
Maybe you need a flag for your compiler to make sure it is ANSI compliant,
and doesn't try any wierd optimizations?

B.

--

   Brian P. Hampson                  ASL Analytical Service Laboratories Ltd
   System Administrator,             Vancouver, BC (604)253-4188
      ----------------- http://www.ASL.CA/ ----------------------------  

I'm not speaking for the company <- They made me say that.
--



Tue, 29 Jan 2002 03:00:00 GMT  
 large array problem?
        I did not see the original post, but assume it had something to
do with a large array local to a procedure. Please read my response in
that context, or ignore it.

: : hi,
: :
: : This code works fine with an array size of 100,000 when I try to increase
: : the size to 1,000,000 it crashes. After debugging it indicates a stack
: : overflow has occured. Why is this happening? I have 64 meg of memory.P2 300
: : mhz win95.The compiler is VC++ 5.0.

: Maybe something in the WIN32 architecture needs to be done differenetly,

        Like, diddling a registry entry perhaps (No idea, but _everything_
seems to be in the registry... :-)

: since this works flawlessly (printing all 1M integers to my screen).

        Then just keep bumping the array size up until it does croak.
4 and 8 meg stack limits are pretty common on a lot of OSes.

        ANSI/ISO C does not, AFAIK, require an implementation to
allow truly arbitrary sized arrays. Most OSes have some notion of
"a reasonable stack", among other reasons to allow them to detect
processes with runaway recursion. _Usually_, there is a way to
say "Trust me, I know what I'm doing" to such OSes. Of course, one
could make the argument that someone who declares such an array
locally, and does not pretty much immediately turn to the "How
to increase process stack size" section of the manual in case of
errors, does _not_ truly know what he's doing... :-)

                                                Mike

--



Wed, 30 Jan 2002 03:00:00 GMT  
 large array problem?
Quote:

>hi,

>This code works fine with an array size of 100,000 when I try to increase
>the size to 1,000,000 it crashes. After debugging it indicates a stack
>overflow has occured. Why is this happening? I have 64 meg of memory.P2 300
>mhz win95.The compiler is VC++ 5.0.
[...]
>void main(void)
Sorry, int main()
> {
>   long long_values[100000];   /* when changed to 1000000 it crashes */

> long elements = 100000, i;     /* when changed to 1000000 it crashes */
[...]
> qsort(long_values, elements, sizeof(long),
>      (int (*) (const void *, const void *)) compare_long);

sorry again, this function type cast is a bug ...
Quote:

> for (i = 0; i < elements; i++)
>     printf("%d ", long_values[i]);

... %d means int, not long but all that is probably not related
to your problem. My guess is M$'s qsort() version just cannot
handle such a large array but how to increase your defaults
stack size is a compiler and platform specific questions, see
the documentation, it is there for sure.

One hint, a decent qsort() implementation requires a stack
that is at worst proportional to lg(elements).

Ta',
Juergen

--
\ Real name     : Jrgen Heinzl                 \       no flames      /

--



Wed, 30 Jan 2002 03:00:00 GMT  
 large array problem?


Quote:
>Maybe something in the WIN32 architecture needs to be done differenetly,
>since this works flawlessly (printing all 1M integers to my screen).
>Maybe you need a flag for your compiler to make sure it is ANSI compliant,
>and doesn't try any wierd optimizations?

ANSI does not require that single objects can be as large as 4M bytes.

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Wed, 30 Jan 2002 03:00:00 GMT  
 large array problem?

writes

Quote:
>void main(void)
> {
>   long long_values[100000];   /* when changed to 1000000 it crashes */

The amount of memory available on the system is irrelevant, how much
memory has been allocated as stack space?

Secondly sizeof(long) could be anything and on a system with 8-bit chars
and 64 bit longs your array of 1000000 will require just under 8Mbytes
which is a hell of a lot of stack space (and would hardly make your
program popular on a multi-tasking system.

Quote:

> long elements = 100000, i;     /* when changed to 1000000 it crashes */
> printf("Here we go \n");
> getchar();

> for (i = 0; i < elements; i++)
>  long_values[i] = i;

> printf("done initializing array\n");
> getchar();

> qsort(long_values, elements, sizeof(long),
>      (int (*) (const void *, const void *)) compare_long);

> for (i = 0; i < elements; i++)
>     printf("%d ", long_values[i]);

> putchar('\n');

> }

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Wed, 30 Jan 2002 03:00:00 GMT  
 large array problem?

Quote:
> hi,

> This code works fine with an array size of 100,000 when I try to increase
> the size to 1,000,000 it crashes. After debugging it indicates a stack
> overflow has occured. Why is this happening? I have 64 meg of memory.P2 300
> mhz win95.The compiler is VC++ 5.0.

Under Win32, the compiler only reserves address space for a megabyte
(or so) of stack space by default.  Since integers are 4 bytes apiece,
that means your array is trying to use 4 megabytes.  You can tell the
linker to reserve more address space for the stack with the /stack
option to the linker.
--



Wed, 30 Jan 2002 03:00:00 GMT  
 large array problem?
<< long long_values[100000]; >>

Based on the error message, this array is clearly too big to place on your
stack, on your system.

Do it this way:

long *long_values = malloc(100000 * sizeof long);

--

Paul Lutus
www.arachnoid.com



<snip>

--



Wed, 30 Jan 2002 03:00:00 GMT  
 large array problem?

   long *long_values = malloc(100000 * sizeof long);

Parentheses are required for a type name:
   long *long_values = malloc(100000 * sizeof (long));
or
   long *long_values = malloc(100000 * sizeof *long_values);

--
"You know, they probably have special dorms for people like us."
--American Pie
--



Wed, 30 Jan 2002 03:00:00 GMT  
 large array problem?
Thank you for this correction.

--

Paul Lutus
www.arachnoid.com


Quote:

>    long *long_values = malloc(100000 * sizeof long);

> Parentheses are required for a type name:
>    long *long_values = malloc(100000 * sizeof (long));
> or
>    long *long_values = malloc(100000 * sizeof *long_values);

> --
> "You know, they probably have special dorms for people like us."
> --American Pie
> --


--



Thu, 31 Jan 2002 03:00:00 GMT  
 large array problem?

Quote:

> int compare_long(long *a, long *b)
> ...
>  qsort(long_values, elements, sizeof(long),
>       (int (*) (const void *, const void *)) compare_long);

Quite apart from the stack problem, you're invoking qsort incorrectly.
Instead of trying to pretend that an incommensurate function has the
specified type, you should define the comparison function per spec
and convert the void* arguments to long* as required.
--



Fri, 01 Feb 2002 03:00:00 GMT  
 large array problem?
  You don't need to use malloc, your function (main) is not recursive, and
you can just make a variable static or extern, like

static long long_values[1000000];

  Another possibility is to use a larger or expandable stack, look for
linker options for doing that.

  Michael

Quote:



>>    long *long_values = malloc(100000 * sizeof long);

>> Parentheses are required for a type name:
>>    long *long_values = malloc(100000 * sizeof (long));
>> or
>>    long *long_values = malloc(100000 * sizeof *long_values);

--



Fri, 01 Feb 2002 03:00:00 GMT  
 large array problem?


Quote:
>  You don't need to use malloc, your function (main) is not recursive, and
>you can just make a variable static or extern, like

>static long long_values[1000000];

As long as the compiler doesn't put this directly in the executable and
supports large static objects.

Quote:
>  Another possibility is to use a larger or expandable stack, look for
>linker options for doing that.

malloc() is generally the best appoach for creating large objects.

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


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



Sat, 02 Feb 2002 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Very large array problem

2. problem with large arrays

3. problem with large arrays

4. problem: seg fault due to very large array?

5. Problem with large arrays

6. problem on large object array.

7. A problem with very large arrays and memory in DOS-based C

8. Problems in array larger than 1MB.

9. Help !!! Concat small array into large array

10. Arrays [256][256] --- error Array size too large!

11. Declaring Large Arrays in C#

12. Creating large C# Null Arrays

 

 
Powered by phpBB® Forum Software