typedef or a multidimensional array?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 7
Reputation: acchao is an unknown quantity at this point 
Solved Threads: 0
acchao acchao is offline Offline
Newbie Poster

typedef or a multidimensional array?

 
0
  #1
Aug 8th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: typedef or a multidimensional array?

 
0
  #2
Aug 8th, 2007
> 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 170
Reputation: TkTkorrovi is on a distinguished road 
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: typedef or a multidimensional array?

 
0
  #3
Aug 8th, 2007
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.
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 7
Reputation: acchao is an unknown quantity at this point 
Solved Threads: 0
acchao acchao is offline Offline
Newbie Poster

Re: typedef or a multidimensional array?

 
0
  #4
Aug 8th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: typedef or a multidimensional array?

 
0
  #5
Aug 9th, 2007
> 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC