TMagic*04
March 10th, 2010, 01:26 PM
I was wondering if someone could explain this seemingly bizarre phenomenon to me. I have a vector of custom struct type address defined as such:
struct address{
char add[11];
char owner[5];
};
vector<address> aList;
and is filled in this function:
void AFC:: generateAddressBatch()
{
int index =0;
string foo;
//if none generate first 5
if(aList.empty())
{
foo = "192.168.0.";
char addr[11];
memcpy(addr, foo.c_str(), foo.size());
for (index; index < 5; index++)
{
struct address tmp;
addr[10] = index + 48;
memcpy(tmp.add, addr, sizeof(addr));
tmp.owner[0] = 0;
cout << "NEW ADDRESS GENERATED: " << tmp.add << endl;
aList.push_back(tmp);
}
}
else
{
index = aList.size() - 1;
int max = index+5;
for (index; index < max; index++)
{
struct address tmp;
foo = "192.168.0." + index;
memcpy(tmp.add, foo.c_str(), foo.size());
tmp.owner[0] = 0;
aList.push_back(tmp);
}
}
}
OUTPUT:
NEW ADDRESS GENERATED: 192.168.0.0
NEW ADDRESS GENERATED: 192.168.0.1
NEW ADDRESS GENERATED: 192.168.0.2
NEW ADDRESS GENERATED: 192.168.0.3
NEW ADDRESS GENERATED: 192.168.0.4
When I later try and pull the struct for editing, the data in the field add ouputs as It should, but after I edit owner it changes in a strange way which you can see in my output below.
address AFC:: getFreeAdd(string name)
{
for(int i = 0; i < aList.size(); i++)
{
if(aList.at(i).owner[0] == 0)
{
cout << aList.at(i).add << endl;
memcpy(aList.at(i).owner, name.c_str(), name.size());
cout << aList.at(i).add << endl;
return aList.at(i);
}
}
cout << "| -- -- No more free address'..." << endl;
}
OUTPUT:
192.168.0.0
192.168.0.0m�192.168.0.1
Could anyone with more knowledge of C please explain to me why this happens, so that I can try and prevent it.
Thanks very much
struct address{
char add[11];
char owner[5];
};
vector<address> aList;
and is filled in this function:
void AFC:: generateAddressBatch()
{
int index =0;
string foo;
//if none generate first 5
if(aList.empty())
{
foo = "192.168.0.";
char addr[11];
memcpy(addr, foo.c_str(), foo.size());
for (index; index < 5; index++)
{
struct address tmp;
addr[10] = index + 48;
memcpy(tmp.add, addr, sizeof(addr));
tmp.owner[0] = 0;
cout << "NEW ADDRESS GENERATED: " << tmp.add << endl;
aList.push_back(tmp);
}
}
else
{
index = aList.size() - 1;
int max = index+5;
for (index; index < max; index++)
{
struct address tmp;
foo = "192.168.0." + index;
memcpy(tmp.add, foo.c_str(), foo.size());
tmp.owner[0] = 0;
aList.push_back(tmp);
}
}
}
OUTPUT:
NEW ADDRESS GENERATED: 192.168.0.0
NEW ADDRESS GENERATED: 192.168.0.1
NEW ADDRESS GENERATED: 192.168.0.2
NEW ADDRESS GENERATED: 192.168.0.3
NEW ADDRESS GENERATED: 192.168.0.4
When I later try and pull the struct for editing, the data in the field add ouputs as It should, but after I edit owner it changes in a strange way which you can see in my output below.
address AFC:: getFreeAdd(string name)
{
for(int i = 0; i < aList.size(); i++)
{
if(aList.at(i).owner[0] == 0)
{
cout << aList.at(i).add << endl;
memcpy(aList.at(i).owner, name.c_str(), name.size());
cout << aList.at(i).add << endl;
return aList.at(i);
}
}
cout << "| -- -- No more free address'..." << endl;
}
OUTPUT:
192.168.0.0
192.168.0.0m�192.168.0.1
Could anyone with more knowledge of C please explain to me why this happens, so that I can try and prevent it.
Thanks very much