The following program is giving error as follows. please get me the solution how to debug this error.
Error:
Debug Assertion Failed!

Program: ...
File: d:\program files\microsoft visual studio 8\vc\include\vector
Line: 1556

Expression: ("this->_Mycont != NULL && this->_Myptr != NULL", 0)

#include<iostream>
#include<vector>
#include<deque>
#include<list>
#include<algorithm>
#include<functional>
#include "print.hpp"
int main()
{
	vector<bool> vin1;
	vector<bool> vin2;
	vector<bool> vout;

	cout<<"Enter the value of vin1 in binary\n";
	for(int i=1;i<=8;++i)
	{
		cin>>elem;
		vin1.push_back(elem);
	}
	cout<<"\nEnter the value of vin2 in binary\n";
	for(int i=1;i<=8;++i)
	{
		cin>>elem;
		vin2.push_back(elem);
	}
                PRINT_ELEMENTS(vin1,"vin1:      ");	
	PRINT_ELEMENTS(vin2,"vin2:      ");					transform(vin1.begin(), vin1.end(),         //Error
				vin2.begin(), vout.begin(),
				logical_and<bool>());
	PRINT_ELEMENTS(vout,"vin1 & vin2: ");
cin.get()
}

Dani AI

Generated

The assertion comes from the MSVC vector debug checks and means an invalid output iterator was dereferenced. correctly pointed out the immediate problems and confirmed the runtime crash went away after applying the resize-based fix. The core issue is that std::transform writes through the output iterator (*result), so the output range must already provide storage (or be an inserting iterator) before calling the algorithm. Also ensure the second input range is at least as long as the first to avoid reading past the end.

Practical options that avoid the assertion (and undefined behavior):

  • Pre-allocate space for the output so vout.begin() is a valid writable iterator (allocate to the number of elements to be produced).
  • Use an inserting iterator to append results without manual sizing. Example using a back inserter:
std::transform(vin1.begin(), vin1.end(),
               vin2.begin(),
               std::back_inserter(vout),
               std::logical_and<bool>());

Troubleshooting/caution notes:

  • Declare the input variable used with cin (the original code left elem undeclared) and keep the missing ; after cin.get() fixed.
  • Verify vin2.size() >= vin1.size() before running the two-range form of transform.
  • std::vector<bool> is a specialized, space-efficient container that uses proxy references; for bitwise-style logic it usually works, but it can be surprising in advanced code. For predictable element semantics consider std::bitset<N> or a byte-based container (e.g., std::vector<uint8_t>).

These points explain the assertion and give two safe, easy ways to fix the crash while avoiding subtle UB.

Recommended Answers

All 2 Replies

The above code won't compile. What is 'elem' ? a bool?
And you missed a semicolon after cin.get()

For other people who want to help: Here's the code from the header (it was discussed in another thread)

#include <iostream>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

But to solve your problem:
You have to resize the output vector before you put values in it. So put this: vout.resize(vin1.size()); Right before the transform line.

The above code won't compile. What is 'elem' ? a bool?
And you missed a semicolon after cin.get()

For other people who want to help: Here's the code from the header (it was discussed in another thread)

#include <iostream>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

But to solve your problem:
You have to resize the output vector before you put values in it. So put this: vout.resize(vin1.size()); Right before the transform line.

thanks i have put vout.resize(vin1.size()); before transform line. now the code is working fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.