Vector Class Discussion

 
thread gcc vector class+std::list crash - Marco Belli - 2013-10-22
reply gcc vector class+std::list crash - Agner - 2013-10-23
last replythread more on vectorclass and STL problems - renato talucci - 2014-10-21
last reply more on vectorclass and STL problems - Agner - 2014-10-21
 
gcc vector class+std::list crash
Author:  Date: 2013-10-22 09:42
Hi Agner,
I started using vectorClass for an hobby project and I found a little problem with GCC 4.8.1 on windows with optimization turned on.

I created a structure "state" that has inside some Vec4i variables and a list containing states

std::list<state> stateInfo;

when i add the first data to stateInfo I got a crash if I use gcc 4.8.1 with optimization enabled ( setting no optimization solve the problem)

state s;

s.vector=0; //<-- Vec4i

stateInfo.push_back(s); <-- CRASH

if I replace the Vec4i with an int the problem is gone.
have you some idea of how to solve the problem? have you ever had such a problem?

best regards Marco

   
gcc vector class+std::list crash
Author: Agner Date: 2013-10-23 00:09
My guess is that it is an alignment issue. It is possible that std::list allocates a memory block that is not aligned by 16 and push_back does something that assumes that the vector is aligned by 16. You may try to debug into it to see if this is the problem.

I would never use the standard template library (STL) on speed-critical applications (see my C++ manual). If you can set a reasonable maximum size for your list at compile time then just use a simple array. This is the fastest solution. If you need an array with dynamic size then you may use AlignedArray from www.agner.org/optimize/cppexamples.zip

   
more on vectorclass and STL problems
Author:  Date: 2014-10-21 06:03
Hi to all forum memberS!

I would like to add something to the question of Mr Belli, about problems with vectorclass and STL container.

This is my example :

#include "vectorclass.h"
#include <iostream>
#include <vector>

class myClass
{
private:
double g[4];
Vec4d av;
public:
myClass();
void prt();
};

myClass::myClass()
{
double f[4]={1,2,3,4};
av.load(f);
std::cout<<av.extract(0)<<std::endl; //THIS CALL IN THE CONSTRUCTOR DOESN'T CAUSE ANY PROBLEM
}

void myClass::prt()
{
std::cout<<av.extract(0)<<std::endl; //THE SAME CALL HERE CAUSES THE PROGRAM TO CRASH
}


int main()
{
std::vector<myClass> aset;
aset.push_back(myClass());
aset.push_back(myClass());
aset[0].prt(); //CALL prt() AND... CRASH!
return(0);
}

this code, compiled with GCC on windows crashes as member function prt() is called. Note that :

1. the same call inside the constructor does not cause the crash..why??
2. if the member double g[4] is removed from myClass, the problem disappears;
3. if the std::vector has just one entry the problem disappears,
4. the same code compiled with GCC in Linux works without crashing

... As I'm not a professional programmer I cannot understand what is happening, I hope someone of you can have some idea about it.

Finally, let me say thank you Mr Agner for such a good piece of code!

Renato

   
more on vectorclass and STL problems
Author: Agner Date: 2014-10-21 13:52
This is the same error. The myClass must be aligned to an address divisible by 32. The compiler makes the correct alignment when creating an instance of myClass. But the STL vector knows nothing about alignment, so it can place its element at any address. This causes the crash in the extract function when the compiler assumes that all instances of Vec4d are properly aligned.

So don't ever put vector classes into STL or other containers.