Appending char pointer after shallow copying - c++

I have made two object of string class each having char* pointer . By shallow copying, i have copied the first object into second object by shallow copying . Now both of them pointing at the same location.
What i have to do is append the char pointer through one object so that it does not make another but increase the size of original char pointer so second object point to the same location.
void String::append(char c) {
auto_ptr<StringBuffer> newdata(new StringBuffer);
newdata.get()->reserve(this->_str->length() + 1);
newdata.get()->smartCopy(this->_str);
this->_str = newdata.release();
this->_str->append(c);
}
The wrapper class of StringBuffer
void StringBuffer::reserve(int n) {
if (_length < n) {
int newlength = n; //max(_length*2,n);
char* newbuf = new char[newlength];
//copy contents of the stored string in the new buffer
revSmartCopy(newbuf);
//return stuff from the new buffer to the stored buffer
delete[] this->_strbuf;
this->_strbuf = newbuf;
this->_length = newlength;
newbuf = 0;
}
}
void StringBuffer::revSmartCopy(char* newString) {
int it = 0;
while (it < this->_length) {
newString[it] = this->_strbuf[it];
it++;
}
}
void StringBuffer::smartCopy(StringBuffer* newString) {
int shorterLength = 0;
(this->_length < newString->_length) ? shorterLength = this->_length : shorterLength = newString->_length;
int it = 0;
while (it < shorterLength) {
*_strbuf++ = *(newString->_strbuf)++;
it++;
}
}
This code is making another copying with object from whom we append pointing to new copy and older one pointing to previous

Let's assume you're doing this as an exercise, because it makes no sense otherwise.
You can't reallocate a pointer to a different size and have it at the same pointer value; this might happen accidentally, but it's impossible to enforce. Since the two objects are independent, the only way to make this work is double indirection - the pointer in your object points to a second pointer, which is the pointer to the character buffer.
You're also going to have a problem with destruction, because you have multiple objects with the same pointer. The standard library has std::shared_ptr to solve this very problem. If a pointer is shared between different objects, use shared_ptr to hold it.
Since there will only be one pointer to the actual character buffer, you can use std::unique_ptr for that one. You could use std::auto_ptr instead, and it will work fine as long as you don't try to copy it, but unique_ptr is a far better choice.

Related

Copying an array from a struct which is senе through a function

I have want to send a struct to json->setInformation but my program crashes when i try to copy the array which is inside the struct. The rest of the data is okay its just the array which makes the crash occur.
info = data->getInformation();
json->setInformation(info);
getInformation returns a struct which i can read in main.cpp
when i try to send this struct to setInformation it crashes...
information.h which holds my struct
struct information{
String location;
String protocol;
uint8_t groupID;
uint8_t* data;
information& operator=(const struct information& that){
location = that.location;
protocol = that.protocol;
groupID = that.groupID;
for (int i = 0; i < 9; ++i){
data[i] = that.data[i];
}
return *this;
}
};
json.cpp
void JSON::setInformation(information data){
info->location = data.location;
info->protocol = data.protocol;
info->groupID = data.groupID;
// for (int i = 0; i < 9; ++i){
// info->data[i] = data.data[i];
// }
// Serial.print("after JSON: ");
// Serial.println(info->data[0]);
}
this code works fine but when i uncomment the for lop which should copy the array it crashes
Did you allocate memory for your uint8_t data* parameter before using it ?
Then remember to deallocate memory when you don't need it anymore, thus avoiding memory leaks.
Your object is passed by copy to the function, but you have no copy constructor.
Default copy constructor will not copy you raw pointer correctly. So either you declare and implement a copy constructor, either you replace your raw pointer (uint8_t*) by a vector (std::vector<uint8_t>) which is safely copyiable (then copying the object will become a valid operation).
Moreover, we can't see who's allocating/deallocating your raw pointer, but I suspect you are missing a destructor function too.
Your code breaks the rule of three which is the minimal requirement for any class you'll declare in C++.

Heap corruption in deconstructor

