#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Greece
    Posts
    7
    Rep Power
    0

    Accurancy and Memory Allocation C++,


    Hello Everyone,
    1)I am dealing with a very important problem. I am writing a code for geophysical perpose and i can't have the accurancy i need.
    This codes takes a series of data sets and makes some computations. Just for testing, all these data sets are the same. So after these computations (there are matrix which i invert them, multiplie with other etc) logically the resullts would be the same (because all the data sets are the some). But no. In SOME elements of the matrixs i have a differnce about (0.0000001).
    These are not random erros, as the occur at the same element every time. I tried to make these matrixs float,double long double, but nothing happend. It is not code error, because every time i use a different matrix (p.s. double for long double) the error exist, but in different element. I used every compiler exist (Borland C++ Builder 6.0, Microsoft .Net 2003, GNU C, Intel Compiler 7.1), but nothing happend. I wonder if anyone had the time to look into my code and tell what is wrong.
    2)I aslo have a problem about allocation of matrix.
    I use the following code

    const int max_mess=100;
    const int max_sounding=50;

    float **initial_occam_model=new float*[max_mess];
    for (i=0;i<=max_mess;i++)
    {initial_occam_model[i]=new float[max_sounding];}

    When i try to deallocate

    for (i=0;i<=max_mess;i++)
    {delete initial_occam_model[i];}
    delete initial_occam_model;

    The program crushes.
    If u comment the //delete [] initial_occam_model; no crushes occur.
    Why?

    3)How can i allocate a 3-dimension array?

    4)Finally i use about 20 of those matrix, plus three 3 dimension arrays of size [100][100][50], all of them float. But there is not enough memory, so the program crushes.Why?

    Thanks for the time someone spent to read by message. Any help would be greatuful.
    Thanks again.
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: Accurancy and Memory Allocation C++,


    Originally posted by karaouli
    Just for testing, all these data sets are the same. So after these computations (there are matrix which i invert them, multiplie with other etc) logically the resullts would be the same (because all the data sets are the some). But no. In SOME elements of the matrixs i have a differnce about (0.0000001).
    These are not random erros, as the occur at the same element every time. I tried to make these matrixs float,double long double, but nothing happend.
    This is because all floating point formats (float, double, long double [which is no longer supported by MSVC++, btw]) do NOT store floats accurately. So, when you compare floats, you need to compare them by subtracting one from the other, and seeing if the absolute value of the result is less than epsilon, where epsilon is some very small number (small enough that you are satisfied the results are equal). Do a search on comparing floats and epsilon on google, and I'm sure some code will pop up. I've answered this concern already in this forum, so try a search here, as well.
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    One big problem I see is you are iterating to i <= max_mess! It should be i < max_mess!;

    Are your arrays always constant (meaning that it is fixed at compile time)? If so, don't dynamically allocate the memory, statically allocate it:

    Code:
    const int max_mess=100; 
    const int max_sounding=50; 
    
    float initial_occam_model[max_mess][max_sounding];
    C supports (to my knowledge) n dimensional arrays, simply add on. Doing it dynamically is a real pain, you always have to check that each memory section is initialized properly. I am not so great at C++ memory allocation, here is how I would do it in C (please notice how much more code than the above!):

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    const int max_mess=100; 
    const int max_sounding=50; 
    
    int main(){
        int i, allocated = 0, totalmem = 0;
    //    float initial_occam_model[max_mess][max_sounding];
        float **initial_occam_model; 
        void * ptr;
        
    //allocate
        initial_occam_model = (float **) malloc(max_mess * sizeof(float *));
        if (initial_occam_model == NULL){
            fprintf(stderr, "Unable to allocate memory for first dimension of array!\n");
            exit(1);
        }
        totalmem = max_mess * sizeof(float *);
    
        for (i=0;i<max_mess;i++){
            ptr = malloc(max_sounding * sizeof(float));
            if (ptr == NULL){
                fprintf(stderr, "Ran out of memory allocating %d sub array!\n", i+1);
                for (int j=0; j<allocated; j++){
                    //this is not necessary if you are going to exit
                    free(initial_occam_model[i]);
                }
                exit(1);
            }
            initial_occam_model[i] = (float *) ptr;
            totalmem += max_sounding * sizeof(float);
            allocated++;
        } 
        printf("Allocated %d bytes of memory\n", totalmem);
    
    //deallocate 
    
        for (i=0;i<max_mess;i++){
            free(initial_occam_model[i]);
        } 
        free (initial_occam_model); 
    
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: Accurancy and Memory Allocation C++,


    Originally posted by karaouli
    4)Finally i use about 20 of those matrix, plus three 3 dimension arrays of size [100][100][50], all of them float. But there is not enough memory, so the program crushes.Why?
    You are probably declaring these arrays in the main() function. When you do that, you are using the stack. By default, most compilers have this set to 256 Kb. Your array takes up 100*100*50*4 = almost 2,000 Kb. This exceeds the stack space. Remember, main() is a function just like any other...
  8. #5
  9. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: Accurancy and Memory Allocation C++,


    Originally posted by karaouli
    3)How can i allocate a 3-dimension array?
    In C++, simply do this:
    Code:
    int a[4][5][6];
    a[1][2][3] = 4;
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Greece
    Posts
    7
    Rep Power
    0

    What is Stack?


    Thanks everyone for the response time to asnwer me.
    1)Yes, all my arrays are declared in main function. I know, that global variables is a bad programming practice, so everything is in main. Is there any way to change this stack size?
    2)Basically, i just declare these array one time for the rest of the program. I generally, don't delete most of them. Idynamical allocate them just for speed (as i know, so the array be in a continue part of memory or something like that...).
    3)Finally, i would like to know how to dynamically allocate an array?


    Thanks Again.
  12. #7
  13. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: What is Stack?


    1. If you are exceeding stack space, then you shouldn't be declaring the arrays within a function. Dynamically allocate them. The only other option is to have them global, and like you said, global variables are usually a bad idea. I wouldn't bother attempting to change the stack size, 256 kb is plenty. Arrays simply should not be placed on the stack.

    3. That's what your code above does. You dynamically allocated an array of pointers. Then you set each of these pointers to equal the location of a dynamically allocated array of floats (a new array for each pointer).
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Greece
    Posts
    7
    Rep Power
    0
    All of my arrays are declared dynamically, in main, But i still have the problem. I changed the stack size, as you said in about 30MB, and now it works fine. But still, what exactly do you mean? How can i do what you are saying?
    Thanks.
  16. #9
  17. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    If changing the stack size solves your problem, then you are running out of stack space, which means you are using the stack for something very big - probably your arrays, which means you are not dynamically allocating all of them. Are you sure you are not mixing up the terms stack and heap?

    You should show us your source code. Not all of it - just the relevant section. Make it as small as possible, and compile it to ensure the problem remains, and then show us. Sometimes in doing this, you'll solve your own problem.
  18. #10
  19. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    11

    Re: Re: Accurancy and Memory Allocation C++,


    Originally posted by Jason Doucette
    You are probably declaring these arrays in the main() function. When you do that, you are using the stack. By default, most compilers have this set to 256 Kb.
    Replace 'most' with 'some'.
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Greece
    Posts
    7
    Rep Power
    0

    Ok, Now


    You were right. I didn't allocate all of my arrays. Besically the big ones, the 3d arrays, i didn't know how to allocate them. But searching in the net, i found how to do it. Now it works fine. No crushes, no changing the stack size. I made .cpp file with how to allocate-deallocate 1d, 2d and 3d arrays. Anyone interesting?
    Thank you Jason Doucette.

    P.S>Nice site you have, and nice software. Keep up the good work.
  22. #12
  23. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Thanks, and you're welcome. Btw, when a program ends abnormally, it's called a crash, not a crush. ;) And, BigBadBob is right, since I am not really sure how many compilers have the limit set to 256kb. In my experience, it is always set to this, but I have not used many compilers.

IMN logo majestic logo threadwatch logo seochat tools logo