Home > OS >  Is it possible to get/set values from a pointer vector<json>?
Is it possible to get/set values from a pointer vector<json>?

Time:08-26

I am currently experimenting with C , basically I am trying to find the most repeated values in a very huge array using vectors and json. However to make my code more efficient I've decided to use threading, however my knowledge of pointers and addresses doesn't seem to work on this one.

Basically I am trying to do this :

#include <thread>
#include <iostream>
#include <vector>
#include "json.hpp"

void read(vector<json> * repeatitions, bool *done){
    // ... more code from here
    cout << "from thread:" << repeatitions->size() << endl; // this works
    cout << *repeatitions[0 % data_multiplier]["0"].is_null() << endl; // this doesn't

    *done = true; // - works obviously
}

int main(){
   // ... more code from here
   bool done = false;
   vector<json> repeatitions(10);
   thread worker(read, &repeatitions, &done);
   worker.join();

   return 0;
}

for some reason it gives me :

 error: no viable overloaded operator[] for type 'vector<json>' (aka 'vector<basic_json<>>')
    cout << repeatitions[0 % data_multiplier]["0"].is_null() << endl;

note: candidate function not viable: no known conversion from 'const char [2]' to 'std::vector<nlohmann::basic_json<>>::size_type' (aka 'unsigned long') for 1st argument
    _LIBCPP_INLINE_VISIBILITY reference       operator[](size_type __n) _NOEXCEPT;

note: candidate function not viable: no known conversion from 'const char [2]' to 'std::vector<nlohmann::basic_json<>>::size_type' (aka 'unsigned long') for 1st argument
    _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const _NOEXCEPT;

If this is not possible, I am planning to make it static however I am not very familiar with static variables I am very hesitant to do this as I want to make my code as efficiently as possible. I hope you can give some insight about this, thanks :)

**edit reproducible error:

#include <thread>
#include <iostream>
#include <vector>
#include "json.hpp"

using json = nlohmann::json;
using namespace std;

void read(vector<json> *repeatitions, bool *done){
    // ... more code from here
    cout << "from thread:" << repeatitions->size() << endl; // this works
    cout << repeatitions[0]["test"].is_null() << endl; // this doesn't
    cout << repeatitions[0]["test"] << endl; // this doesn't

    *done = true; // - works obviously
}

int main(){
   // ... more code from here
   bool done = false;
   vector<json> repeatitions(1);
   repeatitions[0]["test"] = "test";
   thread worker(read, &repeatitions, &done);
   worker.join();

   return 0;
}

CodePudding user response:

I suggest not using the name read (it may conflict with other reads - especially since you do using namespace std; in the global scope - so don't do that).

Also, pass by reference to the thread function. It's easier to deal with. You do that by packaging them in std::reference_wrappers (using std::ref).

Example:

#include "json.hpp"
#include <iostream>
#include <thread>
#include <vector>

using json = nlohmann::json;

void mread(std::vector<json>& repeatitions, bool& done) {
    using namespace std;
    // ... more code from here
    cout << "from thread:" << repeatitions.size() << endl; // this works
    cout << repeatitions[0]["test"].is_null() << endl;     // this now works
    cout << repeatitions[0]["test"] << endl;               // this now works

    done = true;
}

int main() {
    // ... more code from here
    bool done = false;
    std::vector<json> repeatitions(1);
    repeatitions[0]["test"] = "test";
    std::thread worker(mread, std::ref(repeatitions), std::ref(done));
    worker.join();

    return 0;
}

Output:

from thread:1
0
"test"

CodePudding user response:

Using a pointer causes you to get the address of something, it can either be used to access the value that resides inside that address, or to allocate an array, which if you are not using C is not recommended. Vector itself already acts like an array. So when you say std::vector<your_type>* you should be aiming to create either an array of arrays, or to reference a vector. In this case, you are trying to reference it. So learn and use references &.

Using *repeatitions[0 % data_multiplier]["0"] means that you are trying to access the '0 % data_multiplier'th element of a vector array, and in that element you try to get "0" indexed element (which must be a pointer to an object that has a method called is_null() ) and you are de-referencing it to get the value that the pointer is pointing to.

Also, since you are learning, instead of doing (*object).method() you can just do object->method() when de-referencing.

  • Related