Thread: Arrays with very large Indices

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Arrays with very large Indices

    I am working on a simulation program that requires a large amount of variables in an array, such as:

    int Testp = 64;
    int taustp = 4096;
    float *sigma = new float[(Testp+1)*(taustp+1)*sizeof(float)];

    and then when I assign sigma later, I get a Segmentation Fault. This only happens when my index reaches about 16,000. Do I need to declare my indices somehow as a Long?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No idea about the segmentation fault, beyond that int definitiely suffices for 16000, but you new[] calculates the element size on its own - you're currently allocating sizeof(float) (usually 4) times the space you intended.

    Can you post the code that actually segfaults?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you're using some crappy old 16-bit DOS compiler which limits the amount of any single allocation to 64K.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >float *sigma = new float[(Testp+1)*(taustp+1)*sizeof(float)];
    You don't need sizeof(float) in there. Just:
    Code:
    float *sigma = new float[(Testp+1)*(taustp+1)];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Large arrays, malloc, and strcmp
    By k2712 in forum C Programming
    Replies: 1
    Last Post: 09-24-2007, 08:22 PM
  2. Not enough room to store in arrays
    By Bri Rock in forum C++ Programming
    Replies: 6
    Last Post: 08-10-2004, 05:10 PM
  3. How large can arrays be?
    By overspray in forum C++ Programming
    Replies: 9
    Last Post: 07-21-2004, 02:25 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. arrays arrays arrays....
    By Jan79 in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2002, 07:16 AM