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
(C++) Chilkat Functions Returning a StringThis example explains Chilkat functions in C++ that directly return a string. These functions begin with a lowercase letter. If a string is returned directly by a Chilkat method (in 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 <CkJsonObject.h> void ChilkatSample(void) { // This example demonstrates the 10 internal buffer limit for functions returning strings. // We'll demonstrate using the JSON object. CkJsonObject json; // First, build a JSON document. json.UpdateString("x1","1"); json.UpdateString("x2","2"); json.UpdateString("x3","3"); json.UpdateString("x4","4"); json.UpdateString("x5","5"); json.UpdateString("x6","6"); json.UpdateString("x7","7"); json.UpdateString("x8","8"); json.UpdateString("x9","9"); json.UpdateString("x10","10"); json.UpdateString("x11","11"); json.UpdateString("x12","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 char *x1 = json.stringOf("x1"); std::cout << "x1: " << x1 << "\r\n"; const char *x2 = json.stringOf("x2"); std::cout << "x2: " << x2 << "\r\n"; const char *x3 = json.stringOf("x3"); std::cout << "x3: " << x3 << "\r\n"; const char *x4 = json.stringOf("x4"); std::cout << "x4: " << x4 << "\r\n"; const char *x5 = json.stringOf("x5"); std::cout << "x5: " << x5 << "\r\n"; const char *x6 = json.stringOf("x6"); std::cout << "x6: " << x6 << "\r\n"; const char *x7 = json.stringOf("x7"); std::cout << "x7: " << x7 << "\r\n"; const char *x8 = json.stringOf("x8"); std::cout << "x8: " << x8 << "\r\n"; const char *x9 = json.stringOf("x9"); std::cout << "x9: " << x9 << "\r\n"; const char *x10 = json.stringOf("x10"); std::cout << "x10: " << x10 << "\r\n"; // This call invalidates the memory pointed to by x1. const char *x11 = json.stringOf("x11"); std::cout << "x11: " << x11 << "\r\n"; // This call invalidates the memory pointed to by x2. const char *x12 = json.stringOf("x12"); std::cout << "x12: " << x12 << "\r\n"; std::cout << "---" << "\r\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. std::cout << "x1: " << x1 << "\r\n"; std::cout << "x2: " << x2 << "\r\n"; std::cout << "x3: " << x3 << "\r\n"; std::cout << "x4: " << x4 << "\r\n"; std::cout << "x5: " << x5 << "\r\n"; std::cout << "x6: " << x6 << "\r\n"; std::cout << "x7: " << x7 << "\r\n"; std::cout << "x8: " << x8 << "\r\n"; std::cout << "x9: " << x9 << "\r\n"; std::cout << "x10: " << x10 << "\r\n"; std::cout << "x11: " << x11 << "\r\n"; std::cout << "x12: " << x12 << "\r\n"; } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.