By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

problem using vector::resize()

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"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.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
Jul 22 '05 #2

P: n/a
Paul wrote:

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
Jul 22 '05 #3

P: n/a
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
Jul 22 '05 #4

P: n/a
> 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.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.