Stuck with the std:copy() function
May 21, 2013 at 1:30pm UTC
hi all,
could you describe how can i copy the elements with std::copy() function here is the code, i am stuck with it, thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
static int count = 0;
struct freestash {
};
static freestash* head = reinterpret_cast <freestash*> (new unsigned char [sizeof (int )*10]);
class a {
public :
int dat;
a(int dat_): dat(dat_) { }
~a() { }
void * operator new (size_t) {
return &head[(count++)*sizeof (a)];
}
};
for (int i=0; i < 10; i++) new a(i);
freestash* head_1 = reinterpret_cast <freestash*> (new unsigned char [sizeof (int )*20]);
copy(&head[0], head + 10*sizeof (int ), &head_1[0]); // here is the problem
for (unsigned int i=0; i < 10; i++) cout << "Data of b = " << reinterpret_cast <a*> (&head[i*sizeof (a)])->dat << endl;
May 21, 2013 at 1:56pm UTC
reinterpret_cast?
May 21, 2013 at 2:23pm UTC
yup, any suggestions?
May 21, 2013 at 2:24pm UTC
It certainly looks like you are trying to do something the wrong way.
What exactly are you trying to create? A free store manager?
May 21, 2013 at 2:29pm UTC
exactly. just for experimenting. i've created some different ways. This approach fails while copying the bits to the new one. i want to know why copy procedure fails with it.
thank you.
May 21, 2013 at 5:55pm UTC
it's so ridiculous, but i hâve to find it. the the copy function in line 18 must be
1 2
copy(reinterpret_cast <unsigned char *> (head), reinterpret_cast <unsigned char *> (head+(10*sizeof (int )), reinterpret_cast <unsigned char *> (head_1));
then its Works. i think the copy function must take the adresses byte by byte for non primitive types. May be i am completely wrong :) anyway i found the solution even it looks worse than 30 days old banana in the kitchen...
Topic archived. No new replies allowed.