944,111 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 8223
  • C RSS
Aug 8th, 2007
0

typedef or a multidimensional array?

Expand Post »
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.
  1. //Here's the excerpt from my miniSEED.h
  2.  
  3. #define MAXSEEDFRAMES 7 //7 for 512, 63 for 4096
  4. #define MAXWORDSPERFRAME 16
  5. typedef int compressed_frame[MAXWORDSPERFRAME];
  6.  
  7. typedef struct {
  8. mSEED_header head;
  9. compressed_frame frames[MAXSEEDFRAMES];
  10. } mSEED_record;
  11.  
  12. typedef struct mSEED_secBuf {
  13. mSEED_record *rec_ptr; //some pointer to the seed data frame
  14. int subframe[MAXWORDSPERFRAME]; // temp array to hold frame data
  15. int channel; // Determines which channel's frame
  16.  
  17. int ori_dnib; // Subframe formatting
  18. int new_dnib;
  19.  
  20. int data_buffer[7]; // Compressed data pre-storage
  21. int buff_count; // The current buffer position
  22. int sub_count; // The current subframe being written to
  23. int frame_count; // The current frame # being written to
  24. int fwd_int_const; // Forward Integration Constant
  25. int rev_int_const; // Reverse Integration Constant
  26. } mSEED_data, *pmSEED_data;
  27.  
  28. //heres the excerpt where i copy the data over
  29. // store the dnib code, if buffer has enough data, fill the subframe
  30. if(steim_concat(p_msDAT)) {
  31. fill_subframe(p_msDAT);
  32. memcpy(p_msDAT->subframe, p_msDAT->rec_ptr->frames[p_msDAT->frame_count],
  33. (sizeof(int)*MAXWORDSPERFRAME));
  34. }

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acchao is offline Offline
7 posts
since Jul 2007
Aug 8th, 2007
0

Re: typedef or a multidimensional array?

> 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
  1. typedef struct {
  2. int frame[MAXWORDSPERFRAME];
  3. } 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 8th, 2007
0

Re: typedef or a multidimensional array?

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.
Reputation Points: 85
Solved Threads: 13
Junior Poster
TkTkorrovi is offline Offline
170 posts
since Mar 2005
Aug 8th, 2007
0

Re: typedef or a multidimensional array?

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acchao is offline Offline
7 posts
since Jul 2007
Aug 9th, 2007
0

Re: typedef or a multidimensional array?

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: file and multidimensional array
Next Thread in C Forum Timeline: Assmbler implementation using C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC