|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Re: Accurancy and Memory Allocation C++,
Quote:
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
||||
|
||||
|
Re: Accurancy and Memory Allocation C++,
Quote:
|
|
#5
|
||||
|
||||
|
Re: Accurancy and Memory Allocation C++,
Quote:
Code:
int a[4][5][6]; a[1][2][3] = 4; |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
||||
|
||||
|
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). |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
||||
|
||||
|
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. |
|
#10
|
|||
|
|||
|
Re: Re: Accurancy and Memory Allocation C++,
Quote:
Replace 'most' with 'some'. |
|
#11
|
|||
|
|||
|
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. |
|
#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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Accurancy and Memory Allocation C++, |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|