473,788 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2745
On Mon, 23 Jan 2006 23:04:50 -0500, Allin Cottrell <co******@wfu.e du>
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.l earn.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.c om, 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_strin g; /* length of string */
};

---
Best Regards,
Abdur

Jan 24 '06 #4

"Allin Cottrell" <co******@wfu.e du> 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.e du> 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
1719
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 transforms small XML into quite big XML. Now, I have a straightforward C# (see below) code that does this transform and writes result into XmlTextWriter. (Oleg's NXSLT.EXE is also suitable).
7
2438
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 center cluster that are each running a number of web applications. For some reason one of the boxes' asp_wp process keeps recylcing on us. It seems that the memory just grows and grows and then recycles on us. I cam across and have read the...
9
3268
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 Tools to do a memory dump of the app when the lock up occurs, however the stack information is not very useful. I have just put a new build of our system onto production, and this build is a "Debug" build as opposed to a "Release" build. I am...
18
2636
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 tweaking the C++ code appears to fix the problem on Linux. I have a static array that is declared privately in one class and a structure that is declared publicly in another class. The program uses both classes. I was getting core dumps and traced...
0
1274
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 and during this process we have been experiencing issues with the aspnet worker process being recycled as the application's memory keeps growing to around 600mb. From my investigation, it appears that the memory is becoming bloated with character...
9
1532
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 way that can be made threadsafe fair easily. For those who don't know, STM is a really fancy way of saying variables with version control (as far as I can tell :-) designed to enable threadsafe
2
3802
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
6300
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 65000X50 is that it has 65000 keys and each key has a list of 50 tuples. I was able to save a dictionary object with 65000 keys and a list of 15-tuple values to a file. But I could not do the same when I have a list of 25-tuple values for 65000 keys.
1
1712
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 around, replaced RAMwith 1x1GB RAM, and changed the processor. None of this has solved the problem. Without warning I stll get a BSOD and a physical dump of memory. I have reinstalled the operating system and run it with no additional software that...
2
2631
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" : username 13820 0.0 0.0 60368 2964 ? S Aug20 0:33 python ./UpdateJobStatus.py I wonder if there is some way to dump the programme
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.