Problems with Object String Length

Write your question here.
Why in some cases len is calculated using

1. len = strlen(str) + 1;

2. len = strlen(obj.p) + 1;

3. len = obj.len;

4. len = obj.len + 1;

For example 1, I think it is because a literal string is the parameter for the constructor, and hence, the plus 1 is for the '\0' to make it a character string.

For example 2, I don't know the reason for the plus 1. The object being copied should already have the '\0' at the end.

For example 3, I see this as being correct as the existing object should have the '\0' at the end.

For example 4, I don't know the reason for the plus 1.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  int main()
{
	CBadClass obj("Test it!");
	obj.showIt();

	CBadClass obj1("Hello World!");
	obj1.showIt();

	//	Use Assignment Operator
	obj = obj1;
	obj.showIt();

	//	Use Copy Constructor
	obj = "Second test it!";
	CBadClass obj2(obj);
	obj2.showIt();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//	CBadClass.h
#pragma once

#include <cstring>										// For strlen() and strcpy
#include <iostream>

class CBadClass
{
private:
	char* p{};
	int len{};

public:
	//	Constructor
	CBadClass(const char* str = nullptr)
	{
		if (str)
		{
			len = strlen(str) + 1;
			p = new char[len];
			strcpy_s(p, len, str);
		}
	}

	//	Copy Constructor
	CBadClass(const CBadClass& aBad)
	{
		if (aBad.p)
		{
			len = strlen(aBad.p) + 1;
			p = new char[len];
			strcpy_s(p, len, aBad.p);
		}
	}

	//	Move Copy Constructor
	CBadClass(CBadClass&& aBad)
	{
		len = aBad.len;
		p = aBad.p;
		aBad.p = nullptr;
	}

	//	Assignment operator
	CBadClass& operator=(const CBadClass& aBad)
	{
		if (this != &aBad)
		{
			delete p;
			p = nullptr;
			len = 0;
			if (aBad.p)
			{
				len = aBad.len + 1;
				p = new char[len];
				strcpy_s(p, len, aBad.p);
			}
		}
		return *this;
	}

	//	Move Assignment Operator
	CBadClass& operator=(CBadClass&& aBad)
	{
		delete p;
		len = aBad.len;
		p = aBad.p;
		aBad.p = nullptr;
		return *this;
	}

	//	Function to show member data
	void showIt() const
	{
		std::cout << "len = " << len << std::endl
			<< "p: " << p << std::endl;
	}

	~CBadClass()
	{
		delete[] p;
	}
};
Why not use <string> and string.size()?
For c-string there are two distinct lengths:
1) length of string itself. It can be calculated by calling a strlen()
2) Length of memory block required to store string. This is always 1 more than string length to accomodate for trailing 0.

In your code copy assigment operator is incorrect: len member is not a length of string, but size of memory block, and you do not need to add 1 to it.
MiiNiPaa,

Thank you.

So, for the Assignment operator, len should be obj.len; and not obj.len + 1;

Similarly, for the Copy constructor, len should also be obj.len; and not strlen(str) + 1; since the object being copied has a len member, the size of the memory block.

Are the above correct?
Similarly, for the Copy constructor, len should also be obj.len; and not strlen(str) + 1;
Both are fine. len is the length of needed memory block which is equal to strlen(str) + 1;
Topic archived. No new replies allowed.