简体   繁体   中英

Members of wchar_t string array missing

I tried to pass the following array of strings to a function:

const wchar_t *testTokens[] = { L"function",
                                L"(",
                                L"x",
                                L")",
                                L"{",
                                L"var",
                                L"test"
                                L"=",
                                L"\"",
                                L"this"
                                L"is",
                                L"a",
                                L"string",
                                L"\"",
                                L";",
                                L"}" };

The function in question looks something like this:

processArray(const wchar_t **strings, unsigned int numStrings);

Since the array defined above has exactly 16 entries, I set numStrings to be the same. However the program crashed unexpectedly due to a segfault inside of the processArray function.

Naturaly, I investiated what was causing the error using Visual Studios built in debugger and I found that some of the elements inside of testTokes where "fused together" right after its definition:

-       testTokens,16   0x00effba0 {0x00660f88 L"function", 0x00660b70 L"(", 0x00660c04 L"x", 0x00660c0c L")", 0x00660cec L"{", ...}    const wchar_t *[16]
+       [0]     0x00660f88 L"function"              const wchar_t *
+       [1]     0x00660b70 L"("                     const wchar_t *
+       [2]     0x00660c04 L"x"                     const wchar_t *
+       [3]     0x00660c0c L")"                     const wchar_t *
+       [4]     0x00660cec L"{"                     const wchar_t *
+       [5]     0x00660fb0 L"var"                   const wchar_t *
+       [6]     0x00660fbc L"test="                 const wchar_t *
+       [7]     0x00660cf4 L"\""                    const wchar_t *
+       [8]     0x00660cfc L"thisis"                const wchar_t *
+       [9]     0x00660d10 L"a"                     const wchar_t *
+       [10]    0x00660fe4 L"string"                const wchar_t *
+       [11]    0x00660cf4 L"\""                    const wchar_t *
+       [12]    0x00660d5c L";"                     const wchar_t *
+       [13]    0x00660d64 L"}"                     const wchar_t *
+       [14]    0xcccccccc <Error reading String.>  const wchar_t *
+       [15]    0x7f4af182 <Error reading String.>  const wchar_t *

I then tried to manually add null terminators to each string:

const wchar_t *testTokens[] = { L"function\0",
                                L"(\0",
                                L"x\0",
                                L")\0",
                                L"{\0",
                                L"var\0",
                                L"test\0"
                                L"=\0",
                                L"\"\0",
                                L"this\0"
                                L"is\0",
                                L"a\0",
                                L"string\0",
                                L"\"\0",
                                L";\0",
                                L"}\0" };

Which resulted in the following memory layout:

-       testTokens,16   0x0055fe30 {0x006a0f88 L"function", 0x006a0b70 L"(", 0x006a0c04 L"x", 0x006a0c0c L")", 0x006a0cec L"{", ...}    const wchar_t *[16]
+       [0]     0x006a0f88 L"function"              const wchar_t *
+       [1]     0x006a0b70 L"("                     const wchar_t *
+       [2]     0x006a0c04 L"x"                     const wchar_t *
+       [3]     0x006a0c0c L")"                     const wchar_t *
+       [4]     0x006a0cec L"{"                     const wchar_t *
+       [5]     0x006a0fb0 L"var"                   const wchar_t *
+       [6]     0x006a0fbc L"test"                  const wchar_t *
+       [7]     0x006a0cf4 L"\""                    const wchar_t *
+       [8]     0x006a0cfc L"this"                  const wchar_t *
+       [9]     0x006a0d10 L"a"                     const wchar_t *
+       [10]    0x006a0fe4 L"string"                const wchar_t *
+       [11]    0x006a0cf4 L"\""                    const wchar_t *
+       [12]    0x006a0d5c L";"                     const wchar_t *
+       [13]    0x006a0d64 L"}"                     const wchar_t *
+       [14]    0xcccccccc <Error reading String.>  const wchar_t *
+       [15]    0x5d1ee42e <Error reading String.>  const wchar_t *

As you can clearly see the strings that were fused together before are now missing entirely, and I am completely clueless as to why.

You forgot a comma after L"test" , and after L"this" . Therefore, the compiler treats the adjacent string literals "test" and "=" as a single string, and the adjacent string literals "this" and "is" as a single string. You can see this in your debugger output. Thus, the array actually has 14 elements, not 16.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM