I think my teacher was mostly telling us what are the wrong ways to allocate memory ...
But I wasn't totally sure what is the correct way to safely reallocate more memory to a pointer. Purpose of reallocating memory to my mind is because the old memory allocation is not large enough (eg when doing an prefix increment operator overload such as append character 'X', presumably you would need large enough new allocation to accomodate the endresult?)
QUESTIONS:
So I was left rather puzzled by what exactly is the correct way to reallocate memory to a char*c_string, because the old memoryallocation is not large enough anymore, and we need the larger memoryallocation instead (this happens with prefix increment operator overload)?
does my existing code have memoryleaks in the prefix increment operator overload?
how can I find memoryleaks when coding C++ in Visual Studio 2017 on Windows?
Basic information about my project: The goal was to create your own CustomString class, and have char*pointer as a private instance variable of the CustomString class.
I have created my own CustomString class already, and I was supposed to make some operator overloads, such as addition operator, prefix increment operator.
The main purpose with addition operator is of course naturally string concatenation. And with the prefix increment operator, you just add a character 'X' at the end.
I can post here below my definitions for my already existing code.
CustomString.h
#pragma once
#include <iostream>
#include <string.h>
using namespace std;
class CustomString
{
friend ostream &operator<<(ostream &out, const CustomString &reference);//OUTPUT OPERATOR OVERLOADED as friend function
public:
static const int MaxSize = 10;//classwide constant
static int getNumberOfCustomStrings();
CustomString(const char *namepointer="");// constructor
//CustomString(char * pointer);//constructor w/parameters
~CustomString();//destructor
CustomString(const CustomString &stringref); //copy constructor w/dynamic datamember
operator const char*() const; // IMPLICIT CONVERSION operator overloaded
CustomString operator+( const CustomString &addedpart) const; // PLUS OPERATOR OVERLOADED implemented as string concatenate
const CustomString& operator=(const CustomString &ref); //ASSIGNMENT OPERATOR OVERLOADED works for dynamic strings as well
const CustomString &operator++();//PREINCREMENT OPERATOR OVERLOADED
CustomString operator++(int);//POSTINCREMENT OPERATOR OVERLOADEd
char& operator[](int i) const;// INDEXING OPERATOR OVERLOADED
private:
char * c_string;
void list();
static int numberOfCustomStrings;
};
From CustomString.cpp, here is the prefix increment piece of code
const CustomString & CustomString::operator++()// PREFIX INCREMENT OVERLOADED, adds an 'X' character into the dynamic instance variable(char*c_string)
{
if ( strlen(c_string) == (MaxSize-1) ) {
cout << "error cannot increment any further" << endl;
return *this;
}
else {
int oldlength = strlen(c_string) +1;
char*newdata;
newdata = new char[ oldlength+1 ]; //it has to be one char bigger allocation bevause one "X" soon will be added
strcpy_s(newdata, oldlength+1, c_string);//put the old data into new allocation as the baseword
newdata[oldlength-1] = 'X';
newdata[oldlength] = '\0';
delete c_string;//free the old memory
c_string = newdata; //give the new allocation to the dynamic instance variable
return *this;
}
}
I believe that I already did destructor, constructor and copy-constructor correctly, but I can also give their code
Constructor from CustomString.cpp
CustomString::CustomString(const char *pointer)// constructor
{
if( (strlen(pointer)+1) <=MaxSize ){
c_string = new char[strlen(pointer) + 1];
strcpy_s(c_string, (strlen(pointer) + 1), pointer);
numberOfCustomStrings++;
}
else {
c_string = new char[MaxSize];
//printf("char at last index was == %c \n", c_string[MaxSize-1]);
int j = 0;
for (j = 0; j <= (MaxSize-2); j++) {
c_string[j] = pointer[j];
}
//printf("the last truechar was %c \n",pointer[MaxSize-2]);
c_string[MaxSize - 1] = '\0';
//printf("the last char was %c\n", pointer[MaxSize-1]);
numberOfCustomStrings++;
}
}
Here is copy-constructor from CustomString.cpp
CustomString::CustomString(const CustomString &ref) //copy-constructor w/dynamic data member.
{
c_string = new char[strlen(ref.c_string) + 1];
strcpy_s(c_string, strlen(ref.c_string) + 1, ref.c_string);
numberOfCustomStrings++;
}
Here is destructor from CustomString.cpp
CustomString::~CustomString() // destructor
{
delete c_string;
cout << "destructor called!" << endl;
numberOfCustomStrings--;
}
Code:
char *a = new char;
delete a;
char *array = new char[100];
delete [] array;
You can use visual leak detector to Locate a line
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.