Results 1 to 6 of 6

Thread: Large Char arrays

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60

    Large Char arrays

    Is there any way I can declare an array of char that is about 4,000,000 elements long? When I try to do so, the program crashes.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    OS? Compiler?

    For Windows, you can't have more than a megabyte in stack data, so you'll need to do something like:
    Code:
    char *ptr = new char[4000000];
    
    // ...
    
    delete[] ptr;
    This will put it into heap memory, which has more space.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    At 4 MB, if you don't care about portability, you'll be best of using VirtualAlloc, which is designed to allocate very large amounts of memory.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Anytime you use arrays of that size, you get problems. Almost without exception you are better off using an external agent (like a database) to handle your data. The database developers have found most of the gotchas and know how to get around them. You won't.

    CB is right - if you call malloc, your system may thrash for 20 seconds even if the memory allocation completes successfully.
    This only only one of dozens of problems you'll encounter.
    You have to use long int (int in MSVC++) to reference elements of an array that large. And so on.

  5. #5
    New Member
    Join Date
    Oct 2002
    Posts
    2
    Urgh, I'll stick with malloc for now, it seems to work well enough. Thank y'all

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    VirtualAlloc is guaranteed to scare any casual onlooker off.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width