locked
Debug assertion failed! RRS feed

  • Question

  • Hello!

    I'm getting a runtime error while working on Visual C++ project.

    IDE doesn't show any error, but program get stuck while it's running.

    When I used VS breakpoints I noticed that the program get stuck here:

    int String:: has(char a)
    {
    	str=new char[sizeof(str)];
    	int n=0;
    	for (int i=0;i<sizeof(str);i++)
    	{
    		if (isalpha(str[i])==a)//here !!
    		{
    			n++;
    		}
    	}
    	return n;
    }

    Where "str" is type char * .

    Runtime error message:

    Debug Assertion Failed!

    Program:...\...\isctype.c
    Line:56

    Expression: c>=-1 && c<=255


    Admir


    • Edited by admiri92 Friday, December 13, 2013 6:23 PM
    Friday, December 13, 2013 6:07 PM

Answers

  • There are at least 2 problems with your code:

    • You're using sizeof to find the length of the string. That won't work, sizeof gives you the size of the pointer. Use strlen to find the length of the string
    • You're allocating a char array and you're reading from it without having writing anything to it first. Basically you'll reading garbage from memory

    And your use of isalpha is rather curious. isalpha returns 0 or non-zero, it's not specified what non-zero value is returned. Comparing the result of isalpha to a value like you're doing doesn't make a lot of sense.

    • Marked as answer by admiri92 Friday, December 13, 2013 11:48 PM
    Friday, December 13, 2013 6:34 PM