views:

141

answers:

3

Pretty new to c++, i have a rather (i think) stupid question about a piece of code :

class DataStream
{
protected:
 DataStream(void) { };
public:
 DataStream(int Length); 
 ~DataStream(void);

...
 void* DataPtr;
 int Length;
};

I do have a class similiar to this and i want to assign to DataPtr a pointer to some data allocated. When i do

DataStream::DataStream(int length)
{
char* arr = new char[length];
this->DataPtr = arr;
this->Length = length;
} 

All i get is corrupting the class (the length variable assume strange values) and the dataptr is not the same as the arr pointer. Why is that? What am i missing?

EDIT for information :

Windows Platform, Visual Studio 2010, The implementation is just that (done in the constructor). The including class is a simple EMPTY class with only a constructor that calls in return the constructor of the DataStream class. The class name is ShaderFormat. In the main app the only lines are

ShaderFormat* sf = new ShaderFormat();
DataStream* ds = sf->Save();

I was not directly referencing DataStream in the main app, only the shaderformat classes. (that was the problem it seems) Why is that?

+1  A: 

Could you try to shrink your example to a complete yet small one exhibiting the behavior? I see no reason that assignment would affect other members of the object .

vlabrecque
This should be a comment and not a reply.
Computer Guru
i've tried in an isolate environment and the problem does not appear.I've tried to strip down everything. the problem appear on one project, and not on the another. (i use the same code...and there is no additional executed code in either project)
feal87
the only difference i can see its that in one case the class is inside in a lib and in the second case its directly in the app...it can make a differnece?
feal87
+2  A: 

When managing dynamic resources like that in a class, remember the rule of three. You need:

  • copy constructor to make a copy (or increment a reference count) of the resource,
  • copy assignment operator - same,
  • destructor to release the resource if not referenced anymore.

Number of tools is available to ease this management:

  • Plain std::vector<char> as member of your class can probably solve your particular array problem - it manages memory for you and gives you control over length of the buffer, and it is fully copyable.
  • Privately inheriting from boost::noncopyable disables all copy semantics. This might be suitable for non-sharable resources.
  • Boost Smart Pointers library provides set of classes with scoped and reference-counted semantics for managing pointers and arrays.

Getting deeper into C++ development you will find that you work less with bold pointers and more with tiny wrapper classes and references.

Nikolai N Fetissov
Hey, drive-by downvoters, leave a comment at least :)
Nikolai N Fetissov
Seriously, the first step to solving problems is to get rid of the areas where bad things can happen. No reason to be using pointers like in OP's code. +1
GMan
+1 for anti-C-isms in C++.
rubenvb
A: 

what about casting arr to void* and when you need it casting it to char* ?

Giancarlo