"Peter Mrosek" <mr*****@fh-trier.de> wrote in message
news:bv******** *****@news.t-online.com...
Hello,
I have the following declaration in an header file (abbreviated
version):
[...]
typedef struct {
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}RGBQUAD;
[...]
vector<RGBQUAD> colsToGetDist;
[...]
and this is a line from my main program:
[...]
colsToGetDist.r esize(n);
[...]
The problem ist, that I get segmentation faults in this line.
The faults vanish when I add the line
colsToGetDist.r esize(1);
to the constructor, but I don't understand why resize seems to fail
allocating memory.
Are there some circumstances in which resize does't work?
Maybe someone here can help.
Peter
The problem is more than likely caused by something else in your code, and
there is no problem with resize().
You probably corrupted memory, and the resize() call is only a symptom of
the problem, and not the cause of the problem. Try this:
#include <vector>
typedef struct {
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}RGBQUAD;
int main()
{
std::vector<RGB QUAD> QV;
QV.resize(1000) ;
}
Does running this code crash? I can bet that the code above doesn't crash,
so ask yourself why your code crashes.
Unless you have *concrete* evidence that the standard library that comes
with your compiler is buggy, you always first look to the parts of your code
that do any kind of memory management or have the potential to overwrite
memory. For example, if you did this:
char x[100]
memset(x, 1000, 0);
and your program didn't crash after the call to memcpy(), you have a memory
overwrite, and this will affect how other memory related functions will
operate, including vector::resize( )
So post (or look very carefully) at those lines of code where you posted
"[...]" -- the problem isn't resize().
Paul