Re: How to Duplicate CString's Default Data Type

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I think this may have already been answered, but here's my two cents...

The parameter is copied by value because the parameter is a pointer and the pointer is
copied by value. If you create a complex structure, the entire structure will be copied
by value. This is why a CString is really just a wrapper for a pointer to a heap object.
The operations on CString basically manipulate that heap object. This is why you can pass
it to printf, because the pointer is not only a pointer to a heap object but looks like a
pointer to the actual object type expected as an (LPCTSTR). And that's the whole trick.
joe

On Sat, 11 Mar 2006 15:46:36 -0700, "Jonathan Wood" <jwood@xxxxxxxxxxxxxxxx> wrote:

Jolest,

I'm not saying you are wrong,

Well, could we say that it appears that you have serious doubts...? :)

Actually, I had no reason to think you are wrong and, in fact, have since
studied the CString source and have a better understanding of what it is
doing and was able to confirm what you said about only one data member.

How does the compiler know that it should not make a copy of the CString
object in Test2?

I think you're looking at the wrong side of the question. The compiler
only does what it's told to do. Therfore, the question is not "Why
didn't the compiler copy the CString in Test2?". (The answer to that
one is simple, it didn't copy it because it was never told to copy
it...)

Actually, that question didn't come out of thin air. I think I need to give
a little more background so you'll understand where I'm coming from. I wrote
a buffer template class that I was using with a non-Microsoft compiler. When
typed to TCHAR, the template included methods so that the object behaved
like a string. However, we found that when passed untyped as a second
argument to printf, the compiler made a copy of the entire object, passed it
to printf, and then called delete after printf returned.

So I think I have a pretty good reason to ask why the same doesn't happen
with CString, regardless of the number of data members it contains.

In Test2(..), the printf(..) function-template that the compiler saw
(probably in the MSVC header file AFX.H) before compiling your code
determined what kind of calling standard was going to be used in your
code when calling printf. The calling standard that manages pushing
"var-arg" (variable number of arguments) arguments for a function like
printf is an old standard (it pre-dates C++) that doesn't really have
anything to do with "object copying".

Please understand I am not new to programming--it may save you some typing.
I've been programming various languages (including C) for about 20 years. I
understand the hows and whys of the declaration for printf, and I know what
I've seen a compiler do when I tried passing my own object as one of the
untyped arguments.

If you change the definition for Helper(..) to "void Helper(CString&
s)", you'll see all that extra object-duplication go away.

Okay, we'd *really* save some typing if you took it that I had a bit more
knowledge about what we're talking about.

I understand how objects are passed by value. What I've been saying is
that's exactly what a compiler tried to do with my buffer class when passing
it untyped to printf. Now you're saying it doesn't pass the object, just the
data in the object. I guess I could run some more tests with the VC compiler
to see if it tries copy the entire object to printf like the other compiler
did. Because unless the VC compiler has a completely different behavior in
this regard, I still don't understand why it doesn't create a copy of the
CString object, regardless of the number and type of data members it
contains.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: CString to LPCTSTR
    ... Just pass CString object where LPCTSTR is expected. ... The CString class have operators which allows you to reference a CString variable with a const pointer to a character in this string buffer. ... Anyway, the compiler sees your function, and because the CString class ...
    (microsoft.public.vc.language)
  • Re: Restricted pointer parameters in printf()
    ... >is illegal if the string literals are coalesced into the same pointer? ... promise you, Mr Compiler, that I will not change the object at ... formatnever changes while printf() is doing its thing internally. ... If you were to write "double *restrict result", ...
    (comp.lang.c)
  • Re: How to Duplicate CStrings Default Data Type
    ... didn't the compiler copy the CString in Test2?". ... determined what kind of calling standard was going to be used in your ... it's the vtable pointer required by the virtual functions ...
    (microsoft.public.vc.mfc)
  • Re: How to Duplicate CStrings Default Data Type
    ... didn't the compiler copy the CString in Test2?". ... to printf, and then called delete after printf returned. ... regardless of the number and type of data members it ...
    (microsoft.public.vc.mfc)
  • Re: Pointer arithmetic
    ... int main ... How it makes it work is up to the compiler. ... I can understand why the first printf outputs 1996, ... implementation-defined pointer values. ...
    (comp.lang.c)