Thread: error in 2 consecutive assignments

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    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);

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Code:
    for(k=0; k<=latlength*lonlength; k++)
    should be
    Code:
    for(k=0; k < latlength*lonlength; k++)

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    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

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    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

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    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

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    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

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi everyone who helped. it all works fine now
    thank you very much!

    cdf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignments or challenges
    By Dogmasur in forum C Programming
    Replies: 12
    Last Post: 08-20-2008, 02:24 AM
  2. First Consecutive composites Help
    By ch4 in forum C Programming
    Replies: 10
    Last Post: 11-23-2007, 04:55 PM
  3. Replies: 3
    Last Post: 04-06-2007, 05:10 PM
  4. C++ programming assignments and tests
    By NeoNite in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2003, 05:41 AM
  5. Embedded assignments
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-28-2001, 10:58 AM

Tags for this Thread