struct Mars_
{
int ix ;
char iy[18] ;
double iz ;
};
typedef struct Mars_ Mars ;
vector<unique_ptr<Mars> > vec ;
void NewItem(const Mars& m)
{
void* buffer ;
if (posix_memalign(&buffer, 64,sizeof(Mars)) != 0)
{
perror("posix_memalign did not work!");
abort() ;
}
printf("address=(%p) \n",buffer) ;
Mars* ptr = new(buffer) Mars() ;
ptr->ix = m.ix ;
strcpy(ptr->iy,m.iy) ;
ptr->iz = m.iz ;
vec.emplace_back(ptr) ;
}
int main()
{
Mars m ;
char s[10] ;
strcpy(s,"hello") ;
for(int idx=0;idx<10;idx++)
{
m.ix = idx ;
char stmp[18] ;
sprintf(stmp,"%s%02d",s,idx) ;
strcpy(m.iy,stmp) ;
m.iz = idx * idx * 1.1 ;
NewItem(m) ;
}
vector<unique_ptr<Mars> >::iterator it ;
for (it = vec.begin() ; it != vec.end(); ++it)
{
cout << (*it)->ix << " " ;
cout << (*it)->iy << " " ;
cout << (*it)->iz << endl ;
}
}
output :
address=(0xf4d040)
address=(0xf4d100)
address=(0xf4d1c0)
address=(0xf4d280)
address=(0xf4d300)
address=(0xf4d3c0)
address=(0xf4d440)
address=(0xf4d500)
address=(0xf4d580)
address=(0xf4d680)
0 hello00 0
1 hello01 1.1
2 hello02 4.4
3 hello03 9.9
4 hello04 17.6
5 hello05 27.5
6 hello06 39.6
7 hello07 53.9
8 hello08 70.4
9 hello09 89.1
留言列表