CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2011
    Posts
    4

    Assertion Error: Invalid Null Pointer

    Hello everyone, I am learning c++ migrating from Visual basic. I have created a sample program in which a random number is generated and a user must guess what the number is. Unfortunately I am being bogged down by an assertion error, and I cannot figure out what is wrong with my code. Here is my code, and the error occurs at line 33. Any help would be much appreciated!
    Code:
    #include "stdlib.h"
    #include "stdio.h"
    #include "string.h"
    #include <sstream>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
    char thing = NULL;
    
    vector<int> randNum(4);
    vector<int> guesses(4);
    
    cout << "Enter any number \n";
    
    int awesome;
    cin >>  awesome;
    srand(awesome);
    
    cout << "Guess the four correct digits \n";
    
    int bulls = 1;
    int cows = 1;
    string input = 0;
    
    while (bulls < guesses.size()) {
    
    	bulls = 0;
    	cows = 0;
    	
    	cout << "Enter Four Digits";
    		cin >> input;
    
    		
    		
    		
    
    for(int i = 0; i< randNum.size(); i++) {
    		randNum[i] = rand() &#37; 10;
    		guesses[i] = atoi(input.substr(0, 1).c_str());
    		//5 = 4359
    
    	}
    
    
    	
    for (int ii = 0; ii < randNum.size(); ii++) {
    	for(int j = 0; j < guesses.size(); j++) {
    		if(guesses[j] == randNum[ii] && j == ii){
    			bulls += 1;	
    			}
    		else if(guesses[j] == randNum[ii]){
    		 cows += 1;
    			}
    		} //end for (int j)
    	} //end for(int ii)
    if (bulls < guesses.size()) {
    	cout << " You have guessed" << bulls << " bulls and " << cows << " cows";
    	}
    else
    	{
    		"Congratulations! The number you have guessed is correct!";
    	} //end  if(bulls < 4)
    	} // end while bulls < 4
    
    
    
    	cin >> thing;
        //keeps the console window open
    
    return 0;
    
    	}
    Last edited by cilu; August 16th, 2011 at 12:34 AM.

  2. #2
    Join Date
    Aug 2011
    Posts
    4

    Re: Assertion Error: Invalid Null Pointer

    I'm sorry I forgot to state what line 33 is. It is this line of code:
    while (bulls < guesses.size()) {

  3. #3
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Assertion Error: Invalid Null Pointer

    looks like you need to allocate memory for the vector array ...
    try something like this and see

    Code:
     
    
    vector<int> guesses(4) = new vector<int>(4);

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Assertion Error: Invalid Null Pointer

    @vcdebugger, why are you suggesting something like that? That is not even valid C++ code. You allocate memory on the heap, but don't use a pointer. He's code was already correct. Mind what you suggest.

    @Dan3444, did you try debugging step by step? Frankly, I can't figure what could be wrong with that while condition to assert. Maybe you can show us the assertion text.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Aug 2011
    Posts
    4

    Re: Assertion Error: Invalid Null Pointer

    @@Cilu: I did set breakpoints in the code. The point where it errors at is while (bulls < guesses.size()) {

    I can't figure it out for the life of me either... The error text reads: debug assertion Failed!
    Program: c:\users\dan\desktop\c++\dan5\debug\dan5.exe file f:\dd\vctools\crt_bld\self_x86\crt\src\xstring line: 1-94

    Expression: invalid null pointer. For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Assertion Error: Invalid Null Pointer

    It came from this line.

    string input = 0;

    I'm not really sure why you're trying to assign zero to a string. Leave off the assignment and you should be okay.

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

    Re: Assertion Error: Invalid Null Pointer

    Quote Originally Posted by Dan3444 View Post
    @@Cilu: I did set breakpoints in the code. The point where it errors at is while (bulls < guesses.size()) {
    GCDEF identified the probable cause:
    Code:
    string input = 0;
    This line of code does a lot of damage. The reason why this is bad is because there is no constructor of std::string that takes an integer. So what happens is that the compiler attempts to match up the integer with a compatible constructor for std::string, and unlucky for you, the constructor that it does match up best is this one:
    Code:
    string(const char*);
    So how is this bad? The 0 is then converted to a 0-pointer to char, which is a NULL pointer. Then the std::string class is now attempting to read starting from address 0, causing all sorts of bad things to happen.

    It's one of those things you must be aware of with std::string -- you can construct one with an integer. But constructing a std::string with an integer is not doing what you think it's doing, instead all havoc is occurring.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Aug 2011
    Posts
    4

    Re: Assertion Error: Invalid Null Pointer

    Thanks everyone! I can't believe I made such a n00b mistake. Unfortunately Visual studio is a lot different with c++ than with Visual Basic; The only thing that seems to work is text highlighting; I suppose it's time for a new IDE.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Assertion Error: Invalid Null Pointer

    Quote Originally Posted by Dan3444 View Post
    Thanks everyone! I can't believe I made such a n00b mistake. Unfortunately Visual studio is a lot different with c++ than with Visual Basic; The only thing that seems to work is text highlighting; I suppose it's time for a new IDE.
    Not really sure what you mean by that, but the debugger works, which is how I found your problem.

  10. #10
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Assertion Error: Invalid Null Pointer

    Quote Originally Posted by cilu View Post
    @vcdebugger, why are you suggesting something like that? That is not even valid C++ code. You allocate memory on the heap, but don't use a pointer. He's code was already correct. Mind what you suggest.

    .
    sorry, Cilu. I suggested that by seeing the line where it crashed.. thinking memory would not have been allocated.

    Its long time i coded in C++ and still want to be in touch with it and learn something. Thats why I am here.with this.. I learnt ( or got aware ? :P) something. thanks.

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Assertion Error: Invalid Null Pointer

    Quote Originally Posted by Dan3444 View Post
    Unfortunately Visual studio is a lot different with c++ than with Visual Basic; The only thing that seems to work is text highlighting; I suppose it's time for a new IDE.
    The thing is that in C/C++ you're allowed to do all kinds of stupid things without getting your fingers slapped by neither the compiler nor the IDE. I.e in Visual Basic you always have a baby sitter around, in C/C++ you're home alone...

    MSVC has one of the best debuggers around so don't throw it out. You will (and should) be using the debugger a lot while learning.
    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
    https://visualstudio.microsoft.com/vs

  12. #12
    Join Date
    Aug 2011
    Posts
    28

    Re: Assertion Error: Invalid Null Pointer

    Quote Originally Posted by S_M_A View Post
    The thing is that in C/C++ you're allowed to do all kinds of stupid things without getting your fingers slapped by neither the compiler nor the IDE. I.e in Visual Basic you always have a baby sitter around, in C/C++ you're home alone...
    : D kakakakaka

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured