473,722 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem using vector::resize( )

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
Jul 22 '05 #1
4 3404

"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
Jul 22 '05 #2
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
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<RGB QUAD> 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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1302
by: solosnake | last post by:
Hello, and merry Christmas! When trying to compile some code that had specific alignment requirements, I found that the Visual Studio .NET compiler gave me compile time errors. It forbids alignment specification of stack variables. Upon examing the source of the error, it was due to the signatures of the std::vector::resize being as follows: template<class _Ty, class _Ax = allocator<_Ty> >
9
3013
by: Alex Vinokur | last post by:
--------- #include <vector> using namespace std; struct Foo { Foo (int) {} }; int main () { vector<Foo> v1;
10
5163
by: vsgdp | last post by:
On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller than the current size, elements at the end of the container will be destroyed." What does "destroyed" mean? It seems to me that if n is smaller than the current size, simple adjust the internal size counter to n and be done.
8
4909
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike std::vector::erase, it calls T::operator= only three times no matter
3
2474
by: Jim Langston | last post by:
I really am not sure if this question belongs in this newsgroup, but not sure where else to ask it. There is someone working on a game that I tested, and it was taking >30 seconds to load. He stated that everyone else was taking 2 or 3 seconds. Then he found one other person taking >30 seconds, and it turns out the common denominator was both of us have Intel chips (Celeron) where the other people have AMD. I had him send me his code...
5
32611
by: atreya | last post by:
Hi, I'm trying to figure out if there are any differences between the implementation of resize(0) and clear() that a programmer using std::vector should be aware of! Could there be any advantage/disadvantage of using one over the other? I checked the VS7.1 implementation, in which I couldn't find any difference. Both of them are effectively calling erase(begin(), end()).
6
13915
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an instance for the copy constructor). For example: C++ vector<classvClass;
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.