Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C++) Chilkat Functions Returning a StringThis example explains Chilkat functions in Unicode C++ that directly return a string. These functions begin with a lowercase letter. If a string is returned directly by a Chilkat method (in Unicode C++), it is a pointer into internal memory managed by Chilkat. It is not guaranteed to stay in memory forever. In fact, there are 10 possible internal buffers to hold the strings returned by a lowercase function. After the 10th lowercase function is called, the string returned by the least recent function call is no longer valid. The intent of returning the string directly is for convenience. It is for cases where the application will immediately use the string, or for cases where the application immediately copies the string to an object or buffer under its own management. This example demonstrates the 10 internal buffer limit.
#include <CkJsonObjectW.h> void ChilkatSample(void) { // This example demonstrates the 10 internal buffer limit for functions returning strings. // We'll demonstrate using the JSON object. CkJsonObjectW json; // First, build a JSON document. json.UpdateString(L"x1",L"1"); json.UpdateString(L"x2",L"2"); json.UpdateString(L"x3",L"3"); json.UpdateString(L"x4",L"4"); json.UpdateString(L"x5",L"5"); json.UpdateString(L"x6",L"6"); json.UpdateString(L"x7",L"7"); json.UpdateString(L"x8",L"8"); json.UpdateString(L"x9",L"9"); json.UpdateString(L"x10",L"10"); json.UpdateString(L"x11",L"11"); json.UpdateString(L"x12",L"12"); // Call 12 functions in a row that directly return strings. // This is an error because there are a max of 10 internal buffers per Chilkat object // to hold the returned strings. // The calls to get "x11" and "x12" overwrite the internal buffers used by x1 and x2. const wchar_t *x1 = json.stringOf(L"x1"); wprintf(L"x1: %s\n",x1); const wchar_t *x2 = json.stringOf(L"x2"); wprintf(L"x2: %s\n",x2); const wchar_t *x3 = json.stringOf(L"x3"); wprintf(L"x3: %s\n",x3); const wchar_t *x4 = json.stringOf(L"x4"); wprintf(L"x4: %s\n",x4); const wchar_t *x5 = json.stringOf(L"x5"); wprintf(L"x5: %s\n",x5); const wchar_t *x6 = json.stringOf(L"x6"); wprintf(L"x6: %s\n",x6); const wchar_t *x7 = json.stringOf(L"x7"); wprintf(L"x7: %s\n",x7); const wchar_t *x8 = json.stringOf(L"x8"); wprintf(L"x8: %s\n",x8); const wchar_t *x9 = json.stringOf(L"x9"); wprintf(L"x9: %s\n",x9); const wchar_t *x10 = json.stringOf(L"x10"); wprintf(L"x10: %s\n",x10); // This call invalidates the memory pointed to by x1. const wchar_t *x11 = json.stringOf(L"x11"); wprintf(L"x11: %s\n",x11); // This call invalidates the memory pointed to by x2. const wchar_t *x12 = json.stringOf(L"x12"); wprintf(L"x12: %s\n",x12); wprintf(L"---\n"); // If we try to get x1 and x2 again, // we can see how the buffers were overwritten. // In fact, x1 and x2 may point to already-deleted memory, // and accessing the memory at these pointers may cause a crash. wprintf(L"x1: %s\n",x1); wprintf(L"x2: %s\n",x2); wprintf(L"x3: %s\n",x3); wprintf(L"x4: %s\n",x4); wprintf(L"x5: %s\n",x5); wprintf(L"x6: %s\n",x6); wprintf(L"x7: %s\n",x7); wprintf(L"x8: %s\n",x8); wprintf(L"x9: %s\n",x9); wprintf(L"x10: %s\n",x10); wprintf(L"x11: %s\n",x11); wprintf(L"x12: %s\n",x12); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.