views:

64

answers:

3

Hi, is there any standard approach which I've missed at school to dump C structure with nested linked lists on disk in reasonable way? What I don't want to do is:

  • use protocol-buffers or any other like serializators,
  • don't want to create JSON, XML or other

I've few ideas:

  • allocate accurate memory amount (or extend existing one) and manage it by myself, placing list elements in stack like approach using some additional fields to manage relative addresses. When necessary dump block on disk. Having procedures to map block from disk create desirable structure being aware of Byte-order.
  • push main structure to file, then push List elements, store information about list in the header of a file.

To image this I'll give some more details posting example code:

typedef struct{
   int b;
   List *next;
}List;

typedef struct{
   float b;
   List2 *next;
}List2;

typedef struct{
   List *head;
   List *tail;
} info;

typedef struct{
   List2 *head;
   List2 *tail;
} info2;

struct data
{
    int a;
    char t[10];
    info first;
    info second;
    info2 third;
};

cheers P.

EDIT:

I've extended main structure, seems like previous one haven't indicate the problem fully. I'm aware that pointers on disk are useless. Ideas and pseudocode allowed.

A: 
tommieb75
I think you were not aware of structure types in my post, they represent different scheme.Assuming your post I've a feeling that you try to propose more or less this same solution which i've proposed in second point. Or I missed some important part? I'm aware that pointers on disk are useless.
bua
Should never have to store list information as the memory addresses are useless, which is where my solution comes in, by copying a portion of the list node, into a buffer holding, in preparation for writing to disk...you start at the "top" (logical) of the file which represent the start of the list, and read through it, and build up the list going along.....
tommieb75
A: 

Serialize the data in the order it's held in the linked list, record-style to a file. fwrite is particularly good for this. Be sure to dereference pointers, and be aware of the role endianness plays in this.

Here's some vague pseudocode:

List *list_new();
List *list_add(List *, void *data);
List *list_next(List *);

while (node) {
    fwrite(node->data, sizeof(node->data), 1, fp);
    node = list_next(node);
}

Rough code for reading back into a live list:

List *node = list_new();
while (true) {
    struct data *buf = malloc(sizeof(*buf));
    if (1 != fread(buf, sizeof(*buf), 1, fp))
        break;
    list_add(node, buf);
}

Update0

If you begin to nest more advanced structures such as other linked lists, variable length strings etc., you'll need to provide types and lengths for each record, and a way to nest records within other records.

As an example, if your top level linked list had a data member that was another list, you'd be best to store that member as a nested record, complete with a length, and type field. Alternatively, you could define sentinel records, such as \0 for character strings (an obvious choice), and zeroed blocks for struct data.

Matt Joiner
How would you solve more complex problem with deserializing more complex structure containing more linked lists?
bua
Ok, I think this would work:Your solution + assumption that:Each list-chain would be stored in order, saving last element tagged with some information that it's NULL element.Then reading stream I'd know which lists ends where.
bua
A: 

I have not understood your problem correctly, but dumping a struct to disk and reading it back reliably has multiple issues.

Most important one is structure padding or byte stuffing. So you would have to take care of that also.