cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : Memory could not be "Read"-still
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  Memory could not be "Read"-still

Cbas (11)
I have a function which i run three times. Its called float* departments(int a,float b, float c[], float d) and returns a 320 array
It runs perfectly twice, but on the third time window's gives me the error message:

The instruction at "0x7c93426d" reference memory at "0xc2940f10". The memory could not be "read" <Ok> to terminate, <cancel> to debug

I have no idea what is causing this crash,its obviously a memory thing(i think) but how do i fix it?
| Last edited on
Duoas (1087)
You are trying to write or read memory that doesn't belong to you. Look for anyplace that you can access memory outside the proper bounds. (If you can't see it, post your code.)
|
Cbas (11)
How do i access memory outside the proper bounds?
I could post the the code, but its pretty long??
Thanks Duoas,by the way
1
2
3
4
5
6
7
8
9
10
11
12
13
float *department(int tot_ppl,float arrvle [],int mach, float prosR,float simclock)
{....
depart[]={320 vaules}
return depart;
}
float *cutting(20,time,4,0.5,time[0])
//depart its then split into two different arrays: depart1, depart2
...
float *welding(22,depart1,3,0.0755,depart1[0])
float *drilling(21,depart2,5,0.025,depart2[0])
...

| Last edited on
Duoas (1087)
You cannot return a static array. Eg:
1
2
3
4
5
6
7
8
9
10
float *make_float_array()
  {
  float result[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  return result;   // <-- BAD!  DOES NOT WORK
  }

int main()
  {
  float *ptr = make_float_array();
  }

The reason is that result[] ceases to exist when the function terminates, and ptr points to memory you do not own.

The fact that it worked a couple of times is pure luck.
|
Cbas (11)
Thank you again...
But how do i return an array then??
|
Zaita (933)
1
2
3
4
5
6
7
8
9
10
11
12
float *make_float_array()
  {
  float *result =new float[10];
   // fill array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  return result;   // now Ok.
  }

int main()
  {
  float *ptr = make_float_array();
  delete [] ptr; // Free your memory
  }
|
Cbas (11)
THANK YOU THANK YOU
Just one more thing.
My function, as well as returning an array, also outputs a file-the file name is specified by user.
The fucntion also runs three times, with the the option of running more if the user is not satisfied
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
float *make_float_array()
  {
  float *result =new float[10];
char filename[FILENAME_MAX];
cout<<"Please enter a filename where you would like the CSV file to be written\n And dont forget to type .CSV at the end"<<endl;
cin>>filename;
ofstream fileOUT(filename);
// fill array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
...
fileOUT<<"Number of People in the System"<<","<<endl;
return result;  
  }
int main ()
{
//while loop
float *run1 = make_float_array();
information1 = run1[10]
delete[] *run1;
float *run2 = make_float_array();
information1 = run2[10]
delete[] *run2;
float *run3 = make_float_array();
information1 = run3[10]
delete[] *run2;
cout<<"Again";//if yes, run loop agian

The program loops, so the CSV file eventually has about 320 entries in three colums


The problem is, on about the 2nd loop(the 4th CSV created) The memory error occures. I Find if i delete any files in my working folder it seems to run maybe 3 times before i get the "Windows has encountered an error and has to end the program"

Any ideas why??
Thank you!
|
jsmith (187)
Three possible problems.

1) To free the memory, you should do
delete [] run1; // without the asterisk

2) Line 24 is deleting run2 a second time. I think you meant run3.

3) Index [10] of the arrays is outside the bounds of the arrays. Since you do new float[10], the valid indices range from 0...9.
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us