mfc >> CString Problem !!!

by sanjeev_magoo » Fri, 28 Nov 2003 20:12:50 GMT

Hello Friends,

I am using char* everywhere in one of my application.
one of my friend suggested using CString class instead.
I searched for it in MSDN and find it a lot easier due to operators
attached with it.

But my problem is i can free a Char* anytime i want and i am sure it
will be free. but how do i know how CString is maintained internally.
What it's internal implementation is and when will it free the memory.

Any help will be appreciated.

TIA
Sanjeev Kumar.


mfc >> CString Problem !!!

by Mark S » Fri, 28 Nov 2003 20:39:26 GMT


Sanjeev,

The point of using CString over "char *" null terminated strings, is to free
you from such concerns of when and how the memory is allocated and
deallocated. This is not a problem with CString. It's a benefit!

If you allocate a CString on the stack (as a local variable, as a parameter
to a function call, or a return value), the memory will be freed when that
CString object goes out of scope.

If you create the CString on the heap with "new", then the memory for the
string will be deallocated when you "delete" the CString object.

If you have a CString that's going to live for a while, but you want to free
it's memory, you can always...

str.Empty();
str.FreeExtra();









mfc >> CString Problem !!!

by xmontyx » Sat, 29 Nov 2003 18:13:29 GMT

sanjeev
rest assured CString will not only free you from the pains of char *
but it wil also optimize your string usage and you will be using less
memory than if you were using char * :) .






CString Problem !!!

by Mihai N. » Sun, 30 Nov 2003 16:04:51 GMT

> rest assured CString will not only free you from the pains of char *
I doubth the part with "less memory". It is only too much work managing your
char* properly. Short: char* is better for the machine, CString is better for
you :)

But I don't want to argue here.
It is definitely worth moving to CString.

