473,725 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with adding a data into a vector

Hi all;

I have a problem when inserting an element to a vector. All I want is
reading some data from a file and putting them into a vector. But the
program is crashing after pushing a data which has string value. I
really do not understand why push_back() function is trying to remove
previously inserted data.

Thanks for any help

yatko
// program

#include <iostream>
#include <fstream>

#include <vector>
#include <map>
#include <cstdlib>
#include <string.h>
class VLProperty
{

public:
enum Aircraft{
BO105,
F16
};

enum Type{
INT,
BOOL,
LONG,
FLOAT,
DOUBLE,
STRING,
UNSPECIFIED
};
};
struct VLPropertyData {
unsigned id;

// While the type isn't transmitted, it is needed for the destructor
VLProperty::Typ e type;
union{
int int_value;
float float_value;
char* string_value;
};

//std::string string_value;
VLPropertyData( )
{
std::cout << "new data\n";
}

~VLPropertyData () {
if ((type == VLProperty::STR ING) || (type ==
VLProperty::UNS PECIFIED))
{
std::cout << string_value << " is deleted" << std::endl;
delete [] string_value;

}
}
};

class VLAIMultiplayer ;

class VLAIManager
{
public:

static std::string sAircraftIdList[];
static std::map<std::s tring,std::stri ngsAircraftMode lNameMap;

/// maps from the aircraft string to the VLPropertyData vector

typedef std::map< std::string, std::vector <VLPropertyData >* >
ModelPropertyDa taMap;
//static ModelPropertyDa taMap sModelPropertyD ataMap;

static ModelPropertyDa taMap sAircraftProper tyDataMap;

VLAIManager();
~VLAIManager();

VLAIMultiplayer * addMultiplayer( const std::string& callsign, const
std::string& aircraft);
std::vector <VLPropertyData >
addProperties(c onst std::string& aircraft);

private:

/// maps from the callsign string to the VLAIMultiplayer
typedef std::map<std::s tring, VLAIMultiplayer * MultiPlayerMap;
MultiPlayerMap mMultiPlayerMap ;

};
struct VLExternalMotio nInfo;

class VLMultiplayMgr
{
public:

struct PropertyDesc{
PropertyDesc()
{};

PropertyDesc(co nst char* name, VLProperty::Typ e type)
{
this->name = name;
this->type = type;
};

~PropertyDesc()
{};

const char* name;
VLProperty::Typ e type;
};

typedef std::map<unsign ed int, PropertyDescIdP ropertyDescMap;
static IdPropertyDescM ap sIdPropertyDesc Map;

static void Init();

private:
/// maps from the callsign string to the FGAIMultiplayer
typedef std::map<std::s tring, VLAIMultiplayer * MultiPlayerMap;
MultiPlayerMap mMultiPlayerMap ;

};
std::string VLAIManager::sA ircraftIdList [] = {
"",
"t38",
"bo105",
"F16",
"b1900d"
};
VLMultiplayMgr: :IdPropertyDesc Map VLMultiplayMgr: :sIdPropertyDes cMap =
VLMultiplayMgr: :IdPropertyDesc Map();

