473,396 Members | 2,115 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,396 software developers and data experts.

Data storage/retrieval and dynamically allocating memory

I have been tinkering with this project that stores data into binary files.
The ammount of records that it stores is not known at compile-time. For
example, refer to the ZIP file structure. There is basically a header
describing each of the files and their location in the file, followed by the
actual contents of those files.

My header looks something like this:

struct myStruct {
UINT uiNumItems;
UINT *pStart;
UINT *pSize;
}

uiNumItems is the total number of items that will be stored in the custom
binary file (let's say for the moment that they are png files). pStart is
the starting location in my custom binary file where the particular png file
is written, and pSize is the filesize of that png image.

What I would like to do is the following:

myStruct ms;
int nNumItems = 20;

ms.pStart = new UINT[nNumItems];
ms.pSize = new UINT[nNumItems];

(here I will set the contents of my struct)
(now i will write that struct to a binary file)

delete[] ms.pStart;
delete[] ms.pSize;

My question is this: Is this the most elegant way to do this? I want to
implement a way to do this with the least ammount of overhead possible.
Additionally, am I making proper use of new and delete[]?

Thanks in advance :)
Jul 19 '05 #1
7 2576
One more thing I forgot to add...Judging from my implementation, would this
be cross-platform compatible? i.e., will the file that is output from one
system be able to be read on another system without modifications? My main
concern is with the size of an int (or UINT, etc) on my system may be
totally different on another system. Even if the size of an UINT is 1 larger
than on my system, this could drastically change the way I interpret the
data.

"Joe Estock" <je*****@nutextonline.com> wrote in message
news:vr************@corp.supernews.com...
I have been tinkering with this project that stores data into binary files. The ammount of records that it stores is not known at compile-time. For
example, refer to the ZIP file structure. There is basically a header
describing each of the files and their location in the file, followed by the actual contents of those files.

My header looks something like this:

struct myStruct {
UINT uiNumItems;
UINT *pStart;
UINT *pSize;
}

uiNumItems is the total number of items that will be stored in the custom
binary file (let's say for the moment that they are png files). pStart is
the starting location in my custom binary file where the particular png file is written, and pSize is the filesize of that png image.

What I would like to do is the following:

myStruct ms;
int nNumItems = 20;

ms.pStart = new UINT[nNumItems];
ms.pSize = new UINT[nNumItems];

(here I will set the contents of my struct)
(now i will write that struct to a binary file)

delete[] ms.pStart;
delete[] ms.pSize;

My question is this: Is this the most elegant way to do this? I want to
implement a way to do this with the least ammount of overhead possible.
Additionally, am I making proper use of new and delete[]?

Thanks in advance :)

Jul 19 '05 #2


Joe Estock wrote:


My question is this: Is this the most elegant way to do this?
Beauty (and elegance) is in the eye of the beholder :-)

But yes, it's a way to do the job.

I would have done it a little bit different:

struct OneItem
{
UINT Start;
UINT Size;
};

struct Header
{
UINT uiNumItems;
OneItem* pItems;
};

Header MyHeader;

MyHeader.uiNumItems = 20;
MyHeader.pItems = new OneItem[ MyHeader.uiNumItems ];

such that information describing one allocation stays together,
but in principle your system would work too.

I want to
implement a way to do this with the least ammount of overhead possible.
Additionally, am I making proper use of new and delete[]?


Nothing wrong with that.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3


Joe Estock wrote:

One more thing I forgot to add...Judging from my implementation, would this
be cross-platform compatible? i.e., will the file that is output from one
system be able to be read on another system without modifications?
Depends on the hardware, operating system and/or compiler system actually
used on the target machine.
My main
concern is with the size of an int (or UINT, etc) on my system may be
totally different on another system.
Yep. That's one problem.
Even if the size of an UINT is 1 larger
than on my system, this could drastically change the way I interpret the
data.


You figured out what problems await you when transporting binary
files on different computers.

Other problems:
* endianess (low byte first vs. high byte first)
* actual floating point format used (there are lots of them around)
* character codes (eg. ASCII vs. EBCDIC )
...

All of the problems can be solved, some of them are really tricky to
solve. Eg. endianess: Either you nail your binary format down and require
eg. low byte first, or you add additional information in the header which
documents to the file reader in which endianess format the file is written
and the reader has to account for that.
The problem with floating point formats is actually harder to solve. In
principle you again have the same 2 options, but the conversion from one
format into another format may become a pain in the ass.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4
> One more thing I forgot to add...Judging from my implementation, would
this
be cross-platform compatible? i.e., will the file that is output from one system be able to be read on another system without modifications? My main concern is with the size of an int (or UINT, etc) on my system may be
totally different on another system. Even if the size of an UINT is 1 larger than on my system, this could drastically change the way I interpret the data.


No it won't be cross-platform compatible. The size of the UINT (I am
using this is a typedef for an unsigned int, it is not a standard type)
is only one concern. The standard only guarantees the minimum size of
primitive data types, but does not guarantee the exact size. To get
around the size difference a pragmatic approach would be typedefs i.c.w.
conditional compilation:

#if defined(PLATFORM_1)
typedef unsigned long UINT32
#elif defined (PLATFORM_2)
typedef unsigned __int32 UINT32
#else
#error Unsupported platform.
#endif

Another concern is endianess, some platforms store the most significant
bytes of an integer first (big endian), others store the least
significant byte first (little endian).

It is not a good idea to let the file format be a side effect of your
code. I recommend you define a file format first, and then write code
that can read and write that file format later. When you define file
format you will have to choose between little endian and big endian
format and of how many bytes a integer has.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #5

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...