I'm creating a program for decompiling some game script files. The latest part I've added is giving me some errors when dealing with dynamic arrays. This is the offending code:
typedef struct _COD9_ANIMREF_1
{
DWORD name;
DWORD reference;
};
typedef struct _COD9_USEANIM_1
{
WORD name; // offset of name
WORD numOfReferences; // reference count
WORD numOfAnimReferences; // reference count
WORD null1; // always null
DWORD* references = NULL; // dynamic array of references, amount = numOfReferences
_COD9_ANIMREF_1* animReferences = NULL; // dynamic array of references, amount = numOfAnimReferences
~_COD9_USEANIM_1()
{
if (references)
delete[] references;
if (animReferences) // program officially breaks here, if continued causes heap corruption
delete[] animReferences;
}
};
typedef struct _COD9_WORK_1
{
_COD9_GSC_1 Hdr;
char* data = NULL;
int* includes = NULL; //done
_COD9_USEANIM_1* usingAnim = NULL; //not done, heap corruption
_COD9_STRING_1* strings = NULL; //done
_COD9_FUNC_1* functions = NULL; //done
_COD9_EXTFUNC_1* extFunctions = NULL; //done
_COD9_RELOC_1* relocations = NULL; //done
~_COD9_WORK_1()
{
if (data)
delete[] data;
if (includes)
delete[] includes;
if (usingAnim)
delete[] usingAnim;
if (strings)
delete[] strings;
if (functions)
delete[] functions;
if (extFunctions)
delete[] extFunctions;
if (relocations)
delete[] relocations;
}
};
if (tstg.Hdr.numOfUsinganimtree)
{
tstg.usingAnim = new _COD9_USEANIM_1[tstg.Hdr.numOfUsinganimtree];
igsc.seekg(tstg.Hdr.usinganimtreeStructs);
for (int i = 0; i < tstg.Hdr.numOfUsinganimtree; i++)
{
_COD9_USEANIM_1 anim;
igsc.read(reinterpret_cast<char*>(&anim.name), sizeof(anim.name));
igsc.read(reinterpret_cast<char*>(&anim.numOfReferences), sizeof(anim.numOfReferences)); // this is 0 in this instance
igsc.read(reinterpret_cast<char*>(&anim.numOfAnimReferences), sizeof(anim.numOfAnimReferences));
igsc.read(reinterpret_cast<char*>(&anim.null1), sizeof(anim.null1));
anim.references = new DWORD[anim.numOfReferences]; // allocate 0 size array so theres something to delete
if(anim.numOfReferences) // should not be entered
{
igsc.read(reinterpret_cast<char*>(&anim.references), (anim.numOfReferences*sizeof(DWORD))); // if numOfReference = 0, function should return
}
anim.animReferences = new _COD9_ANIMREF_1[anim.numOfAnimReferences];
for (int ii = 0; ii < anim.numOfAnimReferences; ii++)
{
_COD9_ANIMREF_1 animref;
igsc.read(reinterpret_cast<char*>(&animref.name), sizeof(animref.name));
igsc.read(reinterpret_cast<char*>(&animref.reference), sizeof(animref.reference));
anim.animReferences[i] = animref;
}
tstg.usingAnim[i] = anim;
printf("anim: %d\n", i); // program reaches this
}
printf("Anims Done\n"); // program doesn't reach this
ReorderUsingAnim(&tstg);
}
Here is what is being read into the fields:
anim.name = 0x06BB
anim.numOfReferences = 0x0000
anim.numOfAnimReferences = 0x0001
anim.null1 = 0x0000
Where I think the error occurs is with the references array, since technically the size is 0 in this instance. But I'm not sure what to do about it, and I'm pretty lost in general about heap corruptions too.
_COD9_USEANIM_1 (why oh why newbies use such horrible names?? Is it enjoyable for them to call variables something like _Z_ASHD532___8AHQ_ ??) has two arrays (why not vectors??), references and anim_references. It has a destructor which frees the arrays if the pointers are not zero. But no constructor. This is DANGEROUS. You should, as a very least, provide a constructor which initializes them references and anim_references to zero. You also need the copy constructor. Remember the rule: if you provide one of the three (default constructor, destructor, copy constructor), you almost certainly need all three.
Ok, now you start your loop
for (int i = 0; i < tstg.Hdr.numOfUsinganimtree; i++)
In the loop you declare the variable anim
_COD9_USEANIM_1 anim;
You allocate its references and animReferences
anim.references = new DWORD[anim.numOfReferences];
...
anim.animReferences = new _COD9_ANIMREF_1[anim.numOfAnimReferences];
Finally you copy it to tstg.usingAnim
tstg.usingAnim[i] = anim;
You know what happens when you copy it? All fields are just copied. So now references and animReferences of tstg.usingAnim[i] point to the same address as references and animReferences of anim.
And then, the block ends. The evil computer calls the destructor for anim. The destructor calls delete[] for anim.references and anim.animReferences. But, references and animReferences of tstg.usingAnim[i] point to the same adresses. In other words, they now point to the array which were deleted.
Now the behaviour of your heap is unpredictable.
The best suggestion: forget arrays, and use vectors. You know, std::vector from the standard library.
Second best suggestion: provide default constructor and copy constructor. (PS: and assignment operator!)
(Note that you program may have other bugs too.)

