array problem.

This is a discussion on array problem. within the C Programming forums, part of the General Programming Boards category; Dear All I have the following code which gives a segmentation fault. I dont think the array size is too ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    array problem.

    Dear All

    I have the following code which gives a segmentation fault. I dont think the array size is too large to result in a segmentation fault.

    int xdim = 36;
    int ydim = 256;
    int zdim = 256;

    double ***something = (double ***)malloc(xdim * sizeof(double **));
    for(i=0; i< xdim; i++)
    something[i] = (double **)malloc(ydim * sizeof(double *));

    for(j=0; j< ydim; j++)
    something[i][j] = (double *)malloc(zdim * sizeof(double));

    for(i=0; i< xdim; i++)
    for(j=0; j< ydim; j++)
    for(k=0; k< zdim; k++)
    something[i][j][k] = 1.0;


    Thanks in advance,
    Prasad.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Next time use code tags btw. They are there for a reason and display a healthy amount of respect for those you are asking to help you.

    A double is usually 8 bytes I believe.... Let's see.....8 bytes * 36 * 256 * 256.......

    /me pulls out his calculator.

    Hmm..... 18,874,368 bytes. That is.... um..... 18,432 KB..... which is..... 18 MB.

    Mmkay, so you're allocating 18 MB of data, but you're not checking if malloc() is failing or not. Why?
    Last edited by MacGyver; 08-21-2007 at 01:53 AM. Reason: Unable to spell....

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    added allocation failure

    My apologises for not adding the code tags.

    I added the allocation check to make no difference. I still get the sedmentation fault and not the memory allocation failure message. The full code is here.

    Code:
    int xdim = 36;
    int ydim = 256;
    int zdim = 256;
    
    double ***something = (double ***)malloc(xdim * sizeof(double **));
    for(i=0; i< xdim; i++)
    something[i] = (double **)malloc(ydim * sizeof(double *));
    
    for(j=0; j< ydim; j++)
    something[i][j] = (double *)malloc(zdim * sizeof(double));
    
    if(something == NULL)
    {
    printf("Couldnt allocate memory");
    exit(0);
    }
    
    for(i=0; i< xdim; i++)
    for(j=0; j< ydim; j++)
    for(k=0; k< zdim; k++)
    something[i][j][k] = 1.0;
    Thank you for your reply.
    Cheers,
    prasad.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    solved.

    Thank you for your reply...I solved the problem.

    Code:
    for(i=0; i< xdim; i++)
    something[i] = (double **)malloc(ydim * sizeof(double *));
    
    for(j=0; j< ydim; j++)
    something[i][j] = (double *)malloc(zdim * sizeof(double));
    should be

    Code:
    for(i=0; i< xdim; i++)
    something[i] = (double **)malloc(ydim * sizeof(double *));
    
    for(i=0; i< xdim; i++)
    for(j=0; j< ydim; j++)
    something[i][j] = (double *)malloc(zdim * sizeof(double));
    Thank you for your help.

    Cheers,
    Prasad..

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Congrats on solving it, but just as an fyi, your allocation check you did was kind of late. If malloc() failed, you would still be getting a seg fault becase you'd be derefencing and using a NULL pointer.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Check the FAQ on casting malloc in C
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    thank you both for your assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21