_HAS_ITERATOR_DEBUGGING and std::vector

N

ngaloppo

Hi,

I sometimes convert std::vector::iterators to pointers by the (afaik)
legal construct &v[0], e.g. in the construct below. In debug build, the
program terminates, because of what seems to be part of the new checked
iterators in VC8. OTOH, I thought that the code is perfectly legal. Is
it safe to temporarily disable _HAS_ITERATOR_DEBUGGING here? I feel
bad about just shutting up the compiler.

Thanks!

bool QuadsFileIO::Read(const std::string& fn, std::vector<float>&
verts, std::vector<float>& texcoords) {
ifstream ifile(fn.c_str(), ios::binary | ios::in);
if (!ifile.is_open())
{
std::cout << "Warning: Couldn't open file for reading " << fn <<
std::endl;
return false;
}

typedef std::vector<float>::size_type size_type;
size_type size;
ifile.read((char*)&size, sizeof(size_type));

verts.resize(size);
texcoords.resize(size);

ifile.read((char*)&verts[0], sizeof(float) * size);
ifile.read((char*)&texcoords[0], sizeof(float) * size);

ifile.close();

return true;
}
 
H

Holger Grund

I sometimes convert std::vector::iterators to pointers by the (afaik)
legal construct &v[0], e.g. in the construct below. In debug build, the
program terminates, because of what seems to be part of the new checked
iterators in VC8. OTOH, I thought that the code is perfectly legal. Is
it safe to temporarily disable _HAS_ITERATOR_DEBUGGING here? I feel
bad about just shutting up the compiler.
&v[0] is perfectly valid so long as v has at least one element.
You should check that size != 0.

Otherwise, I fail to see why iterator debugging would cause
problems here. Can you get us the exact diagnostic and the call
stack?

-hg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top