Connecting Tech Pros Worldwide Forums | Help | Site Map

problem using vector::resize()

Peter Mrosek
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Paul
Guest
 
Posts: n/a
#2: Jul 22 '05

re: problem using vector::resize()



"Peter Mrosek" <mrosekp@fh-trier.de> wrote in message
news:bvim9j$h3p$05$1@news.t-online.com...[color=blue]
> 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[/color]

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


Paul
Guest
 
Posts: n/a
#3: Jul 22 '05

re: problem using vector::resize()


Paul wrote:
[color=blue]
>
> char x[100]
> memset(x, 1000, 0);
>
> and your program didn't crash after the call to memcpy(), you have a memory[/color]

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

Paul
Peter Mrosek
Guest
 
Posts: n/a
#4: Jul 22 '05

re: problem using vector::resize()


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.[color=blue]
> int main()
> {
> std::vector<RGBQUAD> QV;
> QV.resize(1000);
>}[/color]


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

Thanks for your help

Peter
Keith H Duggar
Guest
 
Posts: n/a
#5: Jul 22 '05

re: problem using vector::resize()


> 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.
Closed Thread