Thread: Declaring huge Arrays

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    5

    Declaring huge Arrays

    I declared an array deskone[2600] and got an error "error c2900: huge array cannot be aligned to segment boundary". the error goes away when I reduced the elements in the array to 1900. I need this array to be 2600. how do I declare this without the error. any help is greatly appreciated

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If the array has automatic storage duration, it typically will be allocated storage on the stack, which tends to be limited, so declare the array to have static or allocated storage duration instead. To declare the array as having static storage duration, you would either declare it at file scope (in which case it becomes a global variable), or declare it static (in which case it can have local scope, though it retains some maintenance disavantage in being static). For allocated storage duration, you would use malloc and free and do manual memory management.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    Quote Originally Posted by laserlight View Post
    If the array has automatic storage duration, it typically will be allocated storage on the stack, which tends to be limited, so declare the array to have static or allocated storage duration instead. To declare the array as having static storage duration, you would either declare it at file scope (in which case it becomes a global variable), or declare it static (in which case it can have local scope, though it retains some maintenance disavantage in being static). For allocated storage duration, you would use malloc and free and do manual memory management.
    Thanks the array is global. I will use malloc

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by John Malloc View Post
    Thanks the array is global. I will use malloc
    If the array is global then you don't need to use malloc.

    You gave us very little information to go on. You didn't tell us the size of the array elements or where/how you declared it.

  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
    I figure that anything which regards 2600 as huge and talks about segments must be some fossil 16-bit compiler from 20 to 30 years ago.

    The first question would be why joining the current millennium isn't an option.
    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
    Dec 2016
    Posts
    5

    Error Using Malloc

    thanks everyone. the array is global in a glob.h file.
    insert
    Code:
    
    typedef struct
    {
    float time
    float key
    .
    .
    }Key_Type;
    
    originally also declared in same file
    extern Key_Type huge key_inputs[2600]; 
    I then declared in another file where key_inputs is used key.c
    key_type huge key_inputs[2600]; 
    
    this worked up to 1900 elements. throws a segmented boundary error at 2600. that hwy I tried to use malloc. Now I have
    in glob.h 
    extern Key_Type key_inputs[2600];
    and in key.c I have
    key_type key_inputs = (key_type *) malloc(2600 *sizeof (key_type) ;
    if (null==key_type)
    {
    fprintf(stderr, "malloc failed\n");
    return (-1);
    }
    Now this gives me 2 errors 1 warning
    key.c - error c2040: key_inputs different level of indirection
    syntax error c2143 missing ";" before if
    warning: c4307: "*": integral constant overflow

    any help on any of the two way is appreciated. need to get this working for an array of 2600 elements with with huge or malloc. huge worked up to 1900.
    thanks

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    What kind of machine are you programming? Is it 16-bits?

    You are still keeping the size of Key_Type a secret for some reason. We can now see that it holds at least 8 bytes. But how f'ing big is it?!

    If it's more than 25 bytes then multiplying it by 2600 will overflow an unsigned 16-bit int.

    "huge" is not a standard keyword, so I don't really know anything about it.

    For the malloc, your extern statement should be:
    Code:
    extern Key_Type *key_inputs;
    
    // Then in key.c:
    
    Key_Type *key_inputs = malloc(2600 * sizeof *key_inputs);

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Using Large Arrays In Turbo C | Dr Dobb's
    Make a note of the date at the top.

    Which compiler are you using? farmalloc might have a different name if you're using some other fossil compiler.
    What machine are you running the program on?
    What OS is running on that 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.

  9. #9
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    Quote Originally Posted by Salem View Post
    Using Large Arrays In Turbo C | Dr Dobb's
    Make a note of the date at the top.

    Which compiler are you using? farmalloc might have a different name if you're using some other fossil compiler.
    What machine are you running the program on?
    What OS is running on that machine?
    its a float array with 17 members. the code is complied in Microsoft C 1.52 compiler. runs under DOS. thanks to everyone who helped

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why is it running on DOS?
    DOS became obsolete for desktop computers about 25 years ago.
    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.

  11. #11
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    the number 2600 is the index to the array.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Huh?

    2600 is not an index to an array with 2600 elements.

    Valid indices run from 0 to 2599 for such an array.
    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.

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    For the old Microsoft huge arrays, if the total array size is greater than 128KB, then element size (in this case Key_Type) needs to be a power of 2 (2,4,8,16, ..., this is due to the way huge (segment : offset) addresses are calculated based on index). This may be the issue. What is the size of Key_Type? I'm guessing it's greater than 50 bytes? If so, pad Key_Type with a dummy member (perhaps call it pad) to make the size of Key_Type 64 bytes or 128 bytes.

    The "huge" version of malloc() is halloc(), prototype:

    Code:
    void huge *halloc(long n, size_t size);
    Last edited by rcgldr; 12-14-2016 at 11:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring Multidimensional Arrays
    By wob_b in forum C Programming
    Replies: 2
    Last Post: 11-10-2010, 01:38 PM
  2. problem in declaring dyanamic arrays in c
    By aqeel in forum C Programming
    Replies: 2
    Last Post: 09-23-2009, 12:10 AM
  3. Multiplying Huge numbers in integer arrays
    By invierno in forum C Programming
    Replies: 5
    Last Post: 04-11-2009, 08:40 PM
  4. Problem declaring large arrays
    By mattAU in forum C Programming
    Replies: 5
    Last Post: 09-28-2006, 05:47 AM
  5. declaring 2d arrays
    By toad in forum C Programming
    Replies: 2
    Last Post: 06-02-2002, 01:50 AM

Tags for this Thread