Joe Estock wrote:

One more thing I forgot to add...Judging from my implementation, would this be cross-platform compatible? i.e., will the file that is output from one system be able to be read on another system without modifications?
Depends on the hardware, operating system and/or compiler system actually
used on the target machine.
My main
concern is with the size of an int (or UINT, etc) on my system may be
totally different on another system.


Yep. That's one problem.
Even if the size of an UINT is 1 larger
than on my system, this could drastically change the way I interpret the
data.


You figured out what problems await you when transporting binary
files on different computers.

Other problems:
* endianess (low byte first vs. high byte first)


This is one of the things that I had not taken into account. How do I
determine the endianess that I am using on my system? Or is it something
that is common knowledge depending on the type of system that I am on?
* actual floating point format used (there are lots of them around)
* character codes (eg. ASCII vs. EBCDIC )
...

All of the problems can be solved, some of them are really tricky to
solve. Eg. endianess: Either you nail your binary format down and require
eg. low byte first, or you add additional information in the header which
documents to the file reader in which endianess format the file is written
and the reader has to account for that.
The problem with floating point formats is actually harder to solve. In
principle you again have the same 2 options, but the conversion from one
format into another format may become a pain in the ass.
How would I accomplish this? e.g., how would I force the low byte to be
first, or vice-versa?

--
Karl Heinz Buchegger
kb******@gascad.at

Jul 19 '05 #6
Joe Estock wrote:


How would I accomplish this? e.g., how would I force the low byte to be
first, or vice-versa?


Makes ints little endian if second parameter is true
otherwise makes them big endian.

#include <cassert>

int make_little_endian(int n, bool little)
{
// this assumes that int is 4 bytes
assert(sizeof(int) == 4);

// Check endian-ness of current machine
// if little endian then the value 1 will
// be stored in the lowest memory address.
// Deference the first byte of int(1), if
// it is 1 then little endian.
const int t = 1;
const int endian = *((char*)&t);
int i = n;

// swap bytes if machines endian-ness is not
// what we want.
if (little != endian) {
i = (n << 16) + (unsigned(n) >> 16);
}
return i;
}

Jul 22 '05 #7


Joe Estock wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...


Joe Estock wrote:

One more thing I forgot to add...Judging from my implementation, would this be cross-platform compatible? i.e., will the file that is output from one system be able to be read on another system without modifications?
Depends on the hardware, operating system and/or compiler system actually
used on the target machine.
My main
concern is with the size of an int (or UINT, etc) on my system may be
totally different on another system.


Yep. That's one problem.
Even if the size of an UINT is 1 larger
than on my system, this could drastically change the way I interpret the
data.


You figured out what problems await you when transporting binary
files on different computers.

Other problems:
* endianess (low byte first vs. high byte first)


This is one of the things that I had not taken into account. How do I
determine the endianess that I am using on my system? Or is it something
that is common knowledge depending on the type of system that I am on?


There are ways to determine the endianess but most of the time one simply
uses common knowledge. Endieness usually is fixed at the CPU level although
there are CPU's which can be switched during runtime :-)
* actual floating point format used (there are lots of them around)
* character codes (eg. ASCII vs. EBCDIC )
...

All of the problems can be solved, some of them are really tricky to
solve. Eg. endianess: Either you nail your binary format down and require
eg. low byte first, or you add additional information in the header which
documents to the file reader in which endianess format the file is written
and the reader has to account for that.
The problem with floating point formats is actually harder to solve. In
principle you again have the same 2 options, but the conversion from one
format into another format may become a pain in the ass.


How would I accomplish this? e.g., how would I force the low byte to be
first, or vice-versa?


Thats not the issue I am talking about. A double may eg consist of
8 bytes. The floating point format may eg be on machine A that the
exponent (expressed as powers of 10) resides in byte 4. On machine
B, the floating point format may be completely different: The exponent
(this time expressed as powers of 2) resides in the 7-th byte. On machine
C the situation is radically different. It doesn't have an exponent at all!

OK. The above is exaggregated but the thing is: you need to get literature
on who your compiler and the target compiler store floating point values.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

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

Similar topics

0
by: Yadagiri Rao KP | last post by:
I got an assignment usig perl and xml. And I was told to design the database. I'd like to know that how one would structure the data storage and retrieval process on a site which stores a...
7
by: smith4894 | last post by:
Hello, I have a question regarding storage locations for different data types. For example, dynamically created objects (using "new") are created on the heap. local objects ( foo() {int x;} )...
12
by: Jeremy | last post by:
Hi all, I'm getting very confused about how DB2 uses shared memory and I wonder if someone could clarify matters for me, please ? We are running 32bit DB2 V7.2 FP9 under AIX 4.3.3 on a machine...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
7
by: Fabian Wauthier | last post by:
Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: ...
3
by: yogi | last post by:
Hi guys, I'm trying to write a program that will read in a series of files and create a 3D array from the files read in for converting 2D images to 3D objects. The values read in will be...
4
by: Holger Marzen | last post by:
Hi all, AFAIK it is possible for columns to be very large, up to about 2 GB. Are there any hints or experiences about storing binary data (jpg-images, pdf-documents) in PostgrreSQL with or...
17
by: Christoph Scholtes | last post by:
Hi, I have two questions about the following code snippet. I am trying to read in a series of strings and save them to character arrays. Since I dont know how long my string is going to be (and...
8
by: Markus | last post by:
Hello everyone. Recently I stumbled upon an interesting problem related to thread-parallel programming in C (and similarily C++). As an example assume a simple "buffer" array of size 8, e.g....
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.