Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / General / June 2005

Tip: Looking for answers? Try searching our database.

Mixed Mode Printing Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Balboos - 08 Jun 2005 04:07 GMT
Hi, all:

I'm constucting some formatted strings with many parameters, and have
been mixing old style C (i.e., printf() family) with managed code
(String *).  The project is a C++.NET console project, and the large
strings are T-SQL insert statements.

In Debug mode, everything is fine.  For the release build, however, I
have an interesting effect, shown in the following simplified snippet.

...
String *a;
a = "Hello";

printf("%s\n", a);
printf("%s\n", a);
printf("%s\n", a);
printf("%s\n", a);

...

The following output is observed:
Debug Build:
Hello
Hello
Hello
Hello  (as expected)

Release Build:
Hello
≡∙°≈≤
≡∙°≈≤
≡∙°≈≤  (the the latter an emailable representaion of jibberish)

This jibberish creation occurs, even if the first printf() call doesn't
even access the String*, such as:

String *a;
a = "Hello";

printf("%s\n", "Howdy");
printf("%s\n", a);

Output the following in Release mode:
Howdy
≡∙°≈≤

One last note:
Int32 types are not trashed, even witin the same printf(), and, through
various versions of testing, it is clear that the String's aren't
corrupted, and printf() is only reacting badly to the String's, and
then, only after the first usage.

This leaves me with (at least) two questions:

1)  What's wrong with the Release Build viz-a-viz the Debug Build, and
how, if at all, can it be fixed.
2)  I'd like to stay in 'pure' .NET, but build a string up using
String::Format() is rediculously tedious.  My supposition is that, in
order to remain compatable w/C#, the variable arg list is gone.

The why's and wherefore's are most desireable, but other approaches
would be appreciated.

Thanks, in advance,

Balboos
recoil@community.nospam - 08 Jun 2005 14:38 GMT
C#.NET has numerous formatting methods for creating Strings.
The printf statements that you posted could easily be replaced with
StringBuidler which is TONS of times faster then string concatenation.

--------------------------- Code Snippet ---------------------------

StringBuilder        TmpBuilder    = new StringBuilder();
TmpBuilder.AppendFormat("The {0} has had {1} {2} on {3}",
                        "cat",
                        3,
                        "kittens" ,
                        DateTime.Now.Subtract(new TimeSpan(3, 0, 0, 0,
0)).ToShortDateString()
                        );
TmpBuilder.Append(Environment.NewLine);
TmpBuilder.AppendFormat("The {0} at offset 0x{1:X} contained a value of
0x{2:X} at {3}",
                        "memory",
                        0x403202,
                        0x10202 ,
                        DateTime.Now.Subtract(new TimeSpan(3, 0, 0, 0,
0)).ToShortDateString()
                        );
TmpBuilder.Append(Environment.NewLine);
Console.Write(TmpBuilder.ToString());
Console.Read();

--------------------------- Output ---------------------------
The cat has had 3 kittens on 6/5/2005
The memory at offset 0x403202 contained a value of 0x10202 at 6/5/2005

--------------------------- Related Links ---------------------------

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemtextstringbuilderclassappendformattopic.asp


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp
conformattingoverview.asp


Hope this helps shed some light on your problem.
Balboos - 12 Jun 2005 12:57 GMT
Thank you for your reply.  I was suddenly called away and just got back
to my Internet access.

Your reply doesn't really help, as all of the overloads limit my input
list to, at most, 3 items (unless it is an array).  In this sense,
there's no clear value added for StringBuilder compared to String.

I'm building SQL INSERT statements that contain anywhere from 8 to 25
values.  To create the full final string, I'd have to construct it of
many components, and then assemble them.  Creating the array, of mixed
types (by representing them as string with/without apostrophes) could be
done, and then using String::Join() to assemble them is feasible, but
would take a good deal of coding (and, in actuality, the simultaneous
building of two such string: field names and values.

Standard (unmanaged) C/C++ allows for variable argument lists, which, in
order to maintain computability with C#.NET (which doesn't), are not
part of C++.NET.  Thus my mixing of managed and unmanaged code.  So, the
simplest solution was sprintf().

The weirdness is that it works fine in the DEBUG version, but fails in
the release version.  But even then, only after sprint() is used in that
particular method.

For now, I went to an SQL script, and that to create a stored procedure
to 'do the deeds' - but this behavior: works in debug, not in release,
is rather troubling.

All the above notwithstanding, I appreciate your reply.

Balboos

> C#.NET has numerous formatting methods for creating Strings.
> The printf statements that you posted could easily be replaced with
[quoted text clipped - 34 lines]
>
> Hope this helps shed some light on your problem.
recoil@community.nospam - 08 Jun 2005 14:39 GMT
Btw, the code sample I posted is in C#  but converting to Managed C++
or any other .NET language should be trivial if you are familiar with
MC++ or whatever .NET language you want to convet it to.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.