C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 5th, 2003, 05:42 PM
karaouli karaouli is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Greece
Posts: 7 karaouli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old August 5th, 2003, 06:10 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
Re: Accurancy and Memory Allocation C++,

Quote:
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.

Reply With Quote
  #3  
Old August 5th, 2003, 06:22 PM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 51 m 4 sec
Reputation Power: 21
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;
}
__________________

Left DevShed May 28, 2005. Reason: Unresponsive administrators.
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.

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

Reply With Quote
  #4  
Old August 5th, 2003, 06:23 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
Re: Accurancy and Memory Allocation C++,

Quote:
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...

Reply With Quote
  #5  
Old August 5th, 2003, 06:25 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
Re: Accurancy and Memory Allocation C++,

Quote:
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;

Reply With Quote
  #6  
Old August 6th, 2003, 09:25 AM
karaouli karaouli is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Greece
Posts: 7 karaouli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #7  
Old August 6th, 2003, 09:31 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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).

Reply With Quote
  #8  
Old August 6th, 2003, 02:37 PM
karaouli karaouli is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Greece
Posts: 7 karaouli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #9  
Old August 6th, 2003, 02:47 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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.

Reply With Quote
  #10  
Old August 6th, 2003, 04:42 PM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Re: Re: Accurancy and Memory Allocation C++,

Quote:
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'.

Reply With Quote
  #11  
Old August 6th, 2003, 06:40 PM
karaouli karaouli is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Greece
Posts: 7 karaouli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #12  
Old August 7th, 2003, 09:00 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Accurancy and Memory Allocation C++,


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |