NO

Author Topic: Problem with wprintf(L"%s","very long string")  (Read 6582 times)

topin89

  • Guest
Problem with wprintf(L"%s","very long string")
« on: March 14, 2012, 08:29:47 AM »
This code crashes for no reason.

Code: [Select]
#include <wchar.h>
#include <stdio.h>

int main(void)
{
wprintf(L"%s","00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
}

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Problem with wprintf(L"%s","very long string")
« Reply #1 on: March 14, 2012, 08:48:34 AM »
It seems to be a problem with the runtime of Pelles C - but I have tried your code with gcc, MS Studio 2005 and Pelles C with msvcrt.lib, and there you don't get any output - so I assume the mix of long and short chars is not really defined.
The only runtime, which I found in a short time which works with your code as expected is Cygwin with gcc.

This code works with all compilers, which I tested: ;)
Code: [Select]
#include <wchar.h>
#include <stdio.h>

int main(void)
{
wprintf(L"%ls",L"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
}
« Last Edit: March 14, 2012, 09:05:12 AM by AlexN »
best regards
 Alex ;)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Problem with wprintf(L"%s","very long string")
« Reply #2 on: March 14, 2012, 01:26:42 PM »
If you use wide char routines the string have to be encoded as widechar. Otherwise there will be present only one null char and the print routine will continue to access memory up to find a short int (2 bytes) equal to zero or to reach the memory bound and crash the program.
Some compilers automatically convert strings to widechar inside w.... functions, but is not mandatory. So to be sure and compliant with all compilers is best to specify suffixing an L (like Alex made)
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: Problem with wprintf(L"%s","very long string")
« Reply #3 on: March 14, 2012, 02:05:15 PM »
So to be sure and compliant with all compilers is best to specify suffixing an L (like Alex made)

:D  Otherwise you get one L of a mess?


topin89

  • Guest
Re: Problem with wprintf(L"%s","very long string")
« Reply #4 on: March 14, 2012, 02:31:00 PM »
AlexN, frankie
Thanks. Good to know that the problem is not compiler-specific. It's funny that code below is working fine.
Code: [Select]
#include <wchar.h>
#include <stdio.h>

int main(void)
{
wprintf(L"%s","0000000000000000000000000000000000\n");
}

Original problem was in using both wprintf and printf in the code.
I tried my code, and wprintf behavior, that depends on argument length, make me think it's a bug.
Now i think that using both version of printf was not clever, so original problem exterminated.
Thanks again.

CommonTater

  • Guest
Re: Problem with wprintf(L"%s","very long string")
« Reply #5 on: March 14, 2012, 06:32:43 PM »
AlexN, frankie
Thanks. Good to know that the problem is not compiler-specific. It's funny that code below is working fine.
Code: [Select]
#include <wchar.h>
#include <stdio.h>

int main(void)
{
   wprintf(L"%s","0000000000000000000000000000000000\n");
}

Original problem was in using both wprintf and printf in the code.
I tried my code, and wprintf behavior, that depends on argument length, make me think it's a bug.
Now i think that using both version of printf was not clever, so original problem exterminated.
Thanks again.

I'm sorry but no your problem is not solved...  wprintf() wants to output unicode text (utf16le on Windows).  What you are doing is cheating by handing it unicode format specifier and specifying an ansi string.  It may work this time but you can't count on it working anywhere else... Some would call this "undefined behaviour"... I would call it dumb luck.

Also you cannot reliably mix ansi and unicode outputs in a windows console program.  This too is undefined behaviour in that you may find after starting with ansi, your unicode outputs don't show up at all (or vice versa).

Alex showed you the correct way.

If you want to learn some about Unicode here's a place to get started -> http://en.wikipedia.org/wiki/Unicode


Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Problem with wprintf(L"%s","very long string")
« Reply #6 on: March 14, 2012, 09:00:21 PM »
The use of char and widechars together in the same program is not wise anyway.
The fact that sometime using an ansi string in a widechar function works is a pure casualty. It basically depends on the presence of null bytes at the end of a string. Usually compilers aligns data on double word boundary (32 bits compilers), so if your string lenght is less than DWORD boundary the compiler will add nulls at the end of the string. Anyway the output is not your ansi string, but the character rappresented by each couple of input string.
To mix char and wchars require a deep knowledge of the compiler you are using and is absolutely not portable nor guarantee to run on a different system. In plain words "Never do it!"
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide