开发者

structure with linked-list memory dump

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 nece开发者_如何学运维ssary 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.


There's no neat way to do this, as these will have memory addresses, and the next time it is read in, it will contain memory addresses that could possibly be invalid...the only thing you could do is have a holding area for data to be read/written, let's look at how to write the data to disk based on the contents of the linked list...

struct rwBufferData{
    int a;
    char t[10];
};

and fill the 'rwBufferData' prior to writing by using memset and memmove

struct rwBufferData rwBuf;
struct data *dataPtr;
memset(&rwBuf, '\0', sizeof(struct rwBufferData));
memmove(&rwBuf, dataPtr, sizeof(struct rwBufferData));

Now you can then write rwBuf to file... I'll leave the reverse operation as an exercise...


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