![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 109
| error in 2 consecutive assignments within an extensive code that I am developing I obtained a very strange behavior from the code itself, and I cannot figure out what it it that is not done correctly. Specifically (code reported down here), I allocate certain arrays (2d and 1d) dynamically with standard dynamic allocation using malloc, and right after I assign them with the corresponding values. The problem here comes from the first assignment of the 2D arrat "vars2[][]" through the 1D array rh_vals[] assigned elsewhere. With the first assignment within a "while loop" I verify the correctness of it by printing the "vars2[][]" and all is right, however, once I exit the while loop and I printf again "vars2[][]", here it is when I do not get the same array. I hope someone can help although the sample code may seem large best CFD Code:
int i,j,k;
int *dims;
int *vardims;
static char varname[NC_MAX_NAME+1];
const char **varnames2; //array of pointers to chars
float *rh_vals; //vector
float **vars2; //matrix
int lonlength, latlength; //constants
lonlength = 64;
latlength = 512;
nvars = 126;
// Dynamic allocation of the arrays to pointers char **varnames and float **vars;
// This line only allocates the number of entries but NOT the length of the entries:
varnames2 = (const char**) malloc(1* sizeof(char));
varnames2[0] = (const char*) malloc( 12 * sizeof(char));
rh_vals = (float*) malloc( latlength*lonlength * sizeof(float));
vars2 = (float**) malloc(1* sizeof(float));
vars2[0] = (float*) malloc( latlength*lonlength * sizeof(float));
strncpy(varnames2[0], "T", 1);
//WHILE LOOP starts HERE
n = 0;
while(n < nvars){
rh_val[n] = dumb_function(rh_val); //This function to find rh_vals isn't reported
varname = dumb_function(varname); //This function to assign varname isn't reported
strncpy(varnames2[n], varname, sizeof(varname));
if( strncmp(varnames2[n],"T",1) == 1){
k=0;
for(i=0; i<lonlength; i++){
for(j=0; j<latlength; j++){
vars2[0][k] = rh_vals[k];
//FIRST PRINT TO CHECK VARS2
printf("k=%d rhvals[%d]: %f\n",k, k,vars2[0][k]);
k++;
}//end for j
}//end for i
}end if
n++;
}//END OF WHILE LOOP HERE.
//ERROR HERE!!!!
for(k=0; k<=latlength*lonlength; k++){
if(vars2[0][k] != 0.0)
//SECOND PRINT TO CHECK VARS2
printf("k=%d rhvals[%d]: %f\n",k, k,vars2[0][k]);
}
//FREE DYN MEMORY:
free(vars2);
free(rh_vals);
free(varnames2);
|
| cfdprogrammer is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: vars2 = (float**) malloc(1* sizeof(float)); Code: vars2 = (float**) malloc(1* sizeof(float *)); Same applies to: Code: varnames2 = (const char**) malloc(1* sizeof(char)); Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Code: for(k=0; k<=latlength*lonlength; k++) Code: for(k=0; k < latlength*lonlength; k++) |
| itCbitC is offline | |
| | #4 |
| Protocol Test Engineer Join Date: Sep 2005 Location: fseek(UK)
Posts: 1,316
| I am a but confused here. Are you trying to do something like this Code: varnames2 = (const char**) malloc(1* sizeof(char));
varnames2[0] = (const char*) malloc( 12 * sizeof(char));
if( ( varnames2 = (const char**) malloc(12 * sizeof(char * ) ) ) != NULL )
for( i=0; i < 12; i++ )
if( ( varnames2[i] = (const char*) malloc( 12 ) ) != NULL )
{ }
Code: free(vars2); free(rh_vals); free(varnames2); -ssharish
__________________ Life is like riding a bicycle. To keep your balance you must keep moving - Einstein |
| ssharish2005 is offline | |
| | #5 | |
| Registered User Join Date: Mar 2009
Posts: 109
| Quote:
thank you | |
| cfdprogrammer is offline | |
| | #6 |
| Registered User Join Date: Mar 2009
Posts: 109
| |
| cfdprogrammer is offline | |
| | #7 | |
| Registered User Join Date: Mar 2009
Posts: 109
| Quote:
thank you for replying Is the first part of the code that you suggested the way to allocate memory for an array of pointers? Also, if free(vars2) etc. is not the correct way to free, should I do it within a loop? something like: Code: for( i=0; i < 12; i++ )
free( varnames2[i])
thank you again | |
| cfdprogrammer is offline | |
| | #8 |
| Registered User Join Date: Mar 2009
Posts: 109
| Hi everyone who helped. it all works fine now thank you very much! cdf |
| cfdprogrammer is offline | |
![]() |
| Tags |
| 1d array, 2d array, assignment, malloc |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Assignments or challenges | Dogmasur | C Programming | 12 | 08-20-2008 02:24 AM |
| First Consecutive composites Help | ch4 | C Programming | 10 | 11-23-2007 04:55 PM |
| Looking for tutorials with assignments for a superdoubleplus beginner... | Eye in the Sky | C++ Programming | 3 | 04-06-2007 05:10 PM |
| C++ programming assignments and tests | NeoNite | C++ Programming | 1 | 06-05-2003 05:41 AM |
| Embedded assignments | Unregistered | C Programming | 1 | 08-28-2001 10:58 AM |