malloc for long long array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mollwollfumble
    New Member
    • Apr 2010
    • 2

    #1

    malloc for long long array?

    I'm a novice C programmer and am getting stuck on the insistence of malloc on using "size_t" for data array length. I want to use:
    Code:
    float *array
    long long int n
    array = malloc(n*sizeof(float))
    C insists on truncating my "long long int" to "size_t". So perhaps I have to redefine "size_t" or use an alternative to malloc. But how?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The parameter to malloc has type size_t. Depending on the platform this is usually defined as one of

    signed int
    unsigned int
    signed long
    unsigned long

    There is no alternative to malloc and redefining size_t isn't possible/wont help because malloc has already been compiled with size_t defined as supplied.

    If you need to use a long long for an array of floats then you are trying to allocate in excess of 16Gbytes of memory in one block.

    Do you have that much ram available (virtual or physical) on your system?
    Does you OS support having a single contiguous block of memory that size?

    Perhaps you need to consider designing a more complex scheme for handling your memory requirements.

    Comment

    • mollwollfumble
      New Member
      • Apr 2010
      • 2

      #3
      > The parameter to malloc has type size_t. Depending on the platform this is usually defined as one of ...

      On Visual Studio 2008 is it unsigned int?

      > If you need to use a long long for an array of floats then you are trying to allocate in excess of 16Gbytes of memory in one block.

      Very good point. At present, the program crashes at an array of length about 64 million. That's nowhere near long enough, but I don't need to go larger than about a Gbyte.

      > Perhaps you need to consider designing a more complex scheme for handling your memory requirements.

      Yes, but in the meantime I want to push the present program to its limit, whatever that is.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        If you are using Visual Studio consider a memory mapped file.

        Reference here.

        Effectively a memory mapped file lets you use a file as though it were memory so you can access it through a pointer and you can have a large amount of data because it is stored on the hard disk. The only limitation is that normally you would not have the entire file mapped at the same time but 1 or 2 pages on the file so you would need to devise a scheme to handle that efficiently.

        Comment

        Working...