Hi,

my name is Andrew. I believe this will be my first post on daniweb so please bear with me.

I'm working on a compression algorithm (miniSEED steim 2 compression if anyone is interested, its for seismic lossless compression) in C so I do a lot of bit manipulation of the data. I need to save the data in a record that has 64 64-byte frames. The first frame is header information. The second frame is where all the real data begins to be stored. Each frame contains 16 32-bit words (or subframes).

So I've created a Record structure that contains a header struct and a new data type of 64 bytes. My issue arises when i do my compression algorithm and I'm trying to transfer the 64 bytes from my temporary frame storage to the corresponding frame in the record structure.

Here's the code to help clarify what I'm talking about.

//Here's the excerpt from my miniSEED.h

#define MAXSEEDFRAMES       7   //7 for 512, 63 for 4096
#define MAXWORDSPERFRAME    16
typedef int     compressed_frame[MAXWORDSPERFRAME];

typedef struct {
    mSEED_header    head;
    compressed_frame frames[MAXSEEDFRAMES];
} mSEED_record;

typedef struct mSEED_secBuf {
    mSEED_record *rec_ptr;  //some pointer to the seed data frame
    int subframe[MAXWORDSPERFRAME];       // temp array to hold frame data
    int channel;            // Determines which channel's frame
    
    int ori_dnib;           // Subframe formatting
    int new_dnib;
    
    int data_buffer[7];     // Compressed data pre-storage
    int buff_count;         // The current buffer position
    int sub_count;          // The current subframe being written to
    int frame_count;        // The current frame # being written to
    int fwd_int_const;      // Forward Integration Constant
    int rev_int_const;      // Reverse Integration Constant
} mSEED_data, *pmSEED_data;

//heres the excerpt where i copy the data over
// store the dnib code, if buffer has enough data, fill the subframe
        if(steim_concat(p_msDAT)) {
            fill_subframe(p_msDAT);
            memcpy(p_msDAT->subframe, p_msDAT->rec_ptr->frames[p_msDAT->frame_count],
                        (sizeof(int)*MAXWORDSPERFRAME));
        }

Right now I'm using typedef and to copy over the mSEED_data.subframe to the mSEED_record.frames[x] . I use memcpy. Would a multidimensional array better serve my purposes or is it simply a matter of style?

Thanks in advance,
Andrew

Recommended Answers

All 4 Replies

> to copy over the mSEED_data.subframe to the mSEED_record.frames[x] . I use memcpy
Except your code is copying the other way...

Besides, the final sizeof should have been sizeof(compressed_frame) to maximise the benefit of using the typedef.

> int subframe[MAXWORDSPERFRAME]; Saying compressed_frame subframe; would have been more in keeping with your efforts elsewhere.

Though if you made it a struct, like this

typedef struct {
    int frame[MAXWORDSPERFRAME];
} compressed_frame;

Then you would be able to do p_msDAT->subframe = p_msDAT->rec_ptr->frames[p_msDAT->frame_count]; as a straigh-forward structure assignment.
The downside is that everywhere else now has to have " .frame[pos] " member access.

> Would a multidimensional array better serve my purposes or is it simply a matter of style?
Despite the typedef, a 2D array is exactly what you have at the moment. It is functionally no different to saying int frames[MAXSEEDFRAMES][MAXWORDSPERFRAME]; Thanks for using the code tags, so few people seem to manage it on their first post that it's a rare treat worthy of comment.

I don't know, in general, c doesn't guarantee really much concerning the exact layout of the structure in memory. So, using memcpy for structures, is likely not so good idea... The only good way to copy structures in c, is member by member. Therefore certainly, if something can be implemented as a multidimensional array, then it should be done so, all the copying and writing would be easier then.

Just to clarify.. typedef int compressed_frame[MAXWORDSPERFRAME] Would I use compressed_frame subframe; as I would use an array of int's? subframe[pos] = data; Thanks Again!

> Would I use compressed_frame subframe; as I would use an array of int's?
Yes.

Remember that typedef doesn't add anything new, it just allows you to express something in a more compact or readable form. So if your typedef is an array, then all the usage will reflect that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.