How can I check if a MFC CString is null?
Asked Answered
P

2

5

I want to check if a MFC CString is null or not. Is there a way to do this?

PS: I want to check if it is null not if it's empty.

CString m_strName;

enter image description here

Presumptuous answered 24/11, 2015 at 9:53 Comment(15)
MFC CString cannot be nullErasion
A pointer can be null but an object cannot be null. If you have a pointer just write if (mypointer == NULL) ..., but that is rather obvious.Gretna
Sounds like an XY problem.Sage
What are you trying to achieve ?Gretna
@Ben if the internal ptr is null the CString is empty.Erasion
We don't know what tokres is. We also don't know, what sort of visualizers you have installed. As posted, the question doesn't contain enough information to answer it.Donley
I am trying to copy the tokres.m_strName which is CString into m_strName which is also a CString. This calls the overloading of the operator= of CString (CSimpleStringT& operator=(In const CSimpleStringT& strSrc)) and crashes because it cannot get the value of the string. This is why i want to check if tokres.m_strName is NULLPresumptuous
tokres is an object of a class containing CString m_strName; data member.Presumptuous
It seems that your tokres is not properly initialized. That's why all class members are pretty much garbage. You should post entire code. So we can help you out.Sapp
Are you sure that tokres.m_strName is a CString object, and not a pointer to a CString object (i.e. a CString*)? The Watch window seems to imply, that it is a pointer.Donley
@Donley yes, it may even be a simple pointer to char.Diego
@Presumptuous what exactly is tokres?Gretna
Since you didn't respond to any of the inquiries to clarify your question, I'm afraid I have to vote to close it off as unclear.Donley
I have already written that tokres is an object of a class containing CString m_strName; data member. From how it looks like it might be a problem of initialization. Is there a way to check if the string is initialized.Presumptuous
Don't tell us what you think tokres is. Post the real code. Everything else is just asking us to guess, and my guess is, that you are doing it wrong. A CString class member will always be initialized, regardless of how the object is constructed. A CString* as a class member may or may not be initialized. You also need to answer what debug visualizers you have installed.Donley
S
11

A CString object is never NULL. Unlike a char* or wchar*, which can be NULL, the internal buffer of a CString object which is a pointer always points to a data. For a given CString object, you can only differentiate whether it is empty or not using CString::IsEmpty().

For the same reason, the LPCTSTR cast operator never returns NULL.

Sapp answered 24/11, 2015 at 10:12 Comment(3)
ok, but in visual studio in debug the value of the cstring is null. I cannot check it with IsEmpty (it crashes because it cannot retrieve the length).Presumptuous
show your code snippet. it is only possible if your object has been destroyed or it is not initialized.Sapp
I added a picture. It has not been initialized.Presumptuous
D
2

Due to the internal layout of the CString class template1), the pointer stored cannot ever be NULL.

The CString class template has a single class member: m_pszData. This member not only contains the string data, but also additional information (like string length, reference count, buffer capacity, etc.; see CStringData). This additional information is stored to the left of the stored pointer. Both parts (string data and character buffer) must be allocated in a single block of memory, as there is only one pointer to reference both. Since the string data always needs to be there, the m_pszData can never be NULL.


1) CString is a typedef for a particular CStringT template instantiation. The CStringT itself is derived from the CSimpleStringT class template.
Donley answered 24/11, 2015 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.