473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

reinterpret_cast doesn't seem to work

2
hello everybody!

I have used reinterpret_cast for interpret a class object as a char*.
This is the object:

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class Coordinates {
  3. public:
  4.     T *x;
  5.     T *y;
  6.     int size;
  7. public:
  8.     Coordinates();
  9.     Coordinates(int s, T data);
  10.     Coordinates(const Coordinates &c);
  11.     ~Coordinates();;
  12.  
  13.     void zeros(void);
  14. };
First I have calculated the size of the object, then I have allocated a buffer(char*) on the heap, next I have copied the object into the buffer, finally I have wrote the buffer into a ofstream just like this:

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. void _write(Coordinates<T> *c, ofstream &file)
  3. {
  4.     int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
  5.     char *buffer=new char[buffer_size];
  6.     for(int i=0; i<buffer_size; i++) 
  7.         buffer[i]=(reinterpret_cast<char*>(c))[i];
  8.  
  9.     file.write(reinterpret_cast<const char*>(buffer), buffer_size);
  10.  
  11. /*    file.write(reinterpret_cast<const char*>(&(c->size)), sizeof(c->size));
  12.     file.write(reinterpret_cast<const char*>(c->x), (c->size)*sizeof(T));
  13.     file.write(reinterpret_cast<const char*>(c->y), (c->size)*sizeof(T));
  14. */    
  15. }
But when I read it from an ifstream like this:

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. void _read(Coordinates<T> *c, ifstream &file)
  3. {
  4.     int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
  5.     char *buffer=new char[buffer_size];
  6.     file.read(buffer, buffer_size);
  7.  
  8.     for(int i=0;i<buffer_size;i++) 
  9.         (reinterpret_cast<char*>(c))[i]=buffer[i];
  10.  
  11. /*    file.read(reinterpret_cast<char*>(&(c->size)), sizeof(c->size));
  12.  
  13.     T *tempx=new T[c->size];
  14.     T *tempy=new T[c->size];
  15.     file.read(reinterpret_cast<char*>(tempx), c->size*sizeof(T));
  16.     file.read(reinterpret_cast<char*>(tempy), c->size*sizeof(T));
  17.     for(int i=0; i<c->size; i++) {
  18.         c->x[i]=tempx[i];
  19.         c->y[i]=tempy[i];
  20.     }
  21.     delete [] tempx;
  22.     delete [] tempy;
  23. */
  24. /*
  25.     file.read(reinterpret_cast<char*>(&(c->size)), sizeof(c->size));
  26.     file.read(reinterpret_cast<char*>(c->x), (c->size)*sizeof(T));
  27.     file.read(reinterpret_cast<char*>(c->y), (c->size)*sizeof(T));
  28. */    
  29. }
I get a result that is not the desired one:
this is the object i wrote
(x,y)
(0,1)
(1,2)
(2,3)
(99,99)
(4,5)

and this is the object i get when i read it from the ifstream

(x,y)
(0,1)
(1,0)
(2,0)
(99,0)
(4,0)

the "x" array is correct but the "y" array isn't

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. template<class T>
  6. class Coordinates {
  7. public:
  8.     T *x;
  9.     T *y;
  10.     int size;
  11. public:
  12.     Coordinates();
  13.     Coordinates(int s, T data);
  14.     Coordinates(const Coordinates &c);
  15.     ~Coordinates();;
  16.  
  17.     void zeros(void);
  18. };
  19.  
  20. template<class T>
  21. Coordinates<T>::Coordinates() : size(0), x(0), y(0)
  22. {
  23.     cout << "Coordinates<T>::Coordinates()\n";
  24. }
  25.  
  26. template<class T>
  27. Coordinates<T>::Coordinates(int s, T data) : size(s)  
  28. {
  29.     x=new T[size];
  30.     y=new T[size];
  31.  
  32.     for(int i=0; i<size; i++) {
  33.         x[i]=i;
  34.         y[i]=i+1;
  35.     }
  36.  
  37.     cout << "Coordinate<T>::Coordinates\n";
  38. }
  39.  
  40. template<class T>
  41. Coordinates<T>::Coordinates(const Coordinates &c)
  42. {
  43.     size=c.size;
  44.     x=new T[size];
  45.     y=new T[size];
  46.  
  47.     for(int i=0; i<size; i++) {
  48.         x[i]=c.x[i];
  49.         y[i]=c.y[i];
  50.     }
  51. }
  52.  
  53. template<class T>
  54. Coordinates<T>::~Coordinates()
  55. {
  56.     cout << "Coordinates<T>::~Coordinates\n";
  57.     if(x) delete [] x;
  58.     if(y) delete [] y;
  59. }
  60.  
  61. template<class T>
  62. void Coordinates<T>::zeros(void)
  63. {
  64.     for(int i=0; i<size; i++)
  65.         x[i]=y[i]=0;
  66. }
  67.  
  68. template<class T>
  69. void _write(Coordinates<T> *c, ofstream &file)
  70. {
  71.     int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
  72.     char *buffer=new char[buffer_size];
  73.     for(int i=0; i<buffer_size; i++) 
  74.         buffer[i]=(reinterpret_cast<char*>(c))[i];
  75.  
  76.     file.write(reinterpret_cast<const char*>(buffer), buffer_size);
  77.  
  78. /*    file.write(reinterpret_cast<const char*>(&(c->size)), sizeof(c->size));
  79.     file.write(reinterpret_cast<const char*>(c->x), (c->size)*sizeof(T));
  80.     file.write(reinterpret_cast<const char*>(c->y), (c->size)*sizeof(T));
  81. */    
  82. }
  83.  
  84. template<class T>
  85. void _read(Coordinates<T> *c, ifstream &file)
  86. {
  87.     int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
  88.     char *buffer=new char[buffer_size];
  89.     file.read(buffer, buffer_size);
  90.  
  91.     for(int i=0;i<buffer_size;i++) 
  92.         (reinterpret_cast<char*>(c))[i]=buffer[i];
  93.  
  94. /*    file.read(reinterpret_cast<char*>(&(c->size)), sizeof(c->size));
  95.  
  96.     T *tempx=new T[c->size];
  97.     T *tempy=new T[c->size];
  98.     file.read(reinterpret_cast<char*>(tempx), c->size*sizeof(T));
  99.     file.read(reinterpret_cast<char*>(tempy), c->size*sizeof(T));
  100.     for(int i=0; i<c->size; i++) {
  101.         c->x[i]=tempx[i];
  102.         c->y[i]=tempy[i];
  103.     }
  104.     delete [] tempx;
  105.     delete [] tempy;
  106. */
  107. /*
  108.     file.read(reinterpret_cast<char*>(&(c->size)), sizeof(c->size));
  109.     file.read(reinterpret_cast<char*>(c->x), (c->size)*sizeof(T));
  110.     file.read(reinterpret_cast<char*>(c->y), (c->size)*sizeof(T));
  111. */    
  112. }
  113.  
  114. template<class T>
  115. void print(Coordinates<T> *c)
  116. {
  117.     for(int i=0; i<c->size; i++)
  118.         cout << '(' << c->x[i] << ',' << c->y[i] << ')' << endl;
  119. }
  120.  
  121. int main() {
  122.     ofstream out("file", ios::binary);
  123.     Coordinates<int> *c = new Coordinates<int>(5, 4);
  124.     //////////////////////////////
  125.     //just for change the numbers pattern
  126.     c->x[3]=99;
  127.     c->y[3]=99;
  128.     //////////////////////////////
  129.     print(c);
  130.     _write(c, out);
  131.     out.close();
  132.     delete c;
  133.  
  134.     ifstream in("file", ios::binary);
  135.     Coordinates<int> *d = new Coordinates<int>(5, 3);
  136.     //////////////////////////////
  137.     //just for change the pattern
  138.     d->zeros();
  139.     //////////////////////////////
  140.     print(d);
  141.     cout << endl;
  142.     _read(d, in);
  143.     print(d);
  144.     in.close();
  145.     delete d;
  146.  
  147.     return 0;
  148. }