Memory management when working with pointers in C++

I have these scenarios and I want to know if I manage my memory correctly. I watch the memory consumption in the Task Manager when I start the executable and see how memory is not popped back to the initial amount, which leads me to suspect that I don't clear memory where is needed.
So, in this first case I have a function that adds a new element to a dynamic array:
struct Color {
int R;
int G;
int B;
}
int TotalColors;
Color* Rainbow;
void AddColor(Color NewColor) {
// So, I create a new array of size TotalColors+1
Color* NewRainbow = new Color[TotalColors+1];
// Now I add the existing elements
for (int i=0; i<TotalColors; i++) {
NewRainbow[i] = Rainbow[i];
}
// And lastly, I add the new element
NewRainbow[TotalColors] = NewColor;
// Now, I assign the NewRainbow to Rainbow (I don't know if it's correct)
Rainbow = NewRainbow;
}
So, in this case, do you think I miss something? This is working but I want to make sure the unused stuff is removed from memory.
I also have a function to remove an element, which looks like this:
void RemoveColor(Color Removable) {
// Again, I create a new array of size TotalColors-1
Color* NewRainbow = new Color[TotalColors-1];
// I scan the list and add only those elements which are not 'Removable'
for (int i=0; i<TotalColors; i++) {
// Let's suppose that Removable exists in the list
if (Rainbow[i].R != Removable.R && Raibow[i].G != Removable.G && ... {
NewRainbow [i] = Rainbow[i];
}
}
// Again, the same operation as above
NewRainbow[TotalColors] = NewColor;
Rainbow = NewRainbow;
}
In this case, I don't know what happens with Rainbow[Removable], I mean, the element of the array that is removed.
And the last case, is this, where I try to send the pointer of an element from the array to a function.
Color* GetColor(int Index) {
Color* FoundColor;
// Scan the array
for (int i=0; i<TotalColors; i++) {
if (i == Index) FoundColor = &Rainbow[i];
}
return FoundColor;
}
// And I use it like this
void ChangeColor(int Index) {
Color* Changeable;
Changeable = GetColor(Index);
SetRGB(Changeable, 100, 100, 100);
}
// And this is what changes the value
void SetRGB(Color* OldRGB, int R, int G, int B) {
(*oldRGB).R = R;
(*oldRGB).G = G;
(*oldRGB).B = B;
}
And this is it. So, this works but I am not sure if with so many pointers I didn't forget to delete something. For example, when I RemoveColor I don't see the memory changed (maybe some bytes don't make the difference) and I just want some professional eye to tell me if I missed something. Thanks!
In the first function AddColor() you are not deleting the previously allocated memory.
Rainbow = NewRainbow; // leaking the memory Rainbow was previously pointing to.
Change that last line to:
delete[] Rainbow;
Rainbow = NewRainbow;
Same thing with RemoveColor()
Any time you use the new operator it needs to have a corresponding delete. Also, if you are allocating an array with new[] as in your case, it must have a corresponding delete[].
In order not to worry whether you've forgotten to delete a pointer, you shouldn't use plain pointers. Instead, use smart pointers such as
std::shared_ptr
std::unique_ptr
etc.
or, if you don't have C++11 yet, use
boost::shared_ptr
boost::scoped_ptr
More on smart pointers, see Wikipedia and the specific documentation.

How to avoid memory leaks in program with dynamical array containing pointers to objects?

I'm having trouble with memory usage in my homework program, which is used to store information about companies and its' owners. Class СompanyTemplate represents this info.
public:
CompanyTemplate (const string & oName,
const string & oAddr,
const string & cName,
const string & cAddr);
Another Class CCompanyIndex is used to store multiple records using a dynamical array of pointers (I'm not allowed to use vectors). Here's CCompanyIndex constructor:
CCompanyIndex :: CCompanyIndex (void)
{
allocated = 1000;
current_size = 0;
pole = new CompanyTemplate* [allocated];
for (int i=0; i<allocated; i++)
{
pole[i] = NULL;
}
}
CCompanyIndex also provides methods Add (add record), Del (delete record), Search(search an information about owner's company.
I'm having trouble with the Add method, although all basic tests are good, I have memory leaks, as valgrind says, in Add method.
bool CCompanyIndex :: Add( const string & oName,
const string & oAddr,
const string & cName,
const string & cAddr )
{
int pos = findPos(oName, oAddr);
if(pos != -1)
{
return false;
}
if ((current_size)>=allocated)
{
CompanyTemplate ** temp;
allocated = allocated*2+1;
temp = new CompanyTemplate* [allocated];
for (int i=0; i<current_size; i++)
{
temp[i]=pole[i];
}
pole = temp;
for (int i=0; i<current_size; i++ )
{
if ((pole[i])->Compare(oName,oAddr)<0)
{
current_size++;
for (int k=current_size-1; k>=i; k--)
{
pole[i] = new Comp pole[k+1]=pole[k];
}anyTemplate(oName, oAddr, cName,cAddr);
return true;
}
}
pole[current_size] = new CompanyTemplate(oName, oAddr, cName,cAddr);
current_size++;
return true;
}
Array elements reallocating works as expected, more likely I have an error in destructor, but still can't find.
Here it is:
CCompanyIndex :: ~CCompanyIndex (void)
{
for (int i=0; i<allocated; i++)
{
delete pole[i];
}
delete [] pole;
pole = NULL;
}
Thanks
if ownership is unclear, just use std::shared_ptr.
of course in a professional setting a better answer might be to analyze better and get a better idea of ownership, like, is it really shared?
but absent that, use a std::shared_ptr.
by the way, it looks like your class fails to handle copying properly. this is called the "rule of three" (or for c++11, the "rule of five"). essentially, if you define either of destructor, copy constructor or copy assignment operator, then you most likely need all three in order to deal properly with copying.
but the simplest is to not define such operations but instead use standard library containers, like std::vector, and standard library smart pointers, like std::shared_ptr.
e.g. instead of defining pole as a raw pointer (to array), define it as a std::vector.
With such a general title, the general answer will: use a vector of shared_ptr.
But I assumed that your homework is to implement a kind of std::vector<CompanyTemplate> using "low" level C++, witout STL and smart pointers (with otherwise is the best way of using c++). So:
Maybe you have other error, but here are two:
CompanyTemplate ** temp;
allocated = allocated*2+1;
temp = new CompanyTemplate* [allocated];
int i=0
for (; i<current_size; i++)
{
temp[i]=pole[i];
}
for (; i<allocated ; i++) // you want to make NULL the new pointers
{
temp[i]=NULL
}
delete [] pole; // delete old array.
pole=temp;

push_back to a Vector

I have a weird problem. I have a vector that I would like to push objects on to like so:
vector<DEMData>* dems = new vector<DEMData>();
DEMData* demData = new DEMData();
// Build DEMDATA
dems->push_back(*demData);
There will be a few hundred DEMData objects in the vector. The problem is when this code is finished, all items are equal to the last item "pushed back" to the vector?
Why are the other objects being overridden in the vector?
edit:
The DemData class is simple, just a data structure with setters and getters:
class DEMData
{
private:
int bitFldPos;
int bytFldPos;
const char* byteOrder;
const char* desS;
const char* engUnit;
const char* oTag;
const char* valType;
int index;
public:
void SetIndex(int index);
int GetIndex() const;
void SetValType(const char* valType);
const char* GetValType() const;
void SetOTag(const char* oTag);
const char* GetOTag() const;
void SetEngUnit(const char* engUnit);
const char* GetEngUnit() const;
void SetDesS(const char* desS);
const char* GetDesS() const;
void SetByteOrder(const char* byteOrder);
const char* GetByteOrder() const;
void SetBytFldPos(int bytFldPos);
int GetBytFldPos() const;
void SetBitFldPos(int bitFldPos);
int GetBitFldPos() const;
friend std::ostream &operator<<(std::ostream &stream, DEMData d);
};
EDIT:
I am reading an XML file and building DEMData objects based on the attributes of each xml element:
DEMData demData;
for (i = 0; attr[i]; i += 2)
{
if(strcmp(attr[i],"BitFldPos") == 0)
{
demData.SetBitFldPos(*attr[i + 1] - '0');
}
else if(strcmp(attr[i],"BytFldPos") == 0)
{
char* pEnd;
int tmp = strtol(attr[i + 1],&pEnd,10);
demData.SetBytFldPos(tmp);
}
else if(strcmp(attr[i],"ByteOrder") == 0)
{
demData.SetByteOrder(attr[i + 1]);
}
else if(strcmp(attr[i],"DesS") == 0)
{
demData.SetDesS(attr[i + 1]);
}
else if(strcmp(attr[i],"EngUnit") == 0)
{
demData.SetEngUnit(attr[i + 1]);
}
else if(strcmp(attr[i],"OTag") == 0)
{
demData.SetOTag(attr[i + 1]);
}
else if(strcmp(attr[i],"ValTyp") == 0)
{
demData.SetValType(attr[i + 1]);
}
else if(strcmp(attr[i],"idx") == 0)
{
char* pEnd;
int tmp = strtol(attr[i + 1],&pEnd,10);
demData.SetIndex(tmp);
}
//printf(" %s='%s'", attr[i], attr[i + 1]);
}
// Insert the data in the vector.
dems.push_back(demData);
Why are you allocating the vector with new? Why are you allocating your temporary DEMData object with new?
A vector stores a copy of what you pass to it, not that data itself, so unless you're deleting the DEMData object you allocated with new, you're leaking memory ever time you push an item onto the vector. Likewise, you're setting yourself up for memory leak problems by allocating the vector dynamically.
As to why the objects in the vector all seem to contain the same data, chances are pretty good that you have more of the same -- probably use of pointers combined with an incorrect copy ctor that ends up doing shallow copies a few places it shouldn't -- but since you haven't shown us that code, that's only a guess.
Edit: Now that you've added the code for your DEMData class, it looks very much like the guess above was correct -- you have pointers and no user-defined copy ctor, so you're getting a shallow copy.
As a first step, I'd get rid of all the pointer char members, and replace them with std::strings. The int members should be all right -- copying them will copy the values.
Edit2: Given what you're doing with these member variables, it looks a lot like you should consider using two std::maps -- one for the std::string variables, and one for the int variables.
Please note that your vector holds not references to the objects, but their copies. This means that after you store a newly-created instance of DEMData in your vector, and update the object, the corresponding entry in the vector won't be updated.
You need to make your vector store DEMData*, and push_back a pointer, not a value pointed to.
I suppose the strings in the objects are the same.
Presumably you use the same demData object to populate the vector.
And since you use default copy constructor all copies contain the same (char*) pointers.
Should you change the content of the memory referred by these pointers, all copies 'see' the changes.
Your DEMData class needs a copy constructor and destructor in order to manage the strings' memory. As it stands what (probably) is happening is that a DEMData object is created, inserted into the vector which creates a new instance of DEMData with the same pointer values. Then you destroy the original DEMData (since there is no destructor) which leaves the pointers in the vector dangling. Then when you create a new DEMData it gets the same memory locations (by chance) and in the end all the objects point at the same locations.
I'm not sure if in this case it's anything to do with the vector itself... The way you're using pointers to the vector and demData (instead of allocating them on the stack) looks a bit suspicious.
I'd expect normal C++ code to look like this:
vector<DEMData>* dems = new vector<DEMData>();
DEMData demData
// Build DEMDATA
dems->push_back(demData);
...
delete dems;
Can you paste the rest of your code? or perhaps the loop that does the demData building?