0

I have a class sample as:

class sample{
public:
    sample(){
        sample_name=new char[10];
    }
    int sample_num;
    float sample_wt;
    char *sample_name;
};

Its object is created and values are accesed as folllows:

sample *object= new sample();
object->sample_num=10;
object->sample_wt=20.02;
object->sample_name="test";

My question is as follows:

How will i create a buffer which contains all the information stored inside object? I tried doing this as follows:

char * buffer = new char[256];
buffer =  reinterpret_cast <char *> ( object );

Now,what i see is the object do consists of all the three values of sample_num, sample_wt and sample_name but these values are not passed to buffer,buffer shows garbage values.

So, how will i get these values inside the buffer?

Scis
  • 2,934
  • 3
  • 23
  • 37
  • You need to design your class to support stateful allocators and use those :-) – Kerrek SB Jun 25 '14 at 08:52
  • You have a memory leak, as your `new char[10]` is never deleted, and can't be because you overwrite the pointer when you use `="test"` – manuell Jun 25 '14 at 08:58
  • When you cast your object to `char *`, you can now access datas stored in it byte per byte. I don't know why you would want to do that. If it's for serialization, it will work for basic types like `int`, `double`, `short` [...] or statically allocated table, but not for pointers : i.e your `char *sample_name` will be unusable (unless it's serialization within the same process). Plus, the `new` statement in your constructor is useless since you assign the pointer directly afterwards. Same thing for your `char *buffer` in your last sample of code. – Unda Jun 25 '14 at 08:58
  • @KerrekSB is there no other way to make my job done as i am not much clear about stateful allocators? – user3767122 Jun 25 '14 at 09:01
  • @user3767122: Well, you have no control over what `new` does. So your design has to change *somehow* to remove the raw `new` in favour of something else that's under your control. The simplest solution may be to just manage all the data manually and serialize it into your buffer in small pieces. – Kerrek SB Jun 25 '14 at 09:14

3 Answers3

0

Instead of using <char *> use <void *> if you are using buffers or you can use stl::vector to buffer some object and put these objects to the vector as shown in below code.

'int main()
{
int x;
int size;


vector<BoxOfProduce>box;
cout<<"How many boxes you want";
cin>>size;
for ( x = 0; x < size; x++)
{
   BoxOfProduce obj; //create an object
   box.push_back(obj); //insert it into the vector
}

for ( x = 0; x < size; x++)
{
    box[x].setItemAry();
    box[x].randomPick();


    box[x].display();
    box[x].change();
    box[x].display2();
}

getchar();getchar();
return 0;

}'

and you can use Ques, Ques are the best options for buffering.

First Create a structure and store all the values in the structure and put that structure in to the queue.

smali
  • 4,687
  • 7
  • 38
  • 60
0

This code is wrong on so many levels.

Your constructor isn't initializing your variables, but creates an array on the free store, and never deletes it. For default constructor just initialize your members to a default value (likely 0 here and nullptr for sample_name).

Replace

sample *object= new sample();
object->sample_num=10;
object->sample_wt=20.02;
object->sample_name="test";

With a proper constructor, that creates an object with the values given as parameters. Also, to create an object, you don't have to use new by default, you can just write

sample object

To get to your question. If you are using reinterpret_cast to see your object as a char[] you are relying on the fact that the layout of your object may or may not be as you see it in it's definition. This may even work, but you need to make sure that your class fulfills the requirements. Here are some further reading on the topic:

Until that, if you want to serialize your object i.e. read and write it to a stream/buffer, you should write serialize/deserialize functions, reading and writing each member to the stream.

Community
  • 1
  • 1
Zsolt
  • 582
  • 2
  • 5
0

Having in mind @Kerrek SB memory leaks and using standard strings and input/output operators:

#include <ostream>
#include <iostream>
#include <sstream>

class sample
{
  public:
    sample() {}
    int sample_num;
    float sample_wt;
    std::string sample_name;

    friend std::ostream & operator<<(std::ostream &, const sample&);
};

std::ostream & operator<<(std::ostream & os, const sample & sam)
{
  os << sam.sample_num << " " << sam.sample_wt << " " << sam.sample_name.c_str();
  return os;
}

int main(int argc, const char * argv[])
{
  sample *object= new sample;
  object->sample_num=10;
  object->sample_wt=20.02f;
  object->sample_name="test";

  std::stringstream buffer;

  buffer << *object;

  std::cout << buffer.str();

  delete object;

  return 0;
}
Tio Pepe
  • 3,071
  • 1
  • 17
  • 22