473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

memory dump/re-read?

A. Suppose my program mallocs and populates an array of structs
of a fixed size apiece. For example, something like:

struct foo {
int i;
double x[8];
char s[16];
};

We want to save this work and re-read it later. One solution
that works well -- so long as we're sure the re-reading will
occur on the same platform as the saving -- is to do a binary
dump of the array of structs with fwrite(), then read them
back into memory later with fread().

B. Now suppose my program mallocs and populates an array
of structs which themselves contain dynamically allocated
members, for example

struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?

Allin Cottrell
Wake Forest University
Jan 24 '06 #1
5 2723
On Mon, 23 Jan 2006 23:04:50 -0500, Allin Cottrell <co******@wfu.edu>
wrote in comp.lang.c:
A. Suppose my program mallocs and populates an array of structs
of a fixed size apiece. For example, something like:

struct foo {
int i;
double x[8];
char s[16];
};

We want to save this work and re-read it later. One solution
that works well -- so long as we're sure the re-reading will
occur on the same platform as the saving -- is to do a binary
dump of the array of structs with fwrite(), then read them
back into memory later with fread().

B. Now suppose my program mallocs and populates an array
of structs which themselves contain dynamically allocated
members, for example

struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?

Allin Cottrell
Wake Forest University


Please use a proper signature delimiter, like mine below. It consists
of three characters "-- ", that is two dashes and a space. Most
newsreaders recognize it and handle it properly automatically when
quoting.

As to your question, you can't portably save pointers to a file and
expect the same memory to be usable again when you read them back. For
the array of doubles, I would suggest writing an int first that
specifies how many doubles are to follow, then the doubles themselves.
You can take a similar approach with the character string, or since
you will know when the string starts, you can just dump the string and
its '\0' terminator.

But you will certainly need to decide on some extra work, to be done
when writing and undone when reading back in.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 24 '06 #2
Allin Cottrell wrote:

A. Suppose my program mallocs and populates an array of structs
of a fixed size apiece. For example, something like:

struct foo {
int i;
double x[8];
char s[16];
};

We want to save this work and re-read it later. One solution
that works well -- so long as we're sure the re-reading will
occur on the same platform as the saving -- is to do a binary
dump of the array of structs with fwrite(), then read them
back into memory later with fread().

B. Now suppose my program mallocs and populates an array
of structs which themselves contain dynamically allocated
members, for example

struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?


Fundamentally, no. You can't get dynamic pointers from anything
except malloc and friends, and you can't control their values. The
sequences returned may vary widely even on separate runs of the
same program with the same data.

One possible fancy footwork is to convert _everything_ to arrays of
known length, and then substitute indices for pointers.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Jan 24 '06 #3

Allin Cottrell wrote:
B. Now suppose my program mallocs and populates an array
of structs which themselves contain dynamically allocated
members, for example

struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?

Allin Cottrell
Wake Forest University


You can do it by having one more additional file of meta data

struct baz_meta_data {
size_t size_of_double; /* size of double array */
size_t length_of_string; /* length of string */
};

---
Best Regards,
Abdur

Jan 24 '06 #4

"Allin Cottrell" <co******@wfu.edu> wrote

struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?

No. It is no longer possible to save the array with a single C call.

What you need is a serialisation / deserialisation pair of fucntions

/* can only fail if IO fails, which you detect later */
void savebaz(struct baz *b, FILE *fp)
/* return a code for fail */
int loadbaz(struct baz *b, FILE *fp)

You can still save the non-pointer members in binary. If you wnat to port
the file, or read it, use text. The load function is trickier, since you
will need to allocate memory, and also detect malformed input.
Jan 24 '06 #5
Malcolm wrote:
"Allin Cottrell" <co******@wfu.edu> wrote
struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};

Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?


No. It is no longer possible to save the array with a single C call.

What you need is a serialisation / deserialisation pair of fucntions


OK, got it. Thanks to all who responded.

--
Allin Cottrell
Wake Forest University
Jan 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yuriy | last post by:
Hi, Can anybody explain the following? Say I have the following source XML and XSLT (see below). No matter what this XSLT does. It is just a sample to show a problem. the idea is that XSLT...
7
by: Rich Denis | last post by:
Hello, I have been trying to solve a mysterious memory leak problem and was hoping that you could help me out on my stuck point. First a bit of background. We have two app servers in an app...
9
by: Microsoft News Server | last post by:
Hi, I am currently having a problem with random, intermittent lock ups in my ASP.net application on our production server (99% CPU usage by 3 threads, indefinately). I currently use IIS Debug...
18
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but...
0
by: xievvv | last post by:
We are experiencing a memory leak in one of our applications. It is a web-based reporting system that produces large (> 500mb) PDF reports. It takes approx 4 hours to run the largest of these reports...
9
by: Michael Sparks | last post by:
Hi, I'm interested in writing a simple, minimalistic, non persistent (at this stage) software transactional memory (STM) module. The idea being it should be possible to write such a beast in a...
2
by: erfan | last post by:
Hi,comp.c: I try to learn malloc,facing a problem like this: -------- #include<unistd.h> #include<stdlib.h> #include<stdio.h> int main(void) {
1
by: Nagu | last post by:
I didn't have the problem with dumping as a string. When I tried to save this object to a file, memory error pops up. I am sorry for the mention of size for a dictionary. What I meant by...
1
by: =?Utf-8?B?d29vZiE=?= | last post by:
My laptop is generating a memory dump and crashing. With some investigation It led me to believe there was a memory problem (used memtest86+). Changed hard drive, swapped the two 512mb RAMS...
2
by: ruqiang826 | last post by:
hi I have written a service running backgroud to do something in linux. unfortunately$B!$(BI deleted the source code by mistake, and I can still see the process running background using "ps aux"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.