Click here to Skip to main content
16,008,490 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get memory access violation error, please help me quickly!!!!

I wrote a code like this, kindly note my commented line of error

C++
float  fValue=0;

stpData ->dbpMG1G1_data = ( float  ** ) malloc (sizeof ( float * ) * nRows * nColumns);

while ( NULL != fgets( cpReadString, 6144, fStream ) && nRow < nRows )

{

while ( NULL != cpSubString && nColumn < nColumns )

{
  fValue =  atof(cpSubString) ; 

  stpData ->dbpMG1G1_data[nRow][nColumn] = fValue ; // here I am getting error	
			
  ++nColumn ;
  
}

++nRow ; 
 
}



I delibratly wrote the memory allocation code line outside the loops so as to
optimze the execution speed. Is it indexing problem or something else?



Complete debugg error notification:

Unhandled exception at 0x0042b9e2 in On_The_Fly-A350.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.




Thanks in advance!!!!!!!
Posted
Updated 12-Dec-12 0:24am
v2

You need a matrix of floats (you don't need (nRows * nColumns) of float pointers).
I think an easy approach would be (using data instead of stpData ->dbpMG1G1_data to simplify notation):
C
data = (float *) malloc( sizeof(float) * nRows * nColumns); // linear array of floats
//..
{
  fValue = atof(cpSubString) ; 
  int i = nRow * Columns + nColumn; // bidimensional to linear: [Row][Column] => [i]
  data[i] = fValue;
}
 
Share this answer
 
Comments
nv3 12-Dec-12 8:05am    
Good advice. +5.
Ajain A K 12-Dec-12 9:31am    
Dear CPallini,

Thanks a lot for very quick response and very useful answer.
CPallini 13-Dec-12 3:33am    
You are welcome.
You are allocating an 1-dimensional array with nRows * nColumns elements of pointers to float and casting this to a float**. This will not work (the total array size would match for floats in this case because sizeof(float) == sizeof(float *) for 32-bit builds, but accessing 2-dim members will cause access violations).

To fix the problem, you have two options:

1. Use a 1-dimensional array and calculate the index when accessing elements:
stpData->dbpMG1G1_data = (float *)malloc(sizeof(float) * nRows * nColumns);
// ...
stpData ->dbpMG1G1_data[nRow * nColumns + nColumn] = fValue;


1. Use a 2-dimensional array which must be allocated using a loop:
stpData->dbpMG1G1_data = (float **)malloc(sizeof(float *) * nRows);
int i;
for (i = 0; i < nRows; i++)
    stpData ->dbpMG1G1_data[i] = (float *)malloc(sizeof(float) * nColumns);

The first version is more performat and does not require freeing the memory inside another loop.
 
Share this answer
 
Comments
Ajain A K 12-Dec-12 7:26am    
Dear Jochen,

Thanks for quick reply, I tried ur first suggestion(1 diamentional array)
but I am getting error like this


cannot convert from 'float' to 'float *'



short ReadMG1G1Data( const char * cpFileName, int nRows, int nColumns, struct MG1G1_ *stpData )
{


float fValue=0;

stpData->dbpMG1G1_data = ( float* )malloc(sizeof(float) * nRows * nColumns);



stpData ->dbpMG1G1_data[nRow * nColumns + nColumn] = fValue; //cannot convert from 'float' to 'float *'


}



I want to follow your first suggestion only but is it possible this conversion here? OR

what else I can do to rectify this error, please help me
Jochen Arndt 12-Dec-12 7:34am    
You must also change the structure member from float** to float* (sorry, I forgot to mention that):
struct MG1G1_ {
float *dbpMG1G1_data;
};
If the structure definition is not under your control (e.g. from a header file of a library), you must use the 2nd suggestion.
Ajain A K 12-Dec-12 9:29am    
Jochen Arndt,

Thanks a lot very good answer, but I hope I will come back to u

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900