[TRAFODION-2772] - retrieve a value from Json string got an error #1439
Conversation
Check Test Started: https://jenkins.esgyn.com/job/Check-PR-master/2416/ |
Test Passed. https://jenkins.esgyn.com/job/Check-PR-master/2416/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider using stack variables instead of heap variables.
core/sql/exp/exp_function.cpp
Outdated
char *rltStr = NULL; | ||
JsonReturnType ret = json_extract_path_text(&rltStr, op_data[1], 1, op_data[2]); | ||
char *jsonStr = new(heap) char[len1+1]; | ||
char *jsonAttr = new(heap) char[len2+1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we need to delete jsonStr and jsonAttr after the json_extract_path_text call to avoid unnecessary heap pressure. Though if json_extract_path_text itself does new's on the same heap, we'd get heap fragmentation.
Another approach would be to allocate these on the stack instead, avoiding both concerns:
char jsonStr[len1+1];
char jsonAttr[len2+1];
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the value of len1 can only be determined when executing the query, we cannot define jsonStr as an Array.
Will release the memory manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we can! C++ allows dynamically-sized arrays on the stack. Try it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an example. Compile this program:
// Demonstrate dynamically-sized arrays on C++ stack
#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
for (size_t i = 5; i < 10; i++)
{
char temp[i+1];
cout << "sizeof(temp) = " << sizeof(temp) << endl;
}
return 0;
}
When you run it, you'll get this output:
[birdsall@edev08 dynChar]$ ./dynChar.exe
sizeof(temp) = 6
sizeof(temp) = 7
sizeof(temp) = 8
sizeof(temp) = 9
sizeof(temp) = 10
[birdsall@edev08 dynChar]$
…lease memory to heap
New Check Test Started: https://jenkins.esgyn.com/job/Check-PR-master/2421/ |
Test Passed. https://jenkins.esgyn.com/job/Check-PR-master/2421/ |
…ing array to replace dynamic memory
New Check Test Started: https://jenkins.esgyn.com/job/Check-PR-master/2425/ |
Test Passed. https://jenkins.esgyn.com/job/Check-PR-master/2425/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 Looks good. Thanks for considering my suggestions.
No description provided.