-1

I ran into a little problem programming something, I've looked around but I didn't seem to find out the answer.

I'll spare you useless code.

Here are the declarations:

struct one {
    std::string string1, string2;
    bool boolean;
};

struct two {
    std::string string3, string4;
    bool boolean;
};

void function(uint first_parameter, one **first, two **second);

And here is what the main looks like:

int main()
{
    one *passes;
    two *users;

    //...

    passes = new one[size_one]();
    users = new two[size_two]();

    //Filling the arrays...

    std::thread t[PARTS];

    for (int start = 0; start < PARTS; start++)
        t[start] = std::thread(function, first_parameter, &passes, &users);
    for (int i = 0; i < PARTS; i++)
        t[i].join();
}

Whenever I try to access an element of one of my structures (allocated on the free store) in my thread function, (I typically would access it like so: (*first)[0].string1[0]) I do not get the string1 I normally can access in main. Aren't the std::strings located in the free store?

O Void
  • 1
  • 1
  • _"Aren't the std::strings located in the free store?"_ Hard to tell from the example code you're giving here. – πάντα ῥεῖ Aug 03 '18 at 20:01
  • 1
    The code you have shown looks okay. can you show a [mcve] that doesn't do what you expect and show us what you expect it to do. Also, if you are using strings and threads why aren't you using vectors and references instead of all the pointers? – NathanOliver Aug 03 '18 at 20:03
  • try `(*first[0]).string1[0]` or `first[0]->string1[0]` – kerrytazi Aug 03 '18 at 20:04
  • I am new to c++ so I'm not sure how strings work. I've mainly studied C since now and am used to c strings. I would normally allocate strings with something like a malloc call, do I need to use the new keyword on every string of my structure array to be able to use them in the thread function? – O Void Aug 03 '18 at 20:05
  • 1
    Consider using a `std::vector`. – Galik Aug 03 '18 at 20:05
  • I'm starting to think I'm not getting any strings because I haven't allocated them on the heap or "free store" as you c++ devs like to call. @kerrytazi I've tried what you asked and it stills shows me (null) when I debug with printf... – O Void Aug 03 '18 at 20:10
  • `.string1[0]` is a `char`, is that what you are expecting? – Galik Aug 03 '18 at 20:10
  • @Galik What's the difference here, are vectors allocated differently ? To my little knowledge they're pretty much the same. Or are you talking about using vectors of the structure and then pass it as reference? Also I know .string1[0] would be a char, it was just for the sake of the example :) – O Void Aug 03 '18 at 20:11
  • Vectors do all the allocation you did but they do it for you. You can either pass the vectors by reference or pass their internal arrays using `myvector.data()`. – Galik Aug 03 '18 at 20:12
  • But passing the vector by reference is preferable because it keeps the size information and other good things vector gives tou. – Galik Aug 03 '18 at 20:13
  • @Galik just remember that in `std::thread` invocation context you need to use `std::ref`, or vector will be passed by value. – SergeyA Aug 03 '18 at 20:14
  • The main thing we're missing right now, trying to figure out what might have gone wrong, is how you're accessing your strings inside `function` and how you're accessing them in `main`. A good [mcve] contains enough code that plausibly, we could copy it over to, say, [OnlineGDB.com](https://www.onlinegdb.com/), and successfully compile/run it (or reproduce the same error message you're getting, if you're getting a compile error), and try to observe the results you're getting. – Xirema Aug 03 '18 at 20:17
  • Yes at least post the code you want to use with your data so we can see how it is not working for you. – Galik Aug 03 '18 at 20:19
  • I think the advice given by other commentators here to use references and `std::vector` are good, general purpose C++ advice, but I think they're distracting from the OP's actual issue. Try to limit those kinds of comments to afterthoughts, i.e. "Here's how you fix your issue, and HERE, using `std::vector`/whatever, is how you make this issue less likely to occur/easier to diagnose in the future". – Xirema Aug 03 '18 at 20:20
  • Does `(*first)[0].string1 = "hello string";` not work for you? – Galik Aug 03 '18 at 20:22
  • If `first` really is of type `one**` then I don't see how `*first[0].string1[0]` can compile successfully. Check [operator precedence](https://en.cppreference.com/w/cpp/language/operator_precedence) (as hinted at by @kerrytazi). – G.M. Aug 03 '18 at 20:22
  • @G.M.It was a typo. – O Void Aug 03 '18 at 20:28

1 Answers1

0

check your function to have valid types of parameters. this example bellow works nice.

#include <iostream>


typedef struct _one
{
    std::string first_str;
    std::string second_str;
    bool check;
} one;


void do_something(one** passes)
{
    one* original = *passes;

    one first = original[0];

    std::cout << first.first_str; // -> hello

    // the same
    std::cout << (*passes)[0].first_str;
}


int main()
{
    one* passes = nullptr;

    passes = new one[10];

    passes[0].first_str = "hello";

    do_something(&passes);
}
kerrytazi
  • 583
  • 1
  • 4
  • 15
  • Unrelated: `typedef struct _one { ... } one;` can be `struct one { ... };` in C++. No `typdef`ing required to allow `one x;`. – user4581301 Aug 03 '18 at 21:16
  • 1
    @user4581301 implicit typedef prevents some bugs with hiddiing of struct names: https://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c/612476#612476 – kerrytazi Aug 03 '18 at 21:37
  • I was not aware of that. Probably never run across it because I tend to do a lot of `grep`ing and the like for usages of identifiers and avoid repeating an identifier like the plague. – user4581301 Aug 03 '18 at 21:52
  • One thing to watch out for with the naming scheme though, `_one` is an reserved identifier in the global namespace. Good rundown on that in [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Aug 03 '18 at 21:55