Help with string object

Why does the empty() from a string object return false, despite it being empty. The string is actually from an object pointer. Debugging shows that the pointer does not have any data, at the string object. So why does string return false, if there is clearly no string values there.

Especially, at the if statement, looking at the string c++ documentation, the expression obj->name.empty() is suppose to return true due to its lack of data, at that location in memory.
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
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::cin;
using std::string;

struct Object
{

	string name;
	int number;
};

int main()
{

	int number = 0;

	cout << "Enter number: ";
    cin >> number;
	
	Object * obj = nullptr;       
	Object regObj;
	
	cout << "obj address : " << obj << endl;
	if(number == 1)
	{
		obj = new Object;       
		obj->name = "John Smith";   
		obj->number = number;
		cout << " if statement obj address : " << obj << endl;
		regObj.name = "John Smoth";
	
		delete obj;
	}
	cout << " out of if obj address : " << obj << endl;	



	cout << "  " << endl;
	cout << "  " << endl;
	
	string s;
	if(obj->name.empty())
		cout << "Object does not point to data " << endl;
	else
		cout << "Object points to data " << endl;

	cout << "After delete obj address: " << obj << endl;
	cout << "  " << endl;
	cout << "  " << endl;


	system("Pause");
	return 0;

}


Thanks in advance
Last edited on
Hi,

That looks like undefined behaviour (UB) to me, you deleted obj , then tried to do obj->name.empty()

Hope that helps :+)

This is too complicated, obj maybe is null or is deleted.
As TheIdeasMan says, you are using a pointer to an object that has been deleted.

You can avoid this in future buy always setting a pointer to "nullptr" when you delete it.

1
2
 delete pObj;
pObj = nullptr;
Hi,



Becareful with pointers. When you delete a object and forget to set nullptr for him, you have this behaviour that you report.

 
delete obj;

The variable still have a valid pointer.

Try this:
1
2
delete obj;
obj = nullptr;


and this

 
if(obj && obj->name.empty())



Regards.
Hi,

I guess if you want to learn about using new & delete, that is fine.

However, really you should avoid them & their associated problems altogether and investigate smart pointers instead.

Here is one of them:

http://www.cplusplus.com/reference/memory/unique_ptr/


The other thing is to prefer the use of references instead of pointers when passing by reference.

Good Luck !!
Thanks guys I think it is undefined behavior.

Emanuel Moraes

I did what you recommended, but the function still returns false.


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
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::cin;
using std::string;

struct Object
{

	string name;
	int number;
};

int main()
{

	int number = 0;

	cout << "Enter number: ";
    cin >> number;
	
	Object * obj = nullptr;       
	Object regObj;
	
	cout << "obj address : " << obj << endl;
	if(number == 1)
	{
		obj = new Object;           
		obj->name = "John Smith";   
		obj->number = number;
		cout << " if statement obj address : " << obj << endl;
		regObj.name = "John Smoth";

		delete obj;           
		obj = nullptr;        <---
	}
	cout << " out of if obj address : " << obj << endl;	

	cout << "  " << endl;
	cout << "  " << endl;
	
	string s;
	if(obj && obj->name.empty())        <---
		cout << "Object does not point to data " << endl;
	else
		cout << "Object points to data " << endl;
	cout << "  " << endl;
	cout << "  " << endl;
	cout << "  " << endl;
	cout << "  " << endl;
	cout << "After delete obj address: " << obj << endl;
	cout << "  " << endl;
	cout << "  " << endl;




	system("Pause");
	return 0;

}


I know its because the pointer has been deleted, but does the program (specifically in this case empty()) count the junk values that are present at that location as real values(in this case as strings).

Ops, sorry :-)



Use this:

if(!obj && obj->name.empty())



Regards
Topic archived. No new replies allowed.