Thread: Help!Maximum array size in C?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Question Help!Maximum array size in C?

    I need to sort a long int array which size at 300000 in C. When I define like this:

    # define SIZE 300001
    long int array[SIZE]

    in my code, complier say that my array is too large.

    Anybody know how do expand the limitation? And what's the real limitation?


    Thanks a lot


    robin
    Last edited by robin; 04-02-2005 at 04:04 PM.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    To use large arrays you have to use dynamic memory. Look up malloc and free.

    The maximum size of an array is different from machine to machine, depending on how much memory it has.

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    are you a VB programmer? your array declaration should be :

    long int array[SIZE];

    works on my machine. I'm not 100% sure about limitations though so i'll leave it for someone else..
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    11
    Sorry for mis typing.
    it shound be array[SIZE].
    Shakti ,do you mean that I must use malloc to allocate the memory one by one? Or I can allocate at a time for 300000 long int array? Can you show an example?


    Thanks

    robin

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps if you said which OS and compiler you were using, we could help further.
    300K for a single array seems a bit on the small side for a limitation on a 32 bit machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    11
    I just use TC as a complier.

    OS is windows XP,machine seems good in the school computer lab.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Welcome to the world of 16 years ago. I mean...
    Welcome to the world of 16 bits. That is to say...
    Get a new compiler.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    If you want to use dynamic memory simply declare a pointer to the type you want to allocate in this case int, and call the malloc() function with the argument being the number of bytes you want the "array" to be. In this case you need to get the sizeof a single integer and multiply it by how big you want it to be (i.e. the number of cells):

    Code:
    int* array;
    array = malloc(sizeof(int) * SIZE);
    if (array == NULL)
    {
          fprintf(stderr,"Could not allocate that much memory");
          return 1;
    }
    
    /* do something with array */
    free(array);
    Make sure you free anything you malloc'ed otherwise you will get memory leaks.
    Last edited by 0rion; 04-02-2005 at 10:01 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It won't matter. They'll hit their compiler's 64K limit. Dynamic allocation won't get around that.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Smile WELL Mr.16

    Quote Originally Posted by quzah
    Welcome to the world of 16 years ago. I mean...
    Welcome to the world of 16 bits. That is to say...
    Get a new compiler.

    Quzah.
    so what if it is 16 years old
    we are discussing logic behind programs dear.it dosent matter if it is 16 years old or 1 day old. c programming needs C that's it. but i do agree that there will be portability problem. ive started using VC++. but still TC is much better

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by coolshyam
    so what if it is 16 years old
    we are discussing logic behind programs dear.it dosent matter if it is 16 years old or 1 day old. c programming needs C that's it. but i do agree that there will be portability problem. ive started using VC++. but still TC is much better
    That's why you're an idiot. Because you don't pay attention to what I just said. It's an old compiler. It is limited by its age. Do you actually know what you scan store in 16 bits? Unsigned? Roughly 64000. OH MY GOD! Does that mean you can't have the number 300001?

    You bet your ass you can't, "dear". There's your "logic behind it", "dear".

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM