C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-14-2009, 01:23 PM   #1
Registered User
 
Join Date: Mar 2009
Posts: 109
error in 2 consecutive assignments

Hi there,

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   Reply With Quote
Old 04-14-2009, 01:45 PM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Code:
    	vars2 = (float**) malloc(1* sizeof(float));
should be
Code:
    	vars2 = (float**) malloc(1* sizeof(float *));
If this is a 64-bit machine, it will make a difference.


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   Reply With Quote
Old 04-14-2009, 01:52 PM   #3
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Code:
for(k=0; k<=latlength*lonlength; k++)
should be
Code:
for(k=0; k < latlength*lonlength; k++)
itCbitC is offline   Reply With Quote
Old 04-14-2009, 04:38 PM   #4
Protocol Test Engineer
 
ssharish2005's Avatar
 
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 )
         { }
and

Code:
free(vars2);
free(rh_vals);
free(varnames2);
you just can free like this. This will result in a memeory leak. Try using the valgrind tool (assuming that your on Unix platform). Thats would give you more information. You better sweapup properly before you leave mean. Or you end up lots of memory leak. As you mentioned that you working on a large amount of code. Doing this will avoid lots of problems in the futhure.

-ssharish
__________________
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
ssharish2005 is offline   Reply With Quote
Old 04-15-2009, 01:49 AM   #5
Registered User
 
Join Date: Mar 2009
Posts: 109
Quote:
Originally Posted by matsp View Post
Code:
    	vars2 = (float**) malloc(1* sizeof(float));
should be
Code:
    	vars2 = (float**) malloc(1* sizeof(float *));
If this is a 64-bit machine, it will make a difference.


Same applies to:
Code:
	varnames2 = (const char**) malloc(1* sizeof(char));
--
Mats
Hi Mat, thanks for helping< it is a 64/bit machine,
thank you
cfdprogrammer is offline   Reply With Quote
Old 04-15-2009, 01:50 AM   #6
Registered User
 
Join Date: Mar 2009
Posts: 109
Quote:
Originally Posted by itCbitC View Post
Code:
for(k=0; k<=latlength*lonlength; k++)
should be
Code:
for(k=0; k < latlength*lonlength; k++)
Hi there, thanks for the hint;
cfd
cfdprogrammer is offline   Reply With Quote
Old 04-15-2009, 01:54 AM   #7
Registered User
 
Join Date: Mar 2009
Posts: 109
Quote:
Originally Posted by ssharish2005 View Post
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 )
         { }
and

Code:
free(vars2);
free(rh_vals);
free(varnames2);
you just can free like this. This will result in a memeory leak. Try using the valgrind tool (assuming that your on Unix platform). Thats would give you more information. You better sweapup properly before you leave mean. Or you end up lots of memory leak. As you mentioned that you working on a large amount of code. Doing this will avoid lots of problems in the futhure.

-ssharish
Hi Sharish,
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   Reply With Quote
Old 04-15-2009, 02:14 AM   #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   Reply With Quote
Reply

Tags
1d array, 2d array, assignment, malloc

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22