Can you help me with this.
Why the "x" array is exactly the same and the "y" isn't
regards jorge
Apr 26 '07 #1
2 2932
weaknessforcats
9,208 Expert Mod 8TB
I was just curious in your read function to see:
Expand|Select|Wrap|Line Numbers
  1. int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
  2.  
where c is a pointer to a Coordinates object.

How can you calculate the size of the object in the disc file when you haven't read it yet??

These Coordinate objects contain arrays. Therefore, Coordinate objects will be of different sizes in the disc file.

I think you need to format your Coordinate object when you write it os that you knoe how top read it later. Like maybe have the size of the object precede the object?? Also, you need to know how many element sto allow for in the member arrays.
Apr 29 '07 #2
jasm
2
I was just curious in your read function to see:
Expand|Select|Wrap|Line Numbers
  1. int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
  2.  
where c is a pointer to a Coordinates object.

How can you calculate the size of the object in the disc file when you haven't read it yet??

These Coordinate objects contain arrays. Therefore, Coordinate objects will be of different sizes in the disc file.

I think you need to format your Coordinate object when you write it os that you knoe how top read it later. Like maybe have the size of the object precede the object?? Also, you need to know how many element sto allow for in the member arrays.
hi!
thanks for posting!
How can you calculate the size of the object in the disc file when you haven't read it yet??
I can calculate the size of the object because the objects "c" and "d" in the main function have their arrays of the same type and size, just the data inside the arrays is different. Saying that, the member c->size is "fixed". So I have use this line in the read function:

Expand|Select|Wrap|Line Numbers
  1. int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
  2.  
Now if the arrays have the same size and type... why when I read the object from disk I can't get the data that I wrote in the first time?

Just in case you wanna know :
I'm writting a b-tree. The reason of this program is to know how can I write a page of the b-tree on disk. The page will contain two arrays too and all the arrays will have the same size and type; so the size of the entire page will be calculated using this "fixed" size and type.

regards
jorge
Apr 29 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. ...
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
15
by: Aman | last post by:
Hi, wrote this piece of code on SunOS 5.9 , compiler g++ 2.95.3 trying to see the byte order of an int or short int by converting to char* . doesn't work . the char* cpt doesn't seem to be...
13
by: Jakob Bieling | last post by:
Hi, I am trying to determine the endianness of the system as follows: int i = 1; bool is_little = *reinterpret_cast <char*> (&i) != 0; But now I was asking myself, if this use of...
4
by: Alex Vinokur | last post by:
Should 'delete below (after casting to void*)' work fine? ------------------------------------------ char* p1 = new (nothrow) char; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1);...
7
by: Peter | last post by:
I never used reinterpret_cast -- probably because I don't know what it means. Can somebody enlighten me? I looked into Stroustrup's "The annoted C++ reference manual" -- but this was no help....
27
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong...
10
by: Lionel B | last post by:
Greetings, I have some code that is to read unformatted data from disc and interpret it as blocks of unsigned integers. In an attempt to achieve efficiency (it is pretty essential for my...
2
by: ciccio | last post by:
Hi, I was wondering what the main reason is why reinterpret_cast fails to work as expected when using optimizations. Here is the simple example code which fail to give the correct result when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.