Reputation: 55
I'm not positive this is even possible, but I am basically trying to define a struct with an array of the same struct inside of it, like so
struct Node {
int numMatches = 0;
Node* leaves[26] = {};
};
Each node would contain a fixed-length array of pointers to other nodes (representing letters). I'm trying to recurse through these nodes, going into the relevant leaf whenever I need. I would initialize a "head" Node* array and bubble down that way. It's seg faulting immediately, and I can see why that might be - it doesn't know how much memory to alloc to an array of such structs. Not quite sure how to solve the problem though.
Pretty simple issue, but I haven't found any C++/C specific threads with this same question.
Thanks!
Upvotes: 0
Views: 133
Reputation: 14201
In your struct
you didn't use an array of the same struct
, but an array of the pointers to the same struct
- and it makes the difference.
Pointers
have known length (e. g. 32-bit in 32-bit operating systems) regardless of the object they point to, so your struct simply allocates (probably)
mumMatches
26 * 32 bits = 26 * 4 bytes = 104 bytes for leaves
108 bytes altogether
Upvotes: 1