dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Mar 2018
    Posts
    36

    debug assertion error(there is a bug in my code)

    hello
    I prepared a following code in visual studio 2017 and when I want to run it in debug state then an error will occurred about debug assertion and I think there is a bug in my code(when I run it with release state I dont have this error and it will run successfully).
    also in this line: for (i = 1, x = 0; i < pow(2, r); i = pow(2, x)) I have a warning that "=" the conversion among double to int may loss a data!!
    Last edited by Kmilano; April 30th, 2018 at 07:20 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,144

    Re: debug assertion error(there is a bug in my code)

    Did you debug your code?
    Where exactly does the "assertion failed" happen?

    And FYI: Surviving the Release Version
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2018
    Posts
    36

    Re: debug assertion error(there is a bug in my code)

    on this line: Data[i] = rand() % 2; when I want to generate random numbers

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,217

    Re: debug assertion error(there is a bug in my code)

    Quote Originally Posted by Kmilano View Post
    on this line: Data[i] = rand() % 2; when I want to generate random numbers
    Correct. i becomes out of bounds. Data.size() is the number of elements in the array - not the index value of the last element. So a condition should be < Data.size() for element access. Note that you also have the same issue elsewhere.

    Why start at element 1 and not element 0? Elements are accessed from index 0 to index .size() - 1.

    PS Note that vector [] doesn't do bounds checking. What happens if [] accesses out of bounds is not defined. For vector bounds checking at run-time there is the .at().
    Last edited by 2kaud; April 27th, 2018 at 08:12 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  5. #5
    Join Date
    Mar 2018
    Posts
    36

    Re: debug assertion error(there is a bug in my code)

    when I initialize element from zero the output of my code (special desired calculation among bits) will be wrong
    how I can solve this problem in your opinion??

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,144

    Re: debug assertion error(there is a bug in my code)

    Your vector Data has the size() of 4. So there 4 elements with the indexes:
    Data [0]
    Data [1]
    Data [2]
    Data [3]
    But you are trying to access the elements

    Data [1]
    Data [2]
    Data [3]
    Data [4] // this is wrong! out of bounds.

    BTW, why do you begin array/vector indexing from 1 instead of 0?
    It is a serious mistake in C/C++ languages!
    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,217

    Re: debug assertion error(there is a bug in my code)

    how I can solve this problem in your opinion??
    Fix the code!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,217

    Re: debug assertion error(there is a bug in my code)

    Code:
    for (i = 1; i <= 4; i++)                                                               
    	{                                                                                       
    		Data.push_back(i);
    	}
    	for (i = 1; i <= Data.size(); i++)
    	{
    		Data[i] = rand() % 2;
    		std::cout << Data[i] % 2 << " ";
    	}
    Why all this?? Why not just

    Code:
    for (i = 0; i < 4; ++i)                                                               
        Data.push_back(rand() % 2);
    
    for (const auto& d : Data)
        std::cout << d << " ";
    or even

    Code:
    std::vector<int> Data(4);
    
    for (auto& d : Data) {
        d = rand() % 2;
        std::cout << d << " ";
    }
    Last edited by 2kaud; April 27th, 2018 at 08:55 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,217

    Re: debug assertion error(there is a bug in my code)

    Code:
    std::vector<int> H(n);
    	for (i = 1; i <= n; i++)
    	{
    		if (i == pow(2, p))
    		{
    			H[i] = 0;
    			p++;
    		}
    		else
    		{
    			H[i] = Data[j];
    			j++;
    		}
    	}
    try (not tested)
    Code:
    std::vector<int> H(n);
    for (i = 1, p =0, j = 0; i <= n; ++i)
        H[i - 1] = (i == (1 << p)) ? ++p, 0 : Data[j++];
    Last edited by 2kaud; April 27th, 2018 at 09:00 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  10. #10
    Join Date
    Mar 2018
    Posts
    36

    Re: debug assertion error(there is a bug in my code)

    about your reply in post #9
    a warning is for another loop(not the loop that you changed in #9)
    in this loop:
    for (i = 1, x = 0; i < pow(2, r); i = pow(2, x))

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,217

    Re: debug assertion error(there is a bug in my code)

    Why are you using pow with 2? Why not just use << (right shift - don't confuse with << when used as stream insertion!) to multiply by 2 instead?

    1 << 1 becomes 2 (pow(2, 1))
    1 << 2 becomes 4 (pow(2, 2))
    1 << 3 becomes 8 (pow 2, 3)) etc etc

    This is quicker than using pow() (although pow may be this as an optimisation?).

    So consider
    Code:
    for (i = 1, x = 0; i < (1 << r); i = (1 << x))
    Last edited by 2kaud; April 27th, 2018 at 10:21 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  12. #12
    Join Date
    Mar 2018
    Posts
    36

    Re: debug assertion error(there is a bug in my code)

    I applied all points you noticed in your poste, but I dont know why my code will return weird numbers(it must return binary numbers)
    my code in post #1 is working in release state in visual studio, but in debug it never works

    is it possible try it in visual studio and help me?

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,217

    Re: debug assertion error(there is a bug in my code)

    For the code in post #1, where is FinalH defined?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  14. #14
    Join Date
    Mar 2018
    Posts
    36

    Re: debug assertion error(there is a bug in my code)

    excuse me, I wrote a code wrongly in post #1

    here, in the following again I attached a correct code:
    Last edited by Kmilano; April 30th, 2018 at 07:21 AM.

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,217

    Re: debug assertion error(there is a bug in my code)

    The code below compiles OK in both debug and release builds and produces the same output from both builds with the same numbers for Data.

    Code:
    #include <ctime>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	srand(unsigned(time(0)));
    	vector<int> Data (4);
    
    	cout << "a generated numbers are: " << endl;
    
    	for (auto& d : Data)
    		cout << (d = rand() % 2) << " ";
    
    	int r = 0;
    
    	for (; (5 + r) > (1 << r); ++r);
    
    	cout << endl << "The no of r : " << r << endl;
    
    	const int n = r + 4;
    
    	std::vector<int> FinalH(n);
    
    	for (int i = 1, p = 0, j = 0; i <= n; ++i)
    		FinalH[i - 1] = (i == (1 << p)) ? ++p, 0 : Data[j++];
    
    	cout << endl << "The proper r " << endl;
    
    	for (int i = 1, x = 0; i < (1 << r); ++x, i = (1 << x))
    	{
    		for (int j = 1; j <= n; ++j)
    			if ((i & j) == i)
    				FinalH[i - 1] ^= FinalH[j - 1];
    
    		cout << endl << i << " " << FinalH[i - 1];
    	}
    
    	cout << endl <<"Total number is: " << n << endl << endl << "the whole codebit: " << endl;
    
    	for (const auto& h : FinalH)
    		cout << h << " ";
    
    	cout << endl;
    }
    If this doesn't display the required results, then at least this will debug!
    Last edited by 2kaud; April 29th, 2018 at 10:38 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)


×
By using this site, you agree to the Privacy Policy