String too long or io error

string too long or io error

_CrtDbgReport: String too long or IO Error. my TRACE is:TRACE(_T("fdsafdsafdsa%s\n"),"ly");. but the string isn't too long. How can I make TRACE work. How do I resolve the "CharacterStringTooLong (Value is too long) encountered with {Value}" error that I received when I tried to create a TXT. After debugging the problem, I found out that the URL generated by that page can be too long. This post is an explanation of the error and how.

Something is: String too long or io error

String too long or io error
Syntax error else is unexpected as 3
LIFESTYLE ERROR 802 CALL BOSE
String too long or io error

You can watch a thematic video

ATERNOS sprers.eurException: sprers.euutOfBoundsException: Index: 25, Size: 5

String too long or io error - think

 there is a _CrtDbgReport Error when changing the Character Set  
AuthorMessage
ly4587





PostPosted: Visual C++ General, there is a _CrtDbgReport Error when changing the Character Set Top

i get a new application without using Unicode Character Set, and later, i chang the Character Set from Use Multi-Byte Character Set to Use Unicode Character Set. build is ok, but when i debug the application, TRACE don't work, and a error arise:

_CrtDbgReport: String too long or IO Error.

my TRACE is :TRACE(_T("fdsafdsafdsa%s\n"),"ly");

but the string isn't too long . How can I make TRACE work



Visual C++4  
 
 
Viorel.





PostPosted: Visual C++ General, there is a _CrtDbgReport Error when changing the Character Set Top

I would recommend this:

TRACE(_T("%s\n"), _T("ly"));

You can also try this:

TRACE(_T("%hs\n"), "ly");

I hope it works.


 
 
ly4587





PostPosted: Visual C++ General, there is a _CrtDbgReport Error when changing the Character Set Top

i try them

they work.but when i put Chinese characters into the first string parameter of

 

for example:

 

_CrtDbgReport: String too long or IO Error

 


 
 
Viorel.





PostPosted: Visual C++ General, there is a _CrtDbgReport Error when changing the Character Set Top

It seems the macro does not support all of characters. You can try this instead:

::OutputDebugString(_T("中文\n"));

If it still does not display characters correctly, then I am afraid you will not be able to show your text in Output window of Visual Studio.


 
 
 

((String)constValue).length() < Pool.MAX_STRING_LENGTH)
return;
log.error(pos, "limit.string");
nerrs++;
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// langtools-b9abf5c3d057\src\share\classes\com\sun\tools\javac\jvm\Pool.java
// com.sun.tools.javac.jvm.Pool

/** An internal structure that corresponds to the constant pool of a classfile.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
publicclassPool{

...

publicstaticfinalint MAX_STRING_LENGTH = 0xFFFF;

...
}
1
2
3
4
5
6
7
8
// src/share/classes/com/sun/tools/javac/resources/compiler.properties

...

compiler.err.limit.string=\
constant string too long

...

通过上边代码可以看到 而 是十进制的 (但是实际上只写了 65534 个就报错了,不知道为什么差一个?),这里应当就是字符串字面量的限制的来源。这些代码整体怎么运作的,我并没有仔细看,不过按照提示信息能对的上来看,因该就是在这些逻辑里处理的字符串字面量长度的限制,也就是说字符串字面量长度是在编译器 代码逻辑层面加的限制。

再看第二篇文章 Java字符串的最大长度 文章的意思是,这个限制是在 文件格式层面限制的:

String内部是以char数组的形式存储,数组的长度是int类型,那么String允许的最大长度就是Integer.MAX_VALUE了。又由于java中的字符是以16位存储的,因此大概需要4GB的内存才能存储最大长度的字符串。不过这仅仅是对字符串变量而言,如果是字符串字面量(string literals),如“abc”、”1a2b”之类写在代码中的字符串literals,那么允许的最大长度取决于字符串在常量池中的存储大小,也就是字符串在class格式文件中的存储格式:

1
2
3
4
5
6
> CONSTANT_Utf8_info {
> u1 tag;
> u2 length;
> u1 bytes[length];
> }
>

u2是无符号的16位整数,因此理论上允许的string literal的最大长度是2^16-1=65535。然而实际测试表明,允许的最大长度仅为65534,超过就编译错误了,有兴趣可以写段代码试试,估计是length还不能为0。

再看第三篇文章Java “constant string too long” compile error. Only happens using Ant, not when using Eclipse:这个是篇爆栈网的主题,题主大致问的是用Ant编译超长字符串字面量会爆出和我遇到的同样的问题,而用eclipse编译则没问题,我自己试了试,用eclipse编译,确实没有了 这个限制:

1
2
3
4
5
6
7
8
9
publicclassTest{

publicstaticvoidmain(String[] args){
String veryLargeLengthStringLiteral = ;
System.out.println(veryLargeLengthStringLiteral.length());
}
}

// 在Eclipse中能正常编译并输出 65549 这个代码换到 idea 里编译会直接报错

那这个又是怎么回事呢?其实这个我倒是能知道个大概,因为我以前就知道,Eclipse编译Java代码并不用 (可参考Which Java compiler is used by Eclipse? )而是用的 JDT 也就是他们自己写的一个编译器,Eclipse能正常编译超过 的字面量,那只能说明他们没在自己的编译器里做长度限制。

那么,第二篇文章的观点岂不是和第三篇有些相悖了?要是在 文件格式层面就做了限制,那么理论上Eclipse编译出来的应该完全跑不起来或者完全就不应该编译成功啊?这里我也不是很明白,留着以后知识盲区被修补了再继续了解吧,反正我现在更倾向于这个限制只是在编译器实现层面加的。

更新与勘误

2019-08-15 更新

后来我在知乎上关于这个主题进行了提问:Java中字符串字面量的65534限制和CONSTANT_Utf8_info的length有关系吗?

  • 之所以JDT能编译过,只是因为JDT优化为了StringBuilder的append
  • 之所以限制是 65534 而不是 65535 只是因为 Javac 源码中做的限制是 注意是 而不是 , 小于65535那自然最多只能是65534了(搞明白这个得感谢“杭州-半拍”的帮助)