pointers; fix heap corruption
This commit is contained in:
42
pr4.cpp
42
pr4.cpp
@@ -16,15 +16,15 @@ public:
|
|||||||
capacity = Capacity;
|
capacity = Capacity;
|
||||||
p = new char[capacity];
|
p = new char[capacity];
|
||||||
len = 0;
|
len = 0;
|
||||||
|
p[0] = '\0'; // Initialize with an empty string
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit BaseString(const char* ptr) //create object from input string
|
explicit BaseString(const char* ptr) //create object from input string
|
||||||
// mark Single-argument constructors explicit to avoid unintentional implicit conversions
|
// mark Single-argument constructors explicit to avoid unintentional implicit conversions
|
||||||
{
|
{
|
||||||
cout << "\nBase Constructor 1\n";
|
cout << "\nBase Constructor 1\n";
|
||||||
len = Length(ptr);
|
len = Length(ptr);
|
||||||
capacity = len; // allocate needed memory dynamically based on the Length of the input.
|
capacity = len + 1; // allocate needed memory dynamically based on the Length of the input including null terminator
|
||||||
//avoid unnecessary memory allocation.
|
|
||||||
p = new char[capacity];
|
p = new char[capacity];
|
||||||
CopyString(p, ptr, len);
|
CopyString(p, ptr, len);
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ public:
|
|||||||
{
|
{
|
||||||
*destination++ = *source++;
|
*destination++ = *source++;
|
||||||
}
|
}
|
||||||
*destination = '\0';
|
*destination = '\0'; // Ensure the destination is null-terminated
|
||||||
}
|
}
|
||||||
|
|
||||||
int Length() const { return len; }
|
int Length() const { return len; }
|
||||||
@@ -76,20 +76,22 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
BaseString(const BaseString& s) // copy constructor
|
BaseString(const BaseString& s) // copy constructor
|
||||||
// using const can avoid unnecessary copying of objects
|
// using const can avoid unnecessary copying of objects
|
||||||
{
|
{
|
||||||
cout << "\nBase Copy Constructor\n";
|
cout << "\nBase Copy Constructor\n";
|
||||||
len = s.Length();
|
len = s.Length();
|
||||||
p = new char[s.capacity];
|
|
||||||
capacity = s.capacity;
|
capacity = s.capacity;
|
||||||
|
p = new char[capacity];
|
||||||
CopyString(p, s.p, len);
|
CopyString(p, s.p, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void print() const
|
virtual void print() const
|
||||||
{
|
{
|
||||||
for (int i = 0; i < len; i++)
|
const char* ptr = p;
|
||||||
|
while (*ptr != '\0')
|
||||||
{
|
{
|
||||||
cout << p[i];
|
cout << *ptr;
|
||||||
|
ptr++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +120,7 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
DerivedString(const DerivedString& s) : BaseString(s) // using const can avoid unnecessary copying of objects
|
DerivedString(const DerivedString& s) : BaseString(s) // using const can avoid unnecessary copying of objects
|
||||||
{
|
{
|
||||||
cout << "\nDerived Copy Constructor\n";
|
cout << "\nDerived Copy Constructor\n";
|
||||||
}
|
}
|
||||||
@@ -132,24 +134,26 @@ public:
|
|||||||
int LastIndexOf(const char* substr) const
|
int LastIndexOf(const char* substr) const
|
||||||
{
|
{
|
||||||
int substrLen = BaseString::Length(substr);
|
int substrLen = BaseString::Length(substr);
|
||||||
int startPos = len; // Start searching from the end of the string
|
if (substrLen > len) return -1; // substr longer than the string
|
||||||
while (startPos >= 0)
|
|
||||||
|
const char* start = p + len - substrLen; // Start from the last possible position
|
||||||
|
while (start >= p)
|
||||||
{
|
{
|
||||||
int i = 0;
|
const char* current = start;
|
||||||
while (i < substrLen && p[startPos + i] == substr[i])
|
const char* sub = substr;
|
||||||
|
while (*sub != '\0' && *current == *sub)
|
||||||
{
|
{
|
||||||
i++;
|
current++;
|
||||||
|
sub++;
|
||||||
}
|
}
|
||||||
if (i == substrLen) // If the entire substring matches
|
if (*sub == '\0') // If the entire substring matches
|
||||||
{
|
{
|
||||||
return startPos;
|
return start - p;
|
||||||
}
|
}
|
||||||
startPos--; // Move to the previous position
|
start--;
|
||||||
}
|
}
|
||||||
return -1; // Not found
|
return -1; // Not found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user