Comments (15)
Even without using this library, I would not expect to be able to have a function that recurses 100,000 times, unless I have a massively huge stack. If you optimize this, you might get away with it due to tail call optimization converting it from recursive to iterative.
from json.
If I remove the json argument, then it works as expected --- I can finish the entire execution. In fact, as long as I don't use json in the function, then it works. This means that something is happening when json is being used that causes this (this happens even if I'm just extracting a value from the json, like std::string a=name["glossary"]["title"];
.
from json.
You pass by value - this means you create thousands of copies of the json value. Can you try to pass by reference?
from json.
I already tried that --- same problem, just happens later.
from json.
I see the same behavior with
#include <string>
void print_json(std::string name,int n){
if (n<=0){
return;
}
printf("%s\n",name.c_str());
print_json(name, n-1);
}
int main(int argc, char** argv){
std::string testing = R"({"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}})";
print_json(testing,100000);
return 0;
}
As @gregmarr mentioned, you are testing against the stack limits, not against the library.
from json.
That's weird --- If I do that (and use references), the program completes just fine, it's just that references with json is not enough to complete the program.
from json.
By adding a local variable with a destructor, you are defeating the tail call optimization.
void print_json(std::string const &name,int n){
if (n<=0){
return;
}
std::string copy = name; // If you comment this out, it will succeed
print_json(name,n-1);
}
int main(int argc, char** argv){
std::string testing = R"({"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}})";
print_json(testing,100000);
return 0;
}
from json.
I understand that, but replacing json
reference with a std::string
reference causes the program to terminate (in both cases, there are no local variables).
from json.
However, using -O3
causes this reproducer to succeed. Unfortunately, my actual function is not tail recursive, so the compiler can't optimize for it.
from json.
The local variable is here, it's a temporary, but it does appear to be affecting the optimization:
printf("%s\n",name.dump(-1).c_str());
^^^^^^^^^^^^^
from json.
Ah, that's why std::string::c_str
works --- it's a pointer, which doesn't need to be deallocated.
Since my function is not tail-recursive, it doesn't seem to apply. However, in my function, I'm not dealing with 10000 iterations, but at most 15, so there's something else going on (I also get a bus error). Given that references alleviate some of the problem tells me that something probably went awry. But I don't think this is due to the library at this point, so I'll close this issue.
from json.
A problem though: when I debug applications, I need to get the json string so I can externally parse it --- however, as you can see, if I'm in heavily nested frame, and I try to dump()
a large json object, I get a stack overflow. Is there a way to get the raw char* directly, to avoid making a local variable?
from json.
There is no string form of a large object until you generate it using dump()
, and that form is not stored, only returned to you.
from json.
When I call test["a"]["b"]["c"]
on a json& test, are new objects created/copied for each of the keys? If so, that could explain the overflow.
from json.
test["a"] returns a reference, which you then access by calling ["b"], which returns a reference, which you then access by calling ["c"], which returns a reference. This reference is either const or non-const depending on the const-ness of test. If test is non-const, then it can create new sub-objects, but that's all heap-based. The references may or may not be stack objects, or may just be stored in registers, depending on the optimization, but if they are stack objects, then they are pointer/reference sized.
from json.
Related Issues (20)
- confused of paresr callback's behavior of null object: remove or discarded? HOT 1
- natvis not updated to 3.11.3 HOT 1
- Json parsed from raw string does not interpret lists of objects like json parsed from file HOT 2
- Cannot unflatten json object HOT 2
- [json.exception.parse_error.101] parse error at line 1, column 4520: syntax error while parsing value - invalid string: forbidden character after backslash; last read: '".\? HOT 2
- (official) example for parsing "custom data source" (implementing input iterator) does not compile HOT 1
- Unexpected conversion from 'true'/'false' to some integer types HOT 2
- CodeQL suppressions lack justification HOT 2
- Can't use nlohmann on Linux HOT 7
- Clone is long due to large history HOT 4
- iteration_proxy has limited usefulness in C++20 range views
- json.exception.type_error.302 HOT 3
- Error 302: type must be number, but is it number HOT 2
- nlohmann::ordered_json: not compatible with NLOHMANN_DEFINE_TYPE_INTRUSIVE
- when enum is used as key in a map the json format is not an expected format HOT 2
- JSON can't parse a simple data HOT 2
- JSON_BuildTests fail when JSON_DisableEnumSerialization is set to ON
- VS 2022 latest version on win 10 latest version don't compile any recent version of JSON... HOT 2
- Crashes when I try to use βjson::at()β on a properly structured, non null, and correctly constructed β.jsonβ file HOT 2
- Unable to parse JSON string from snake case to camel case HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json.