Home > Enterprise >  using struct in sets
using struct in sets

Time:02-25

I am trying to make a set of sets, what is the correct to method to do that in C . What i am trying to achieve is something like this

One = { {"DDD", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64},{"JJ", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64},
{"kk", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64}, {"LL", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64} };

I tried like this

#include <set>
#include <iostream>
#include <algorithm>
#include <cstring>

struct Config
{
    const char* lbl;
    const char* desc;
    std::uint8_t se_2A;
    std::uint8_t se_2B;
    std::uint8_t se_2C;
    std::uint8_t se_2D;
    std::uint8_t se_2E;
    std::uint8_t su_2A;
    std::uint8_t su_2B;
    std::uint8_t su_2C;
    std::uint8_t su_2D;
    std::uint8_t su_2E;
    std::size_t total_size;
};

inline bool operator<(const Config& lhs, const Config& rhs)
{
    return lhs < rhs;
}


int main(){


    Config b1  = {"DDD", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
    Config b2  = {"JJ", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
    Config b3  = {"kk", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
    Config b4  = {"LL", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};

    std::set<Config> newConfig;
    std::set<std::set<Config>> One;

    newConfig.insert(b1);
    newConfig.insert(b2);

}

But gives me this error

Program received signal SIGSEGV, Segmentation fault.
0x00005555555555b9 in operator< (
    lhs=<error reading variable: Cannot access memory at address 0x7fffff7feff8>, rhs=<error reading variable: Cannot access memory at address 0x7fffff7feff0>)
    at main.cpp:32

Whats the correct to way to do this or fix the error?

CodePudding user response:

inline bool operator<(const Config& lhs, const Config& rhs)
{
    return lhs < rhs;
}

This operator< calls the operator< which calls the operator< which the operator< which the operator< which calls... can you spot the problem? The recursion is infinite and will eventually overflow the stack.

What you're probably intending to do is to compare the members of one side to the members of the other side. That said, it's easier to let the compiler do the work (C 20 or later required):

struct Config
{
    ... members ...
    friend auto operator<=>(const Config&, const Config&) = default;
};

Another issue that comparing pointers to strings isn't comparing the contents of the string. It might not cause problems in this trivial example, but "Numbers" in this translation unit doesn't necessarily have the same address as "Numbers" in another translation unit. And an automatic array such as char str[] = "Numbers"; definitely wouldn't have the same address.

To compare the strings by their content, you could use std::string_view instead:

struct Config
{
    std::string_view lbl;
    std::string_view desc;

CodePudding user response:

The problem with your code is that your function calls operator< function recursively infinitely.

To fix your issue, you'll have to replace your operator< function with the below code: (as said by @Aconcagua)

inline bool operator<(const Config& lhs, const Config& rhs)
{
    return lhs.lbl < rhs.lbl && 
        lhs.desc < rhs.desc && 
        lhs.se_2A < rhs.se_2A &&
        lhs.se_2B < rhs.se_2B &&
        lhs.se_2C < rhs.se_2C &&
        lhs.se_2D < rhs.se_2D &&
        lhs.se_2E < rhs.se_2E &&
        lhs.su_2A < rhs.su_2A &&
        lhs.su_2B < rhs.su_2B &&
        lhs.su_2C < rhs.su_2C &&
        lhs.su_2D < rhs.su_2D &&
        lhs.su_2E < rhs.su_2E &&
        lhs.total_size < rhs.total_size;
}
  • Related