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 <C_CkJsonObject.h> void ChilkatSample(void) { HCkJsonObject json; const char *x1; const char *x2; const char *x3; const char *x4; const char *x5; const char *x6; const char *x7; const char *x8; const char *x9; const char *x10; const char *x11; const char *x12; // This example demonstrates the 10 internal buffer limit for functions returning strings. // We'll demonstrate using the JSON object. json = CkJsonObject_Create(); // First, build a JSON document. CkJsonObject_UpdateString(json,"x1","1"); CkJsonObject_UpdateString(json,"x2","2"); CkJsonObject_UpdateString(json,"x3","3"); CkJsonObject_UpdateString(json,"x4","4"); CkJsonObject_UpdateString(json,"x5","5"); CkJsonObject_UpdateString(json,"x6","6"); CkJsonObject_UpdateString(json,"x7","7"); CkJsonObject_UpdateString(json,"x8","8"); CkJsonObject_UpdateString(json,"x9","9"); CkJsonObject_UpdateString(json,"x10","10"); CkJsonObject_UpdateString(json,"x11","11"); CkJsonObject_UpdateString(json,"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. x1 = CkJsonObject_stringOf(json,"x1"); printf("x1: %s\n",x1); x2 = CkJsonObject_stringOf(json,"x2"); printf("x2: %s\n",x2); x3 = CkJsonObject_stringOf(json,"x3"); printf("x3: %s\n",x3); x4 = CkJsonObject_stringOf(json,"x4"); printf("x4: %s\n",x4); x5 = CkJsonObject_stringOf(json,"x5"); printf("x5: %s\n",x5); x6 = CkJsonObject_stringOf(json,"x6"); printf("x6: %s\n",x6); x7 = CkJsonObject_stringOf(json,"x7"); printf("x7: %s\n",x7); x8 = CkJsonObject_stringOf(json,"x8"); printf("x8: %s\n",x8); x9 = CkJsonObject_stringOf(json,"x9"); printf("x9: %s\n",x9); x10 = CkJsonObject_stringOf(json,"x10"); printf("x10: %s\n",x10); // This call invalidates the memory pointed to by x1. x11 = CkJsonObject_stringOf(json,"x11"); printf("x11: %s\n",x11); // This call invalidates the memory pointed to by x2. x12 = CkJsonObject_stringOf(json,"x12"); printf("x12: %s\n",x12); printf("---\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. printf("x1: %s\n",x1); printf("x2: %s\n",x2); printf("x3: %s\n",x3); printf("x4: %s\n",x4); printf("x5: %s\n",x5); printf("x6: %s\n",x6); printf("x7: %s\n",x7); printf("x8: %s\n",x8); printf("x9: %s\n",x9); printf("x10: %s\n",x10); printf("x11: %s\n",x11); printf("x12: %s\n",x12); CkJsonObject_Dispose(json); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.