Pointer to array within a structure? Bus error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • msl24
    New Member
    • Apr 2010
    • 3

    #1

    Pointer to array within a structure? Bus error?

    Thanks in advance to anyone/everyone who can help. I've written a code (see below) that works for small test values of N and M, say N = 50 and M = 30. However, I run into problems when I try to use large values of N = 401450280 and M = 32768. I can compile the code, but when I run it, I get a "bus error" that appears to be caused by the malloc line in function help.

    See snippets below:

    Code:
    struct coords{
       float *length;
    };
    
    void combos(struct coords *events, int M);
    float help(struct coords *events, int M);
    
    void main(){
    
       int N = 50;
       int M = 30;
    
       struct coords *events;
       events = malloc(N*sizeof(*events));
    
       combos(events, M);
    
    }
    
    void combos(struct coords *events, int M){
    
        help(combos, M);
    
    }
    
    float help(struct coords *events, int M){
    
        events->length = malloc(M*sizeof(float));
        /* code follows to assign floats to every element of array length */
    
    }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Does it allocate memory or returns null? And where is value of N used?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      You must check each return from malloc to see if there was an error (ie, if malloc returned NULL).

      Comment

      • msl24
        New Member
        • Apr 2010
        • 3

        #4
        My apologizes, a typo. In main, it should read

        Code:
        events = malloc(N*sizeof(*events));
        Instead of using events->length = malloc(M*sizeof (float)), I replaced it with

        Code:
        if ( (events->length = malloc(M*sizeof(float))) == NULL)
           {
                    printf("yikes!\n");
                    exit(1);
           }
        and "yikes!" doesn't print, so I think it's allocating space properly.

        Comment

        • msl24
          New Member
          • Apr 2010
          • 3

          #5
          I just checked each return from malloc, and I get null from the line
          events = malloc(N*sizeof (*events))
          within main. So I guess that's my problem. I'll have to find a way around that somehow, because unfortunately I need N that large.

          Thanks for all of your help!

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            It is always worth checking the size of the memory you are trying to allocate

            For N=401450280 and sizeof(struct coords) == 4 (which is likely sionce all it contains is a pointer. You are trying to allocate 401450280 x 4 = 1605801120 = ~1.5Gigabytes of data in a single block.

            And presumably you will allocated more memory for each pointer, say each coordinate has 2 values, size of float is normally so size of 2 floats is 8 so you would be allocating

            401450280 x 8 = 3211602240 = ~3Gigabytes in 401450280 8 byte blocks.

            For every block there is likely to be some overhead too in system memory used to track the block for freeing.

            Does your computer have enough memory to copy with a minimum of 4.5 gigabytes of request memory?

            If its a 32bit system then probably not particularly on windows where the maximum amount of memory a process can have is 2 gigabytes.

            When dealing with large amounts of data you should look at this sort of thing first and design your program round the issue. My guess is you will probably have to use a file to hold the data and only read in a small amount of it at a time. A smaller cache of values in the program might then be used to speed everything up.

            Comment

            Working...