1

so, I have been given a header file that contains typedefs for structs

I have to define said structs in a source file and I am unable to modify the header

What kind of restrictions does this impose upon any of these structs I create?

In my header file I have

typedef struct tldnode TLDNode;

In my source file I have

struct tldnode
{
    int count;
    char *tld;
    TLDNode *left;
    TLDNode *right;
};

I get a segfault when running my program, and using GDB I have found at the point of the fault I am uinable to print the value of any of the TLDNode members because I cannot access the memory locations

Is this related to the way the struct is defined, like I mention at the top, or likely to be something else?

A weird note, the memory location of the TLDNode pointer is the same location as the first member (int count), I am pretty sure this means I screwed up somewhere with memory allocation but not sure

user1079404
  • 308
  • 1
  • 5
  • 15
  • What's the connection between the `struct tldlist` typedef you provide, and the `struct tldnode` struct definition you provide? – Crowman Oct 17 '13 at 20:32
  • Sorry about that, just a mistake, meant to be TLDNode, fixed that – user1079404 Oct 17 '13 at 20:34
  • Can you add a sample of the code that cause a segfault. There is no real limitation with what you are trying to do except if you want to access a member of the node from outside the source file. – Lohrun Oct 17 '13 at 20:42
  • I am working on it, there is a lot of code and it will take some time to isolate, but I will get there – user1079404 Oct 17 '13 at 20:50
  • @Lohrun if(strcmp(tld ,node -> tld) <= 0) this creates the fault, and GDB reports that I cannot access any of node's (which is of type TLDNode) members – user1079404 Oct 17 '13 at 21:09
  • Maybe your node pointer is `NULL`, try to find where the node pointer is set and where you went wrong ! I suppose this code is in the source file where you have the `struct tldnode` defined ? – Lohrun Oct 17 '13 at 21:11
  • @Lohrun I have a `NULL` check just a few lines above it, so it shouldn't be `NULL` at this point anyway – user1079404 Oct 17 '13 at 21:14
  • Reading your comment again, you cannot access member of a TLDNode pointer outside the file where the struct tldnode is declared. You need to define helper function like a getter to help you. I will add a small example to my answer. – Lohrun Oct 17 '13 at 21:16
  • You can also just declare the struct in all the files in which you want to access the members, of course (which is exactly what would happen after preprocessing if you put the declaration in the header file), but you obviously run the risk of having those declarations go their own separate ways if you ever change one of them. – Crowman Oct 17 '13 at 21:43

1 Answers1

2

The memory location of the pointed struct tldnode is the same as the memory location of the first member of the tldnode.

This is due to the memory layout of the structure member : each member is allocated next to the others in a memory bloc large enough to contain all of them. The size of the memory bloc is the size of the struct.

With an example, it might be clearer :

struct X {
 int a;
 int b;
};

struct X x;

printf("%p\n", &x);    // some address
printf("%p\n", &x.a);  // same address
printf("%p\n", &x.b);  // same address + 4 (provided that int are stored on 4 bytes)

The error is probably elsewhere.

You seem to want to access from a third source file the content of a pointer of type TLDNode. In short, you cannot, you don't know what is hidden behind the typedef struct tldnode TLDNode. And it is probably for the better.

If you want to access the tld pointer from inside a (TLDNode *) you need to add a getter function that will get it to you using the internal knowledge of the structure.

In the header, add something like

char * getTld(TLDNode * node);

In the source file you have, add the definition of this function.

char * getTld(TLDNode * node)
{
    return node->tld; // NULL check maybe...
}

And in your third source file, you can replace node->tld by getTld(node).

Lohrun
  • 6,432
  • 1
  • 24
  • 22
  • 1
    Not necessarily for the next structure members, don't forget about [structure packing](http://stackoverflow.com/questions/4306186/structure-padding-and-structure-packing) – fvdalcin Oct 17 '13 at 21:07