hello everybody!
I have used reinterpret_cast for interpret a class object as a char*.
This is the object: - template<class T>
-
class Coordinates {
-
public:
-
T *x;
-
T *y;
-
int size;
-
public:
-
Coordinates();
-
Coordinates(int s, T data);
-
Coordinates(const Coordinates &c);
-
~Coordinates();;
-
-
void zeros(void);
-
};
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: - template<class T>
-
void _write(Coordinates<T> *c, ofstream &file)
-
{
-
int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
-
char *buffer=new char[buffer_size];
-
for(int i=0; i<buffer_size; i++)
-
buffer[i]=(reinterpret_cast<char*>(c))[i];
-
-
file.write(reinterpret_cast<const char*>(buffer), buffer_size);
-
-
/* file.write(reinterpret_cast<const char*>(&(c->size)), sizeof(c->size));
-
file.write(reinterpret_cast<const char*>(c->x), (c->size)*sizeof(T));
-
file.write(reinterpret_cast<const char*>(c->y), (c->size)*sizeof(T));
-
*/
-
}
But when I read it from an ifstream like this: - template<class T>
-
void _read(Coordinates<T> *c, ifstream &file)
-
{
-
int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
-
char *buffer=new char[buffer_size];
-
file.read(buffer, buffer_size);
-
-
for(int i=0;i<buffer_size;i++)
-
(reinterpret_cast<char*>(c))[i]=buffer[i];
-
-
/* file.read(reinterpret_cast<char*>(&(c->size)), sizeof(c->size));
-
-
T *tempx=new T[c->size];
-
T *tempy=new T[c->size];
-
file.read(reinterpret_cast<char*>(tempx), c->size*sizeof(T));
-
file.read(reinterpret_cast<char*>(tempy), c->size*sizeof(T));
-
for(int i=0; i<c->size; i++) {
-
c->x[i]=tempx[i];
-
c->y[i]=tempy[i];
-
}
-
delete [] tempx;
-
delete [] tempy;
-
*/
-
/*
-
file.read(reinterpret_cast<char*>(&(c->size)), sizeof(c->size));
-
file.read(reinterpret_cast<char*>(c->x), (c->size)*sizeof(T));
-
file.read(reinterpret_cast<char*>(c->y), (c->size)*sizeof(T));
-
*/
-
}
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: - #include<iostream>
-
#include<fstream>
-
using namespace std;
-
-
template<class T>
-
class Coordinates {
-
public:
-
T *x;
-
T *y;
-
int size;
-
public:
-
Coordinates();
-
Coordinates(int s, T data);
-
Coordinates(const Coordinates &c);
-
~Coordinates();;
-
-
void zeros(void);
-
};
-
-
template<class T>
-
Coordinates<T>::Coordinates() : size(0), x(0), y(0)
-
{
-
cout << "Coordinates<T>::Coordinates()\n";
-
}
-
-
template<class T>
-
Coordinates<T>::Coordinates(int s, T data) : size(s)
-
{
-
x=new T[size];
-
y=new T[size];
-
-
for(int i=0; i<size; i++) {
-
x[i]=i;
-
y[i]=i+1;
-
}
-
-
cout << "Coordinate<T>::Coordinates\n";
-
}
-
-
template<class T>
-
Coordinates<T>::Coordinates(const Coordinates &c)
-
{
-
size=c.size;
-
x=new T[size];
-
y=new T[size];
-
-
for(int i=0; i<size; i++) {
-
x[i]=c.x[i];
-
y[i]=c.y[i];
-
}
-
}
-
-
template<class T>
-
Coordinates<T>::~Coordinates()
-
{
-
cout << "Coordinates<T>::~Coordinates\n";
-
if(x) delete [] x;
-
if(y) delete [] y;
-
}
-
-
template<class T>
-
void Coordinates<T>::zeros(void)
-
{
-
for(int i=0; i<size; i++)
-
x[i]=y[i]=0;
-
}
-
-
template<class T>
-
void _write(Coordinates<T> *c, ofstream &file)
-
{
-
int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
-
char *buffer=new char[buffer_size];
-
for(int i=0; i<buffer_size; i++)
-
buffer[i]=(reinterpret_cast<char*>(c))[i];
-
-
file.write(reinterpret_cast<const char*>(buffer), buffer_size);
-
-
/* file.write(reinterpret_cast<const char*>(&(c->size)), sizeof(c->size));
-
file.write(reinterpret_cast<const char*>(c->x), (c->size)*sizeof(T));
-
file.write(reinterpret_cast<const char*>(c->y), (c->size)*sizeof(T));
-
*/
-
}
-
-
template<class T>
-
void _read(Coordinates<T> *c, ifstream &file)
-
{
-
int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
-
char *buffer=new char[buffer_size];
-
file.read(buffer, buffer_size);
-
-
for(int i=0;i<buffer_size;i++)
-
(reinterpret_cast<char*>(c))[i]=buffer[i];
-
-
/* file.read(reinterpret_cast<char*>(&(c->size)), sizeof(c->size));
-
-
T *tempx=new T[c->size];
-
T *tempy=new T[c->size];
-
file.read(reinterpret_cast<char*>(tempx), c->size*sizeof(T));
-
file.read(reinterpret_cast<char*>(tempy), c->size*sizeof(T));
-
for(int i=0; i<c->size; i++) {
-
c->x[i]=tempx[i];
-
c->y[i]=tempy[i];
-
}
-
delete [] tempx;
-
delete [] tempy;
-
*/
-
/*
-
file.read(reinterpret_cast<char*>(&(c->size)), sizeof(c->size));
-
file.read(reinterpret_cast<char*>(c->x), (c->size)*sizeof(T));
-
file.read(reinterpret_cast<char*>(c->y), (c->size)*sizeof(T));
-
*/
-
}
-
-
template<class T>
-
void print(Coordinates<T> *c)
-
{
-
for(int i=0; i<c->size; i++)
-
cout << '(' << c->x[i] << ',' << c->y[i] << ')' << endl;
-
}
-
-
int main() {
-
ofstream out("file", ios::binary);
-
Coordinates<int> *c = new Coordinates<int>(5, 4);
-
//////////////////////////////
-
//just for change the numbers pattern
-
c->x[3]=99;
-
c->y[3]=99;
-
//////////////////////////////
-
print(c);
-
_write(c, out);
-
out.close();
-
delete c;
-
-
ifstream in("file", ios::binary);
-
Coordinates<int> *d = new Coordinates<int>(5, 3);
-
//////////////////////////////
-
//just for change the pattern
-
d->zeros();
-
//////////////////////////////
-
print(d);
-
cout << endl;
-
_read(d, in);
-
print(d);
-
in.close();
-
delete d;
-
-
return 0;
-
}
Can you help me with this.
Why the "x" array is exactly the same and the "y" isn't
regards jorge
2 2932
I was just curious in your read function to see: -
int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
-
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.
I was just curious in your read function to see: -
int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
-
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: -
int buffer_size=c->size*sizeof(T)*2 + sizeof(c->size);
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
...
|
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...
|
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...
|
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...
|
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);...
|
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....
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |