Thread: Three-Dimensional Array... Error while compiling

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    22

    Three-Dimensional Array... Error while compiling

    Hi there,


    I am trying to compile the code shown below but an error appears while compiling. I have defined three functions in this code. The first function, * Array3D(), is used to allocate and initialize a three dimensional array (array3d) with zeros. The second function, DisplayArray3D(), is used to display on the screen the array3d already created. The third function, FreeArray3D(), is used to free array3d up from the memory. I think rest of the code is self-explanatory.

    Any help/comment will be greatly appreciated. Thank you vey much.


    Code:
    typedef struct array3D_type {
        double_t *** array;
        int64_t size[3];
    } array3D_t;
    
    
    array3D_t * Array3D(int64_t sizeDim1, int64_t sizeDim2, int64_t sizeDim3);
    void DisplayArray3D(array3D_t array3D);
    void FreeArray3D(array3D_t * array3D);
    
    
    
    array3D_t * Array3D(int64_t sizeDim1, int64_t sizeDim2, int64_t sizeDim3)
    {
        if (sizeDim1<=0)
        {
            fprintf(stderr, "Array3D::Error: sizeDim1 must be positive and non-zero.\n");
            exit(EXIT_FAILURE);
        }
        
        if (sizeDim2<=0)
        {
            fprintf(stderr, "Array3D::Error: sizeDim2 must be positive and non-zero.\n");
            exit(EXIT_FAILURE);
        }
        
        if (sizeDim3<=0)
        {
            fprintf(stderr, "Array3D::Error: sizeDim3 must be positive and non-zero.\n");
            exit(EXIT_FAILURE);
        }
        
        array3D_t * array3D;
        
        array3D->size[0]=sizeDim1;
        array3D->size[1]=sizeDim2;
        array3D->size[2]=sizeDim3;
        
        array3D->array=malloc(sizeDim1*sizeof(double_t **));
        
        if (array3D->array==NULL)
        {
            fprintf(stderr, "Array3D::Error: Out of memory.\n");
            exit(EXIT_FAILURE);
        }
        
        for (int64_t i=0; i<sizeDim1; i++)
        {
            array3D->array[i]=malloc(sizeDim2*sizeof(double_t *));
            
            if (array3D->array[i]==NULL)
            {
                fprintf(stderr, "Array3D::Error: Out of memory.\n");
                exit(EXIT_FAILURE);
            }
        }
        
        for (int64_t i=0; i<sizeDim1; i++)
            for (int64_t j=0; j<sizeDim2; j++)
            {
                array3D->array[i][j]=malloc(sizeDim3*sizeof(double_t));
                
                if (array3D->array[i][j]==NULL)
                {
                    fprintf(stderr, "Array3D::Error: Out of memory.\n");
                    exit(EXIT_FAILURE);
                }
            }
        
        for (int64_t i=0; i<sizeDim1; i++)
            for (int64_t j=0; j<sizeDim2; j++)
                for (int64_t k=0; k<sizeDim3; k++)
                    array3D->array[i][j][k]=0.;
        
        return array3D;
    }
    
    
    
    void DisplayArray3D(array3D_t array3D)
    {
        printf("Array3D::Size=(%lli,%lli,%lli).\n", array3D.size[0], array3D.size[1], array3D.size[2]);
        
        for (int64_t k=0; k<array3D.size[2]; k++)
        {
            printf("\nArray3D(:,:,%lli)=\n", k+1);
            
            for (int64_t i=0; i<array3D.size[0]; i++)
            {
                printf("R%lli:\t",i+1);
                
                for (int64_t j=0; j<array3D.size[1]; j++)
                    printf("%lf\t", array3D.array[i][j][k]);
                
                printf("\n");
            }
        }
    }
    
    
    
    void FreeArray3D(array3D_t * array3D)
    {
        for (int64_t i=0; i<array3D->size[0]; i++)
            for (int64_t j=0; j<array3D->size[1]; j++)
                free(array3D->array[i][j]);
        
        for (int64_t i=0; i<array3D->size[0]; i++)
            free(array3D->array[i]);
        
        free(array3D->array);
        
        free(array3D);
    }
    
    
    
    
    
    int main (void)
    {
        array3D_t * s=Array3D(5,3,4);
        
        for (int i=0; i<s->size[0]; i++)
            for (int j=0; j<s->size[1]; j++)
                for (int k=0; k<s->size[2]; k++)
                    s->array[i][j][k]=5/3.*i-2*j+7*k;
        
        DisplayArray3D(*s);
        
        FreeArray3D(s);
        
        return 0;
    }

    Thank you!

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    The error says that program received signal: "EXC_BAD_ACCESS" while executing this line:

    Code:
    array3D->size[0]=sizeDim1;
    Any idea about the error and how it could be solved?

    I have another question... when executing FreeArray3D(s) I am really freeing s up from the memory?

    Thank you.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You can't just declare a pointer to an object and start writing to the garbage address it is pointing to. A pointer is an address. Make it point to a valid memory location by either using static allocated objects, or dynamic allocated objects via malloc.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Dear claudiu,

    Thank you very much for reply.

    Could you please be more precise? I am a beginner in the programming world.

    I mean, what do you mean by I am declaring a pointer to an object and starting to write to the garbage address it is pointing to.

    Thank you!

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by adarpodracir View Post
    Dear claudiu,

    Thank you very much for reply.

    Could you please be more precise? I am a beginner in the programming world.

    I mean, what do you mean by I am declaring a pointer to an object and starting to write to the garbage address it is pointing to.

    Thank you!
    at line 35 what does array3D point to ??

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Dear sparkomemphis,

    Thanks for reply. I think it is pointing to array3D_t. Then I am assigning the value of sizeDim1 to size[0].

    Code:
    (* array3D).size[0] = sizeDim1;
    If this cannot be done. What method should I use to return an array3D_t pointer?

    Thank you.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by adarpodracir View Post
    Dear sparkomemphis,

    Thanks for reply. I think it is pointing to array3D_t. Then I am assigning the value of sizeDim1 to size[0]
    No, it's not pointing at anything:
    Code:
        array3D_t * array3D;
         
        array3D->size[0]=sizeDim1;
    That first line delcares a variable of type "pointer to type array3D_t", but it doesn't actually put a value there (it doesn't make it point anywhere you've specified). It's like doing this:
    Code:
    int x;
    
    printf("x is %d", x );
    What's x store? Who knows! You didn't make it store anything.

    You need to actually make it point to something you control before you start mucking with it.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by adarpodracir View Post
    The error says that program received signal: "EXC_BAD_ACCESS"
    Hint: That is not a compile error, that's a run-time error.
    A compile-time error is one that prevents the compiler from even being able to produce an executable at all, so there is nothing to crash or run in any way shape or form. E.g. if you misspelt return as retrun, that would be a compile-time error.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why a[i] is not compiling . array compilation error
    By nkrao123@gmail. in forum C Programming
    Replies: 17
    Last Post: 09-06-2011, 08:49 AM
  2. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  3. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  4. two dimensional array
    By saurav sarkar in forum C Programming
    Replies: 5
    Last Post: 08-04-2002, 11:12 AM
  5. two dimensional array
    By saurav sarkar in forum C Programming
    Replies: 0
    Last Post: 08-04-2002, 09:31 AM