void
VLMultiplayMgr: :Init()
{
sIdPropertyDesc Map[100] = VLMultiplayMgr: :PropertyDesc(" surface-
positions/left-aileron-pos-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[101] = VLMultiplayMgr: :PropertyDesc(" surface-
positions/right-aileron-pos-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[102] = VLMultiplayMgr: :PropertyDesc(" surface-
positions/elevator-pos-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[103] = VLMultiplayMgr: :PropertyDesc(" surface-
positions/rudder-pos-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[104] = VLMultiplayMgr: :PropertyDesc(" surface-
positions/flap-pos-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[105] = VLMultiplayMgr: :PropertyDesc(" surface-
positions/speedbrake-pos-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[106] = VLMultiplayMgr: :PropertyDesc(" gear/tailhook/
position-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[107] = VLMultiplayMgr: :PropertyDesc(" gear/
launchbar/position-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[108] = VLMultiplayMgr: :PropertyDesc(" gear/
launchbar/state", VLProperty::STR ING);
sIdPropertyDesc Map[109] = VLMultiplayMgr: :PropertyDesc(" gear/
launchbar/holdback-position-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[110] = VLMultiplayMgr: :PropertyDesc(" canopy/
position-norm", VLProperty::FLO AT);

sIdPropertyDesc Map[200] = VLMultiplayMgr: :PropertyDesc(" gear/gear[0]/
compression-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[201] = VLMultiplayMgr: :PropertyDesc(" gear/gear[0]/
position-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[210] = VLMultiplayMgr: :PropertyDesc(" gear/gear[1]/
compression-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[211] = VLMultiplayMgr: :PropertyDesc(" gear/gear[1]/
position-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[220] = VLMultiplayMgr: :PropertyDesc(" gear/gear[2]/
compression-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[221] = VLMultiplayMgr: :PropertyDesc(" gear/gear[2]/
position-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[230] = VLMultiplayMgr: :PropertyDesc(" gear/gear[3]/
compression-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[231] = VLMultiplayMgr: :PropertyDesc(" gear/gear[3]/
position-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[240] = VLMultiplayMgr: :PropertyDesc(" gear/gear[4]/
compression-norm", VLProperty::FLO AT);
sIdPropertyDesc Map[241] = VLMultiplayMgr: :PropertyDesc(" gear/gear[4]/
position-norm", VLProperty::FLO AT);

sIdPropertyDesc Map[300] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[0]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[301] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[0]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[302] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[0]/rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[310] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[1]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[311] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[1]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[312] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[1]/rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[320] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[2]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[321] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[2]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[322] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[2]/rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[330] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[3]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[331] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[3]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[332] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[3]/rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[340] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[4]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[341] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[4]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[342] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[4]/rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[350] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[5]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[351] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[5]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[352] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[5]/rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[360] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[6]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[361] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[6]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[362] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[6]/rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[370] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[7]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[371] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[7]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[372] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[7]/rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[380] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[8]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[381] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[8]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[382] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[8]/rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[390] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[9]/n1", VLProperty::FLO AT);
sIdPropertyDesc Map[391] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[9]/n2", VLProperty::FLO AT);
sIdPropertyDesc Map[392] = VLMultiplayMgr: :PropertyDesc(" engines/
engine[9]/rpm", VLProperty::FLO AT);

sIdPropertyDesc Map[800] = VLMultiplayMgr: :PropertyDesc(" rotors/main/
rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[801] = VLMultiplayMgr: :PropertyDesc(" rotors/tail/
rpm", VLProperty::FLO AT);
sIdPropertyDesc Map[810] = VLMultiplayMgr: :PropertyDesc(" rotors/main/
blade[0]/position-deg", VLProperty::FLO AT);
sIdPropertyDesc Map[811] = VLMultiplayMgr: :PropertyDesc(" rotors/main/
blade[1]/position-deg", VLProperty::FLO AT);
sIdPropertyDesc Map[812] = VLMultiplayMgr: :PropertyDesc(" rotors/main/
blade[2]/position-deg", VLProperty::FLO AT);
sIdPropertyDesc Map[813] = VLMultiplayMgr: :PropertyDesc(" rotors/main/
blade[3]/position-deg", VLProperty::FLO AT);
sIdPropertyDesc Map[820] = VLMultiplayMgr: :PropertyDesc(" rotors/main/
blade[0]/flap-deg", VLProperty::FLO AT);
sIdPropertyDesc Map[821] = VLMultiplayMgr: :PropertyDesc(" rotors/main/
blade[1]/flap-deg", VLProperty::FLO AT);
sIdPropertyDesc Map[822] = VLMultiplayMgr: :PropertyDesc(" rotors/main/
blade[2]/flap-deg", VLProperty::FLO AT);
sIdPropertyDesc Map[823] = VLMultiplayMgr: :PropertyDesc(" rotors/main/
blade[3]/flap-deg", VLProperty::FLO AT);
sIdPropertyDesc Map[830] = VLMultiplayMgr: :PropertyDesc(" rotors/tail/
blade[0]/position-deg", VLProperty::FLO AT);
sIdPropertyDesc Map[831] = VLMultiplayMgr: :PropertyDesc(" rotors/tail/
blade[1]/position-deg", VLProperty::FLO AT);

sIdPropertyDesc Map[900] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/length", VLProperty::FLO AT);
sIdPropertyDesc Map[901] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/elastic-constant", VLProperty::FLO AT);
sIdPropertyDesc Map[902] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/weight-per-m-kg-m", VLProperty::FLO AT);
sIdPropertyDesc Map[903] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/dist", VLProperty::FLO AT);
sIdPropertyDesc Map[904] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/connected-to-property-node", VLProperty::BOO L);
sIdPropertyDesc Map[905] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/connected-to-ai-or-mp-callsign", VLProperty::STR ING);
sIdPropertyDesc Map[906] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/brake-force", VLProperty::FLO AT);
sIdPropertyDesc Map[907] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/end-force-x", VLProperty::FLO AT);
sIdPropertyDesc Map[908] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/end-force-y", VLProperty::FLO AT);
sIdPropertyDesc Map[909] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/tow/end-force-z", VLProperty::FLO AT);
sIdPropertyDesc Map[930] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/is-slave", VLProperty::BOO L);
sIdPropertyDesc Map[931] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/speed-in-tow-direction", VLProperty::FLO AT);
sIdPropertyDesc Map[932] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/open", VLProperty::BOO L);
sIdPropertyDesc Map[933] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/local-pos-x", VLProperty::FLO AT);
sIdPropertyDesc Map[934] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/local-pos-y", VLProperty::FLO AT);
sIdPropertyDesc Map[935] = VLMultiplayMgr: :PropertyDesc(" sim/hitches/
aerotow/local-pos-z", VLProperty::FLO AT);

sIdPropertyDesc Map[1001] = VLMultiplayMgr: :PropertyDesc(" controls/
flight/slats", VLProperty::FLO AT);
sIdPropertyDesc Map[1002] = VLMultiplayMgr: :PropertyDesc(" controls/
flight/speedbrake", VLProperty::FLO AT);
sIdPropertyDesc Map[1003] = VLMultiplayMgr: :PropertyDesc(" controls/
flight/spoilers", VLProperty::FLO AT);
sIdPropertyDesc Map[1004] = VLMultiplayMgr: :PropertyDesc(" controls/
gear/gear-down", VLProperty::FLO AT);
sIdPropertyDesc Map[1005] = VLMultiplayMgr: :PropertyDesc(" controls/
lighting/nav-lights", VLProperty::FLO AT);
sIdPropertyDesc Map[1006] = VLMultiplayMgr: :PropertyDesc(" controls/
armament/station[0]/jettison-all", VLProperty::BOO L);

sIdPropertyDesc Map[1100] = VLMultiplayMgr: :PropertyDesc(" sim/model/
variant", VLProperty::INT );

sIdPropertyDesc Map[10001] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/transmission-freq-hz", VLProperty::STR ING);
sIdPropertyDesc Map[10002] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/chat", VLProperty::STR ING);

sIdPropertyDesc Map[10100] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[0]", VLProperty::STR ING);
sIdPropertyDesc Map[10101] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[1]", VLProperty::STR ING);
sIdPropertyDesc Map[10102] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[2]", VLProperty::STR ING);
sIdPropertyDesc Map[10103] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[3]", VLProperty::STR ING);
sIdPropertyDesc Map[10104] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[4]", VLProperty::STR ING);
sIdPropertyDesc Map[10105] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[5]", VLProperty::STR ING);
sIdPropertyDesc Map[10106] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[6]", VLProperty::STR ING);
sIdPropertyDesc Map[10107] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[7]", VLProperty::STR ING);
sIdPropertyDesc Map[10108] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[8]", VLProperty::STR ING);
sIdPropertyDesc Map[10109] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/string[9]", VLProperty::STR ING);

sIdPropertyDesc Map[10200] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[0]", VLProperty::FLO AT);
sIdPropertyDesc Map[10201] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[1]", VLProperty::FLO AT);
sIdPropertyDesc Map[10202] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[2]", VLProperty::FLO AT);
sIdPropertyDesc Map[10203] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[3]", VLProperty::FLO AT);
sIdPropertyDesc Map[10204] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[4]", VLProperty::FLO AT);
sIdPropertyDesc Map[10205] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[5]", VLProperty::FLO AT);
sIdPropertyDesc Map[10206] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[6]", VLProperty::FLO AT);
sIdPropertyDesc Map[10207] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[7]", VLProperty::FLO AT);
sIdPropertyDesc Map[10208] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[8]", VLProperty::FLO AT);
sIdPropertyDesc Map[10209] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/float[9]", VLProperty::FLO AT);

sIdPropertyDesc Map[10300] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[0]", VLProperty::INT );
sIdPropertyDesc Map[10301] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[1]", VLProperty::INT );
sIdPropertyDesc Map[10302] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[2]", VLProperty::INT );
sIdPropertyDesc Map[10303] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[3]", VLProperty::INT );
sIdPropertyDesc Map[10304] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[4]", VLProperty::INT );
sIdPropertyDesc Map[10305] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[5]", VLProperty::INT );
sIdPropertyDesc Map[10306] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[6]", VLProperty::INT );
sIdPropertyDesc Map[10307] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[7]", VLProperty::INT );
sIdPropertyDesc Map[10308] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[8]", VLProperty::INT );
sIdPropertyDesc Map[10309] = VLMultiplayMgr: :PropertyDesc(" sim/
multiplay/generic/int[9]", VLProperty::INT );

}
VLAIManager::Mo delPropertyData Map
VLAIManager::sA ircraftProperty DataMap =
VLAIManager::Mo delPropertyData Map();

std::map<std::s tring,std::stri ng>
VLAIManager::sA ircraftModelNam eMap =
std::map<std::s tring,std::stri ng>();
std::vector<VLP ropertyData>
VLAIManager::ad dProperties(con st std::string& aircraft)
{
if (0 < sAircraftProper tyDataMap.count (aircraft))
return sAircraftProper tyDataMap[aircraft][1];

std::vector <VLPropertyData >* constantPropert ies;
std::vector <VLPropertyData >* varyingProperti es;
VLPropertyData propertyData;
std::string modelName, id, value, cv;
//VLPropertyData* ptr;
//size_t pos = fileName.find('/');
//fileName = fileName.substr (pos + 1, fileName.find('/', pos+1) - pos
- 1) + ".txt";

std::ifstream myfile(("./data/" + aircraft + ".txt").data()) ;
std::cout << __FILE__ << ":" << __LINE__ << " " << aircraft << "\n";
sAircraftProper tyDataMap[aircraft] = new std::vector
<VLPropertyData >[2];
constantPropert ies = &sAircraftPrope rtyDataMap[aircraft][0];
varyingProperti es = &sAircraftPrope rtyDataMap[aircraft][1];

if (myfile.is_open ())
{
myfile >modelName;
sAircraftModelN ameMap[aircraft] = modelName;

while (! myfile.eof() )
{
//ptr = new VLPropertyData;
myfile >id >value >cv;

propertyData.id = atoi(id.data()) ;

if (VLMultiplayMgr ::sIdPropertyDe scMap.count(pro pertyData.id) 0)
{
propertyData.ty pe =
VLMultiplayMgr: :sIdPropertyDes cMap[propertyData.id].type;;

// How we decode the remainder of the property depends on the type
switch (propertyData.t ype) {
case VLProperty::INT :
case VLProperty::BOO L:
case VLProperty::LON G:
propertyData.in t_value = atoi(value.data ());
//cout << pData->int_value << "\n";
break;
case VLProperty::FLO AT:
case VLProperty::DOU BLE:
propertyData.fl oat_value = atof(value.data ());
//cout << pData->float_value << "\n";
break;
case VLProperty::STR ING:
case VLProperty::UNS PECIFIED:

//propertyData.st ring_value = value;
//propertyData.st ring_value = (char*) (new
std::string(val ue.c_str()))->c_str();
int len;
len = value.length();

propertyData.st ring_value = new char[len + 1];

strcpy(property Data.string_val ue, value.data());
propertyData.st ring_value[len] = '\0';

//std::cout << propertyData.st ring_value << "\n";
break;

default:
propertyData.fl oat_value = atof(value.data ());
std::cout << "Unknown Prop type " << propertyData.id << " " <<
propertyData.ty pe << "\n";
break;
}
if (cv == "c")
constantPropert ies->push_back(prop ertyData);
else if (cv == "v")
varyingProperti es->push_back(prop ertyData);
else
std::cout << "Property type c|v error\n";
}
else
{
// We failed to find the property. We'll try the next packet
immediately.
//cout << " Unknown\n";
}

myfile >cv;

}

myfile.close();
}

else
std::cout << __FILE__ << ":" << __LINE__ << " "<< "Unable to open
file\n";

std::cout << __FILE__ << ":" << __LINE__ << " " << aircraft << "\n";
std::vector<VLP ropertyDatavar = sAircraftProper tyDataMap[aircraft]
[1];

std::cout << sAircraftProper tyDataMap.count (aircraft) << "\n";

return var;

}

VLAIManager::VL AIManager()
{

}
VLAIManager::~V LAIManager()
{

}

int
main()
{

VLAIManager* mgr = new VLAIManager;
VLMultiplayMgr: :Init();

mgr->addProperties( "bo105");

std::string name ("oktay");

std::cout << name.length()<< ":" << name.size() << "\n";
std::cout << "ok\n";
return 0;
}
//data file

Aircraft/bo105/Models/bo105.xml
10002 Hello c sim/multiplay/chat
10300 1 c sim/multiplay/generic/
int[0]
10001 118500000 c sim/multiplay/transmission-freq-hz
10100 Yellow_MedEvac c sim/multiplay/generic/string[0]
10101 red-cross c sim/multiplay/generic/string[1]
// output of program

new data
.../main.cpp:321 bo105
Hello is deleted
is deleted
*** glibc detected *** /home/mvms/workspace/reading/Debug/reading:
double free or corruption (fasttop): 0x0000000001f23 2a0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x33c9a78158]
/lib64/libc.so.6(cfree +0x76)[0x33c9a7a796]
/home/mvms/workspace/reading/Debug/reading[0x408ed0]
/home/mvms/workspace/reading/Debug/reading[0x408ee7]
/home/mvms/workspace/reading/Debug/reading[0x408f04]
/home/mvms/workspace/reading/Debug/reading[0x408f36]
/home/mvms/workspace/reading/Debug/reading[0x40b666]
/home/mvms/workspace/reading/Debug/reading[0x40b73c]
/home/mvms/workspace/reading/Debug/reading[0x402137]
/home/mvms/workspace/reading/Debug/reading[0x4069d5]
/lib64/libc.so.6(__lib c_start_main+0x fa)[0x33c9a1e32a]
/home/mvms/workspace/reading/Debug/reading(__gxx_p ersonality_v0+0 x91)
[0x4017e9]
======= Memory map: ========
00400000-00411000 r-xp 00000000 fd:00
295669 /home/mvms/workspace/reading/Debug/
reading
00610000-00611000 rw-p 00010000 fd:00
295669 /home/mvms/workspace/reading/Debug/
reading
01f1f000-01f40000 rw-p 01f1f000 00:00
0 [heap]
33c8800000-33c881d000 r-xp 00000000 fd:00
2179394 /lib64/ld-2.8.so
33c8a1c000-33c8a1d000 r--p 0001c000 fd:00
2179394 /lib64/ld-2.8.so
33c8a1d000-33c8a1e000 rw-p 0001d000 fd:00
2179394 /lib64/ld-2.8.so
33c9a00000-33c9b62000 r-xp 00000000 fd:00
2179395 /lib64/libc-2.8.so
33c9b62000-33c9d62000 ---p 00162000 fd:00
2179395 /lib64/libc-2.8.so
33c9d62000-33c9d66000 r--p 00162000 fd:00
2179395 /lib64/libc-2.8.so
33c9d66000-33c9d67000 rw-p 00166000 fd:00
2179395 /lib64/libc-2.8.so
33c9d67000-33c9d6c000 rw-p 33c9d67000 00:00 0
33c9e00000-33c9e84000 r-xp 00000000 fd:00
2179407 /lib64/libm-2.8.so
33c9e84000-33ca083000 ---p 00084000 fd:00
2179407 /lib64/libm-2.8.so
33ca083000-33ca084000 r--p 00083000 fd:00
2179407 /lib64/libm-2.8.so
33ca084000-33ca085000 rw-p 00084000 fd:00
2179407 /lib64/libm-2.8.so
33d0200000-33d0216000 r-xp 00000000 fd:00
2179408 /lib64/libgcc_s-4.3.0-20080428.so.1
33d0216000-33d0415000 ---p 00016000 fd:00
2179408 /lib64/libgcc_s-4.3.0-20080428.so.1
33d0415000-33d0416000 rw-p 00015000 fd:00
2179408 /lib64/libgcc_s-4.3.0-20080428.so.1
33d3e00000-33d3eee000 r-xp 00000000 fd:00
2602470 /usr/lib64/libstdc++.so.6. 0.10
33d3eee000-33d40ee000 ---p 000ee000 fd:00
2602470 /usr/lib64/libstdc++.so.6. 0.10
33d40ee000-33d40f5000 r--p 000ee000 fd:00
2602470 /usr/lib64/libstdc++.so.6. 0.10
33d40f5000-33d40f7000 rw-p 000f5000 fd:00
2602470 /usr/lib64/libstdc++.so.6. 0.10
33d40f7000-33d410a000 rw-p 33d40f7000 00:00 0
7f9f40000000-7f9f40021000 rw-p 7f9f40000000 00:00 0
7f9f40021000-7f9f44000000 ---p 7f9f40021000 00:00 0
7f9f449f6000-7f9f449f9000 rw-p 7f9f449f6000 00:00 0
7f9f44a14000-7f9f44a17000 rw-p 7f9f44a14000 00:00 0
7fff4ca01000-7fff4ca16000 rw-p 7ffffffea000 00:00
0 [stack]
7fff4cbfe000-7fff4cc00000 r-xp 7fff4cbfe000 00:00
0 [vdso]
ffffffffff60000 0-ffffffffff60100 0 r-xp 00000000 00:00
0 [vsyscall]
Jul 3 '08 #1
10 2197
On Thu, 03 Jul 2008 04:45:02 -0700, oktayarslan wrote:
Hi all;

I have a problem when inserting an element to a vector. All I want is
reading some data from a file and putting them into a vector. But the
program is crashing after pushing a data which has string value. I
really do not understand why push_back() function is trying to remove
previously inserted data.

Thanks for any help

yatko
// program
[snip huge chunk of code]

Blimey, you're joking surely? No way I for one am wading through that
lot! Please see:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

with particular regard to the phrase "Post minimal code: just enough to
demonstrate the problem".

(I'll wager that in creating a minimal program demonstrating the issue
you'll find that the problem actually exists elsewhere in your code).

--
Lionel B
Jul 3 '08 #2
ok*********@gma il.com wrote:
Hi all;

I have a problem when inserting an element to a vector. All I want is
reading some data from a file and putting them into a vector. But the
program is crashing after pushing a data which has string value.

Without diving too deeply into your code (too much, post something _minimal_
that exhibits the problem), I venture the following conjecture: you are
using a vector

vector< some_user_defin ed_type >

and some_user_defin ed_type does not satisfy the copy-constructible and
assignable requirements due to mishandling of char* members.

I really do not understand why push_back() function is trying to remove
previously inserted data.
It has to do that whenever the vector reallocates.

[snip code]
Best

Kai-Uwe Bux
Jul 3 '08 #3
Hi all;

I am sorry for previous posting. Actually, the problem can be
described as follows:

I have a struct given below and I am trying to read properties from a
file and store all properties into a std::vector < VLPropertyData>
vector. But the program crashes whenever I put a property with string
data into the vector. I have written some codes in the destructor and
constructor and I see that push_back() function tries to remove some
of previously inserted elements. Hence, this causes invalid pointer
error as you can see in the outputs.

Thanks

yatko
struct VLPropertyData {
unsigned id;

// While the type isn't transmitted, it is needed for the destructor
VLProperty::Typ e type;
union{
int int_value;
float float_value;
char* string_value;
};

//std::string string_value;
VLPropertyData( )
{
std::cout << "new data\n";
}

~VLPropertyData () {
if ((type == VLProperty::STR ING) || (type ==
VLProperty::UNS PECIFIED))
{
std::cout << string_value << " is deleted" << std::endl;
delete [] string_value;

}
}

};
Jul 3 '08 #4
On Jul 3, 9:49 am, oktayars...@gma il.com wrote:
Hi all;

I am sorry for previous posting. Actually, the problem can be
described as follows:

I have a struct given below and I am trying to read properties from a
file and store all properties into a std::vector < VLPropertyData>
vector. But the program crashes whenever I put a property with string
data into the vector. I have written some codes in the destructor and
constructor

But you didn't write copy constructor and operator=.
Once you do that it shouldn't crash anymore.
Jul 3 '08 #5
ok*********@gma il.com writes:
Hi all;

I have a problem when inserting an element to a vector. All I want is
reading some data from a file and putting them into a vector. But the
program is crashing after pushing a data which has string value. I
really do not understand why push_back() function is trying to remove
previously inserted data.

Thanks for any help
class VLProperty
{

public:
enum Aircraft{
BO105,
F16
};

enum Type{
INT,
BOOL,
LONG,
FLOAT,
DOUBLE,
STRING,
UNSPECIFIED
};
};
This is not a class.
What if you have an Aircraft named the LONG ?

Better write:

namespace Aircraft {
enum Aircraft {
Bo105, F16, Unspecified }};

namespace Type {
enum Type {
Int, Bool, Long, Float, String, Unspecified }};

and: Type::Int, Type::Unspecifi ed, Aircraft::Unspe cified, Aircraft::F16.
But see below, you shouldn't need these enums.
struct VLPropertyData {
unsigned id;

// While the type isn't transmitted, it is needed for the destructor
VLProperty::Typ e type;
union{
int int_value;
float float_value;
char* string_value;
};

//std::string string_value;
VLPropertyData( )
{
std::cout << "new data\n";
}

~VLPropertyData () {
if ((type == VLProperty::STR ING) || (type ==
VLProperty::UNS PECIFIED))
{
std::cout << string_value << " is deleted" << std::endl;
delete [] string_value;

}
}
};

This is very wrong.

class Data {
protected:
unsigned id;
public:
virtual ~Data(){
std::cout<<"dat a id "<<id<<" is deleted"<<std:: endl;
};
};
class Int:public Data {
protected:
int value;
public:
Int(int aValue):value(a Value){}
Int(float aValue):value(a Value){}
Int(string aValue):value(a toi(aValue)){} // for example, if you need it.
virtual ~Int(){
std::cout<<"An Int ";
}
};

class String:public Data {
protected:
std::string value;
public:
String(int aValue){ std::ostringstr eam s; s<<aValue; value=s.str(); } // for example, if you need it.
String(string aValue):value(a Value){}
virtual ~String(){
std::cout<<"A String ";
// There's nothing to do to delete a std::string, it'll be done automatically!
}
};

etc for the other types.
class Property{
protected:
std::string name;
Data* value;
public:
Property(std::s tring aName,int aValue):name(aN ame),value(new Int(aValue){}
Property(std::s tring aName,std::stri ng aValue):name(aN ame),value(new String(aValue){ }
...
virtual ~Property(){
delete value;
}
};
property[i++]=Property::Prop erty("wheels",4 ");
property[i++]=Property::Prop erty("name","a string");
property[i++]=Property::Prop erty("pi",3.14) ;
if (VLMultiplayMgr ::sIdPropertyDe scMap.count(pro pertyData.id) 0)
{
propertyData.ty pe = VLMultiplayMgr: :sIdPropertyDes cMap[propertyData.id].type;;

// How we decode the remainder of the property depends on the type
switch (propertyData.t ype) {
case VLProperty::INT :
case VLProperty::BOO L:
case VLProperty::LON G:
propertyData.in t_value = atoi(value.data ());
This is wrong. Each type of data is written with a specific syntax.
This is wrong because you are giving the same bit of information
twice, once in the type, and another time in the syntax. What would happen if you had:
VLProperty::INT "\"toto\""
VLProperty::INT "3.14"
VLProperty::INT "42"

Only the last couple is valid. Forget the type! Analyse the token,
and infer the type automatically:

"false" --new Bool(false);
"\"toto\"" --new String("toto");
"3.14" --new Float(3.14);
"42" --new Int(42);

If you want to do it correctly, have a look at lex or flex.

Otherwise if you want to do a slow Q&D kludge, you can use regexps:

if(match("^ *false $",value)){
return new Bool(false);
}else if(match("^ *true $",value)){
return new Bool(true);
}else if(match("^[0-9][0-9]*$",value)){
return new Int(atoi(value) );
}else if(match("^[0-9][0-9]*\\.[0-9][0-9]*\\([Ee]\\(+\\|-\\)\\?[0-9][0-9]*$",value)){
return new Float(atof(valu e));
}else if(match("^\"\\ ([^\"]*\\|\\.\\)*\"$" ,value)){
return new String(unwrapSt ring(value));
....
}else{
error("lexical error: invalid token: ",value);
}
--
__Pascal Bourguignon__
Jul 3 '08 #6
ok*********@gma il.com wrote:
Hi all;

I am sorry for previous posting. Actually, the problem can be
described as follows:

I have a struct given below and I am trying to read properties from a
file and store all properties into a std::vector < VLPropertyData>
vector. But the program crashes whenever I put a property with string
data into the vector. I have written some codes in the destructor and
constructor and I see that push_back() function tries to remove some
of previously inserted elements. Hence, this causes invalid pointer
error as you can see in the outputs.

Thanks

yatko
struct VLPropertyData {
unsigned id;

// While the type isn't transmitted, it is needed for the destructor
VLProperty::Typ e type;
union{
int int_value;
float float_value;
char* string_value;
};

//std::string string_value;
VLPropertyData( )
{
std::cout << "new data\n";
}

~VLPropertyData () {
if ((type == VLProperty::STR ING) || (type ==
VLProperty::UNS PECIFIED))
{
std::cout << string_value << " is deleted" << std::endl;
delete [] string_value;

}
}

};
As predicted :-)

The type VLPropertyData does not handle copy construction and assignment
correctly. Therefore, it does not satisfy the requirements for use in
standard container classes. Write a copy constructor and assignment
operator with deep copy semantics, and you should be fine.
Best

Kai-Uwe Bux
Jul 3 '08 #7
Thanks guys.

I wrote copy constructor and assignment operator and it works
perfectly.

yatko
Jul 3 '08 #8
ok*********@gma il.com writes:
Hi all;

I am sorry for previous posting. Actually, the problem can be
described as follows:

I have a struct given below and I am trying to read properties from a
file and store all properties into a std::vector < VLPropertyData>
vector. But the program crashes whenever I put a property with string
data into the vector. I have written some codes in the destructor and
constructor and I see that push_back() function tries to remove some
of previously inserted elements. Hence, this causes invalid pointer
error as you can see in the outputs.
As explained by Darío, you are missing a copy constructor. That's
needed because push_back may have to copy the objects inserted in the
vector to some other place when it expands the size of the vector.

In any normal OO programming languages, objects are actually
references (pointers) to the memory block used to store the object
state. But not in C++. Here objects are the memory block itself.
Therefore when you don't explicitely use pointers to objects (which
IMO you should do always, objects are made to live on a heap), then
you have to copy the object from one place to the other (instead of
only copying the pointer) and therefore cannot keep the object
identity.
--
__Pascal Bourguignon__
Jul 3 '08 #9
ok*********@gma il.com wrote:
Thanks guys.

I wrote copy constructor and assignment operator and it works
perfectly.

yatko
Congratulations !!!!
Jul 3 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4735
by: Ivan Paganini | last post by:
Hello all. I'm trying to use a vector<double> to alocate de data that i need, and I am passing by reference to the function to receive the values. The code goes like this: //code void GenerateRandomPositions(vector<double>& posiX, vector<double>& posiY, int nbneurons, int* sizespace) {posiX.clear();
5
4036
by: Abhishek Pandey | last post by:
Hi, I have a vector of strings. I want to hold it only unique strings. That is, before adding any new string, I want to check if it is already present, and if yes, then I will not add the new item. Is there a simpler and efficient way of doing it (instead of traversing linearly through the whole list and comparing strings everytime I want to add new item). Thanks in advance, Abhishek
34
4170
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
2
3777
by: silversurfer | last post by:
Hello, I am a little unsure whether this method really makes sense. The goal is to add an element to a vector. This is the struct and method I am using: std::vector<Entry> models; struct Entry{ int index; FeatureVector* value;
3
1273
by: Alan | last post by:
I am having a strange problem with a vector. I am reading data in from a file and putting it in the vector. On the third item read in, it reaches the .push_back code. However, after that, I use .size(), and it says that it only has two items. This is bizarre. Any thoughts? Code fragment may be found below. Thanks, Alan while (inFile >sensed_data.time >sensed_data.sensor_who >> sensed_data.aircraft_who
8
1950
by: Mike Jolley | last post by:
Hello First off, I'm a student so I'm pretty new to C++, and therefore I have probably made a stupid mistake somewhere. Anyway Ive been trying to fix this 5 hours straight now, so i need a little assistance. What I'm trying to do I am using inheritance to make some bookings for a marina, which are: Booking
13
2465
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
15
4153
by: vivekian | last post by:
Hi, I have this following class class nodeInfo ; class childInfo { public: int relativeMeshId ;
9
1924
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can consist of one or more words and a few integers. I'm able to create the file properly, as confimed by viewing it in a text editor, but something goes wrong when I tried to read it. I've made a test program illustrating the problem: #include...
0
9401
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
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9111
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
8096
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
6702
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
4517
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...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.