Re: Basic question : Replacing CString when moving from MFC to Win32

From: KS (ks_at_blah.com)
Date: 02/19/04


Date: Thu, 19 Feb 2004 13:14:07 -0700

Yes, you'll have to allocate the memory because LPTSTR is a pointer. If you
don't want to allocate memory and let the application do it for you, do
this:

TCHAR m_szFirstName[41];

So if you know the first name will never exceed 40 characters, then this
will work great. It has to be 41 because the m_szFirstName will be a null
character '\0'. However, the only drawback is that if the name is "Kevin",
it will still allocate all 41 and you're wasting 36 characters.

KS

"John Sway" <john@aol.com> wrote in message
news:euLxJQy9DHA.1804@TK2MSFTNGP12.phx.gbl...
> My other (related) question is: if I have a LPTSTR member in a Win32
class,
> do I need to allocate it's memory manually?
>
> For example:
>
> If my class is :
>
> class CPerson
> {
> public:
> LPTSTR m_szFirstName;
> }
>
> do I have to "initiate" or allocate any memory into m_szFirstName in the
> class contructor?
>
> I am sorry for such a basic question, but I have always used CString
> variables and have NO idea how to deal with strings directly.
>
> Thanks!
>
>
>
>
>
> "John Sway" <john@aol.com> wrote in message
> news:ekOLbFy9DHA.2804@TK2MSFTNGP09.phx.gbl...
> > We are porting one of our MFC apps to Pocket PC and Smartphone devices.
> Due
> > to disk and memory constraints, we cannot use MFC for these platforms.
> >
> > So I have to convert our MFC-based objects into plain Win32 classes. My
> > question is what string type do you use for replacing MFC's CString? For
> > example, if my MFC class is :
> >
> > class CPerson : public CObject
> > {
> > CString m_strFirstName;
> > CString m_strLastName;
> > }
> >
> > How would I write this in Win32? I realize, the CObject will have to go.
> >
> > Regarding the strings, will they be defined as LPCTSTR or LPTSTR? Does
it
> > make any difference? Also, is there any "base" class like CObject in
Win32
> > that will help when serializing our data?
> >
> > Thanks!
> >
> >
>
>



Relevant Pages

  • Re: CStrings and memory
    ... Then you allocate a few more strings ... Now you need to allocate another string, but there is no more memory. ... In addition to strings, there are other things that get allocated: ... have perhaps confused "address space" with "working set". ...
    (microsoft.public.vc.mfc)
  • Re: CStrings and memory
    ... Then you allocate a few more strings ... Now you need to allocate another string, but there is no more memory. ... What you need to measure are the working set size before the strings are allocated (which ...
    (microsoft.public.vc.mfc)
  • Tru64 issues with Infinite limits
    ... An automated test in my nightly build was failing due to Out of Memory ... Cannot allocate block 146 ...
    (comp.unix.tru64)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (comp.lang.cpp)