CXX0030: Error: expression cannot be evaluated
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: CXX0030: Error: expression cannot be evaluated

  1. #1
    Join Date
    Feb 2009
    Posts
    2

    Post CXX0030: Error: expression cannot be evaluated

    Im writing a script to encode a string using a 2d dynamic array with the pattern of spiral data.

    here's my work so far:
    Code:
    	int irow, icolumn, x_pos_p, x_pos_m, y_pos_p, y_pos_m;
    	char str_input[100];
    	string str_reformed, str_input_str, rowscols;
    	vector<string> rcstore;
    
    	getline(cin, str_input_str);
    	strcpy(str_input, str_input_str.c_str());
    	getline(cin, rowscols);
    	rcstore = explode(rowscols, " "); // explode = split (ex. "14 7" to "14" and "7" splitted by " " (whitespace) delimiter)
    	irow = atoi(rcstore.at(0).c_str());
    	icolumn = atoi(rcstore.at(1).c_str());
    
    	char **spiral;
    	spiral = new char*[irow];
    	for(int k = 0; k < irow; k++)
    		*(spiral+k) = new char[icolumn];
    
    	for(int i = 0; i < irow; ++i)
    	{
    	   for(int j = 0; j < icolumn;++j)
    	   {
    		   spiral[i][j] = NULL;
    	   }
    	}
    
    	int x_pos = 0, y_pos = 0;
    	bool is_done = false;
    	int direction = 1;
    	int current_char = 0;
    
    	while(!is_done && str_input[current_char] != NULL)
    	{
    		while(x_pos >= 0 && x_pos < icolumn)
    		{
    			if(spiral[y_pos][x_pos] == NULL)
    			{
    				spiral[y_pos][x_pos] = str_input[current_char];
    				current_char++;
    				x_pos += direction;
    			}
    		   else break;
    		}
    		x_pos -= direction;
    		y_pos += direction;
            
    		while(y_pos >= 0 && y_pos < irow)
    		{
    		   if(spiral[y_pos][x_pos] == NULL)
    		   {
    			   spiral[y_pos][x_pos] = str_input[current_char];
    			   current_char++;
    			   y_pos += direction;
    		   }
    		   else
    		   {
    			   break;
    		   }
    		}
    		y_pos -= direction;
    		x_pos -= direction;
    
    		x_pos_p = x_pos+1;
    		x_pos_m = x_pos-1;
    		y_pos_p = y_pos+1;
    		y_pos_m = y_pos-1;
    
    		if((spiral[y_pos_p][x_pos] && spiral[y_pos_m][x_pos]) && (spiral[y_pos][x_pos_p] && spiral[y_pos][x_pos_m])) // HERE IS THE ERROR
    		{
    			is_done = true;
    		}
    
    		direction = -direction;
    	}
    
    	for(int i = 0; i < irow; ++i)
    	{
    	   for(int j = 0; j < icolumn; ++j)
    	   {
    		   str_reformed += spiral[i][j];
    	   }
    	}
    
    	cout << str_reformed << endl;
    
    	delete[] *spiral;
    	delete[] spiral;
    
    	system("PAUSE");
    everything is working fine until:
    Code:
    if((spiral[y_pos_p][x_pos] && spiral[y_pos_m][x_pos]) && (spiral[y_pos][x_pos_p] && spiral[y_pos][x_pos_m]))
    and the debugger gives me this message:
    CXX0030: Error: expression cannot be evaluated


    What's wrong with my code?
    (FYI: I'm using VC++ 2008 EE)

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: CXX0030: Error: expression cannot be evaluated

    Quote Originally Posted by djzmo View Post
    What's wrong with my code?
    (FYI: I'm using VC++ 2008 EE)
    So you're going to change your code so that the debugger is happy? Does your code work? If so, then this is a problem or issue with the debugger, not your code.

    First, are you debugging an optimized version of your application?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2009
    Posts
    2

    Re: CXX0030: Error: expression cannot be evaluated

    Yes the code work.
    I can compile and run it in the debug mode.
    but once I entered an input for the str_input_str and rowscols variable, an error occured..

    Unhandled exception at 0x00412206 in ...: 0xC0000005: Access violation reading location 0xfdfdfdff.
    and the debugger gives me an error msg..
    CXX0030: Error: expression cannot be evaluated
    and the error pointed to

    Code:
    if((spiral[y_pos_p][x_pos] && spiral[y_pos_m][x_pos]) && (spiral[y_pos][x_pos_p] && spiral[y_pos][x_pos_m]))
    ...
    and I tried to compile the code using Dev-C++..
    but now I got an unlimited while loop......

  4. #4
    Join Date
    Apr 1999
    Posts
    27,418

    Re: CXX0030: Error: expression cannot be evaluated

    Quote Originally Posted by djzmo View Post
    Yes the code work.
    If it works, then why do you then say this:
    but once I entered an input for the str_input_str and rowscols variable, an error occured..
    The code is *not* working. It is giving you an exception, more than likely you are accessing an array beyond the boundaries of the array.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,418

    Re: CXX0030: Error: expression cannot be evaluated

    Also, the debugger is going to say that if the data you're trying to view is truly invalid.
    Code:
    if((spiral[y_pos_p][x_pos] && spiral[y_pos_m][x_pos]) && (spiral[y_pos][x_pos_p] && spiral[y_pos][x_pos_m])) // HERE IS THE ERROR
    I notice you do no checks at all to see if any of those indices are valid. You should see what the values of y_pos_p, x_pos, etc. are when it gives you that error. I bet one or more are out of bounds.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    May 2007
    Posts
    798

    Re: CXX0030: Error: expression cannot be evaluated

    Quote Originally Posted by djzmo View Post
    Yes the code work.

    I can compile and run it in the debug mode.
    but once I entered an input for the str_input_str and rowscols variable, an error occured..

    and the debugger gives me an error msg..
    CXX0030: Error: expression cannot be evaluated
    and the error pointed to

    Code:
    if((spiral[y_pos_p][x_pos] && spiral[y_pos_m][x_pos]) && (spiral[y_pos][x_pos_p] && spiral[y_pos][x_pos_m]))
    ...
    and I tried to compile the code using Dev-C++..
    but now I got an unlimited while loop......
    Well, how can you say it works when clearly it crashes.

    Unhandled exception at 0x00412206 in ...: 0xC0000005: Access violation reading location 0xfdfdfdff.
    This means that you wrote past allocated memory. Here is more info showing what exactly those hex number means.

    Why all those pointers and manual memory management, how about use stl?

    I also don't understand how switching compilers going to magically fix your code, there is no willy-nilly in programming.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,418

    Re: CXX0030: Error: expression cannot be evaluated

    This is also wrong:
    Code:
    delete[] *spiral;
    delete[] spiral;
    Your program has a major memory leak. Where is the loop to delete the memory that you allocated (in a loop)?

    And as STLDude mentioned, why are you doing manual memory management. You included <vector>, why don't you use it? You used it once, but you didn't use it for spiral.
    Code:
    char **spiral;
    	spiral = new char*[irow];
    	for(int k = 0; k < irow; k++)
    		*(spiral+k) = new char[icolumn];
    Replaced with this:
    Code:
    typedef std::vector<char> CharArray;
    typedef std::vector<CharArray> CharArray2D;
    
    CharArray2D spiral(irow, CharArray(icolumn));
    Then there is no need for new or delete.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 6th, 2009 at 09:34 PM.

  8. #8
    Join Date
    Feb 2012
    Posts
    2

    Re: CXX0030: Error: expression cannot be evaluated

    I got exactly the same error for my variable "p_age" when I ran this code:

    Code:
    void save_drivers (Driver driver[],short total)
    {
         ofstream myfile ("example.txt");
         char age;
         char * p_age;
         for (short n=0; n < total; n++)
         {
               myfile << driver[n].Name << endl;
    	   age = driver[n].Age;
    	   p_age = reinterpret_cast<char*>(age);
               myfile.write(p_age,1);
    	   myfile << endl;
         }
         myfile.close();
    }
    So I changed it to this:

    Code:
    void save_drivers (Driver driver[],short total)
    {
         ofstream myfile ("example.txt");
         char age;
    
         for (short n=0; n < total; n++)
         {
               myfile << driver[n].Name << endl;
    	   age = driver[n].Age;
    
    	   myfile.put(age);
    	   myfile << endl;
         }
         myfile.close();
    }
    And it worked.

    I don't understand why it had an issue with this line:
    Code:
    myfile.write(p_age,1);

  9. #9
    Join Date
    Apr 1999
    Posts
    27,418

    Re: CXX0030: Error: expression cannot be evaluated

    Quote Originally Posted by Jimnik View Post
    I got exactly the same error for my variable "p_age" when I ran this code:
    Let's see:
    Code:
         char * p_age;
    p_age is uninitialized, so it points to who-knows-where. Then you do this:
    Code:
               myfile << driver[n].Name << endl;
    	   age = driver[n].Age;
    	   p_age = reinterpret_cast<char*>(age);
    So let me ask you -- why are you taking the value of age and changing it to a pointer? What is the reason for this?

    For example, if age is 10, the p_age is now equal to 10. Is address 10 a valid address? No.

    I know what you're problem is. You are calling the write() function, and the write() function takes a pointer as the first argument. So you thought that you need to declare a pointer, and do whatever you need to do to pass it to write(), correct?

    This is a mistake that a lot of beginner programmers make. They see that a function requires a pointer, so they think that they need to declare a pointer. Wrong. The function is looking for an address -- the address must be a valid address.

    Here is your function using write() in a correct manner:
    Code:
    void save_drivers (Driver driver[],short total)
    {
         ofstream myfile ("example.txt");
         char age;
         for (short n=0; n < total; n++)
         {
               myfile << driver[n].Name << endl;
    	   age = driver[n].Age;
               myfile.write(&age,1);
    	   myfile << endl;
         }
         myfile.close();
    }
    I am now passing the address of the age variable. See the difference?

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: CXX0030: Error: expression cannot be evaluated

    When you encounter values like 0xfdfdfdff this link provide a clue to what's wrong.
    http://www.nobugs.org/developer/win3..._crt_heap.html

    (This is one of many available so a search might reveal one with even better explanations)
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  11. #11
    Join Date
    Feb 2012
    Posts
    2

    Re: CXX0030: Error: expression cannot be evaluated

    Quote Originally Posted by Paul McKenzie View Post
    Let's see:
    Code:
         char * p_age;
    p_age is uninitialized, so it points to who-knows-where. Then you do this:
    Code:
               myfile << driver[n].Name << endl;
    	   age = driver[n].Age;
    	   p_age = reinterpret_cast<char*>(age);
    So let me ask you -- why are you taking the value of age and changing it to a pointer? What is the reason for this?

    For example, if age is 10, the p_age is now equal to 10. Is address 10 a valid address? No.

    I know what you're problem is. You are calling the write() function, and the write() function takes a pointer as the first argument. So you thought that you need to declare a pointer, and do whatever you need to do to pass it to write(), correct?

    This is a mistake that a lot of beginner programmers make. They see that a function requires a pointer, so they think that they need to declare a pointer. Wrong. The function is looking for an address -- the address must be a valid address.

    Here is your function using write() in a correct manner:
    Code:
    void save_drivers (Driver driver[],short total)
    {
         ofstream myfile ("example.txt");
         char age;
         for (short n=0; n < total; n++)
         {
               myfile << driver[n].Name << endl;
    	   age = driver[n].Age;
               myfile.write(&age,1);
    	   myfile << endl;
         }
         myfile.close();
    }
    I am now passing the address of the age variable. See the difference?

    Regards,

    Paul McKenzie
    Ah, that makes so much more sense now. Thanks.

    Indeed, I misinterpreted the first parameter of the write function. In fact I misunderstood this whole pointer concept. But I think I've got it now.

    Really appreciate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center