You get also some extras not mentioned before:
- solve many buffer problems (especialy when you don't know the size)
str.Format and str.FormatMessage are way better than sprintf.
You don't care about size, they can take a resource ID, so that you don't
use hard-coded messages, FormatMessage can take care of argument's order.
str.LoadString loads strings from resources and you don't care about size
- better internationalization (handling double byte characters),
just in case you need it
- it's easy to move to Unicode if the application is ANSI
(just recompile with CString, have to replace char with wchar_t otherwise)


--
Mihai
-------------------------
Replace _year_ with _ to get the real email


CString Problem !!!

by xmontyx » Mon, 01 Dec 2003 18:16:01 GMT

i agree that its too much work
but it will save you some memory as it keeps a reference count with in
CString str = "Blah Blah";
CString str1 = str;
there is actually only one copy of "Blah Blah" in memory

but in todays 128mb Ram World not much of a matter




Similar Threads

1. Migration to VC7: CString problem

2. Cstring problem VS.Net 2003

I am writing a MC++ wrapper for a native dll, that will be called by C# 
applications.

One of the functions returns a CString parameter: 

long GetProductStringDescriptor(int DevID, CString& sDescriptor) 

My wrapper accepts a ref to a managed string (below) and needs to pass this 
into to function so it is changed and returned. 

GetProductStringDescriptor(int DevID, String __gc* __gc* StringDesc) 

The problem I'm finding is that I get "Object not set to an insatance of an 
object" errors when I call the unmanaged function. I have tried passing a 
CString and String* into the function and converting it back to be returned. 
Both of these options cause the error.

I have tried all the MSDN solutions on mixed dll's, and this has not helped.

Any help would be gratefully received.

3. CString problem

4. unsigned char* to CString - problem

How to convert unsigned char* to CString:

I have some data and wrote some function to convertion data to CString
and dispay it:
e.g.
u_char_[0] =10;
u_char_[1] =100;
u_char_[2] ='A';
u_char_[3] ='\0';

Convert_to_CS(u_char_);

****
void Convert_to_CS (unsigned char* u_char){

CString str0;

str0.Format("%d",u_char[0]);
AfxMessageBox ("First:" +str0);

CString str(u_char);
AfxMessageBox ("Second:" +str);

}

***

Output:
First: 10  //good
Second:  // so nothing, why ??

Why working only first convetion ? I want convert whole array of uchar,
how to do it ?
When I  declare on input:
unsigned char u_char[] ="abcdef" then works good and display :
Second: abcdef
but I must put data individually for each byte...

5. UNICODE CString problem

6. CString problem

Chung, you might want to consider using the CStringList class instead
of developing your own linked list.  Or if you want more than just a
single CString in each item, use CPtrList (or CObList). For instance:

class CContentData {
public:
    CString  Title;
    CString  Content;
};

then in program:

  CPtrList  ContentList;

  CContentData	*p = new CContentData;

  p->Title = "Test";
  p->Content = "this is a test";
  ContentList.AddTail (p);

  ... add some more

then to extract:

  POSITION pos = ContentList.GetHeadPosition ();
  while (pos) {
    CContentData  &Ct = *(CContentData*) ContentList.GetNext (pos);
  }

or you can use GetTailPosition and GetPrev instead of GetNext.

I usually build my own list class to reduce the casting and
automatically clean up the items in the list, like this:

class CContentList : public CPtrList {
public:

  ~CContentList () { RemoveAll (); }

  CContentData*  GetNext (POSITION& p)
  { return (CContentData*) CPtrList::GetNext (pos);

  CContentData*  RemoveTail ()
  { return (CContentData*) CPtrList::RemoveTail (); }

  void  RemoveAll () { while (! IsEmpty ()) delete RemoveTail (); }
};

Then the program code reduces to:

to extract:

  POSITION pos = ContentList.GetHeadPosition ();
  while (pos) {
    CContentData  &Ct = *ContentList.GetNext (pos);
  }

This way the program code is cleaner and when you exit the routine (or
delete your CContentList class instance) the list is automatically
disposed.  And you can also add other useful functions to your list
class, such as Find, insert (in order), Sort, operator=, operator==,
etc.

Hope this was helpful.

Russ



On Tue, 22 Jul 2003 18:45:01 +0800, Chung < XXXX@XXXXX.COM >
wrote:

>It works perfects to me!
>thanks Scott.
>I'm used to write Ansi C rather visual C++.
>
>Scott McPhillips wrote:
>
>> Chung wrote:
>> 
>>>I want to make a link list of a struct included a CString member.
>>>in the .h file i declare:
>>>
>>>-----------------------------------------------------
>>>struct items{
>>>        struct items * next;
>>>        struct items * prev;
>>>        CString title;
>>>        CString content;
>>>};
>>>typedef struct items Items;
>>>-------------------------------------------------------
>>>
>>>and when i use it,
>>>i tried :
>>>Items *node;
>>>node = (Items*)malloc(sizeof(Items));
>>>node->content = (CString)"testing\n";
>>>
>>>however i got the following message:
>>>Assertion Failed: vixo: File afx.inl, Line 124
>>>
>>>also , i tried the followings but still error:
>>>1)
>>>CString temp = "testing\n";
>>>node->content = temp;
>>>2)
>>>CString temp("testing\n");
>>>node->content = temp;
>>>
>>>how can i fix the problem? thanks
>> 
>> 
>> When programming in C++ forget you ever heard of malloc.  Use 'new'.  It
>> properly initializes C++ objects by invoking their constructor and the
>> constructors of all embedded members.  Also the cast to (CString) is an
>> error. But alternatives 1 and 2 are OK, and even simpler is
>> node->content = "testing\n";
>> 
>> --
>> Scott McPhillips [VC++ MVP]
>

7. Weird CString problem

8. CString problem!!!

This is my code.
#include <iostream>
#include "atlstr.h"
using namespace std;
int main()
{
    char *orig = "Hello, World!";
    CString cstring(orig);
    cout << orig << endl;
    cout << cstring << endl;
}

My compiler is Visual Studio 2005.
The output is;
Hello, World!
001551A0

Why 001551A0 instead of Hello, World! ?

Anybody knows why. PLEASE tell me.