problem using vector::resize()

P

Peter Mrosek

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.resize(n);
[...]

The problem ist, that I get segmentation faults in this line.
The faults vanish when I add the line
colsToGetDist.resize(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
 
P

Paul

Peter Mrosek said:
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.resize(n);
[...]

The problem ist, that I get segmentation faults in this line.
The faults vanish when I add the line
colsToGetDist.resize(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<RGBQUAD> 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
 
P

Paul

Paul said:
char x[100]
memset(x, 1000, 0);

and your program didn't crash after the call to memcpy(), you have a memory

That of course should state "any your program didn't crash after calling
*memset*"

Paul
 
P

Peter Mrosek

Hi Paul,

thanks for your opinion.
I already feared that resize() won't be the problem
I don't have any code that directly manipulates the memory. I only have
a lot of vectors and mathematical calculations

Your example works fine.
int main()
{
std::vector<RGBQUAD> QV;
QV.resize(1000);
}


Thats bad, because now I have no idea where to suppose the problem.

Thanks for your help

Peter
 
K

Keith H Duggar

Thats bad, because now I have no idea where to suppose the problem.

If you want to post a link to a more complete code listing maybe we can help out.
 

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

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top