|
-
October 3rd, 2005, 04:26 AM
#1
best way to respresent data
hi,
I'm looking for the best way to respresent the following type data.
<int val_1>, <int letter_1>, <bool flag_1>, <int val_2>, <int letter_2>, <bool flag_2>, ....... <int val_n>, <int letter_n>, <bool flag_n> where n can be upto any large integer value.
first I was thinking maybe use a varaible length array but maybe better would be to have an array of structures maybe like the following
typedef struct data
{
int val;
char letter;
bool flag;
} data_form;
data_form data_arr[];
I would like your expert opinion on pros and cons for this and is there a better way.
In extension to this I would to be able to encode and decode this type of data and I think this is possible thru the use of "rpc gen".
From reading RFC 1832 on web I know that you can have variable length arrays by use of " <> " for variable length.
It also states that you can have arrays of any types so I guess variable length array of type struct like above would be possible.
I would appreciate any comments.
thank you.
-
October 3rd, 2005, 04:51 AM
#2
Re: best way to respresent data
To represent that data, it is correct to use a structure.
If your program is a C++ program, you can use std::vector<data>.
For more informations, you can get some documentation:
http://www.sgi.com/tech/stl/Vector.html
note : a std: eque<data> can be more efficient if you have many insertions to do in your container.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
October 3rd, 2005, 06:30 AM
#3
Re: best way to respresent data
thanks for reply.
it is a C program.
does anyone know if its possible to have an an array of structures in XDR format.
from I know it should be possible but I get following error from this code
struct test{
int val;
char letter;
bool flag;
};
typedef test test1;
test1 test_arr<>;
ERROR:
test1 test_arr<>;
^^^^^
line 55: definition keyword expected
-
October 3rd, 2005, 08:48 AM
#4
Re: best way to respresent data
 Originally Posted by Moore
does anyone know if its possible to have an an array of structures in XDR format.
I don't think C supports that!
XDR is just a descriptive languages that describes data structures.
To use variable-length arrays in C, you must use malloc and free, and realloc in some cases.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
October 3rd, 2005, 09:02 AM
#5
Re: best way to respresent data
well I tried the following XDR description:
struct test {
int val;
char letter;
bool flag;
};
typedef struct test test_List<>;
The following C hdr info is generated:
struct test {
int val;
char letter;
bool_t flag;
};
typedef struct test test;
typedef struct {
u_int test_List_len;
test *test_List_val;
} test_List;
do you know if this is valid?
thank you.
-
October 3rd, 2005, 03:43 PM
#6
Re: best way to respresent data
 Originally Posted by Moore
do you know if this is valid?
Yes, it is a correct declaration of structure containing an array.
But, if you want to use it, you will need some work.
For example, here is a usage example:
Code:
#include <stddef.h>
#include <alloc.h>
#include <mem.h>
#include <stdio.h>
typedef enum bool {false=0,true=1} bool;
typedef bool bool_t;
typedef size_t u_int;
struct test {
int val;
char letter;
bool_t flag;
};
typedef struct test test;
typedef struct {
u_int test_List_len;
test *test_List_val;
} test_List;
bool test_List_Construct(test_List *This,u_int size)
{
This->test_List_len=size;
This->test_List_val=(test *)malloc(sizeof(test)*size);
return size==0 || This->test_List_val!=NULL;
}
bool test_List_ConstructFill(test_List *This,u_int size,const test *FillItem)
{
test *p,*pEnd;
if (!test_List_Construct(This,size)) return false;
p=This->test_List_val;
pEnd=p+This->test_List_len;
for(; p!=pEnd ; ++p)
*p=*FillItem;
return true;
}
void test_List_Destroy(test_List *This)
{
free(This->test_List_val);
This->test_List_val=NULL; /* not really needed, but may avoid danling pointers */
This->test_List_len=0;
}
bool test_List_Copy(test_List *NewList,test_List *This)
{
if (!test_List_Construct(NewList,This->test_List_len)) return false;
memcpy(NewList->test_List_val , This->test_List_val , This->test_List_len*sizeof(test));
return true;
}
bool test_List_Resize(test_List *This,u_int new_size)
{
void *NewArray=realloc(This->test_List_val,new_size*sizeof(test));
if (!NewArray) return false;
This->test_List_len=new_size;
This->test_List_val=(test *)NewArray;
return true;
}
bool test_List_Assign(test_List *This,test_List *Source)
{
if (!test_List_Resize(This,Source->test_List_len)) return false;
memcpy(This->test_List_val , Source->test_List_val , Source->test_List_len*sizeof(test));
return true;
}
void TestDisplay(test_List *This)
{
u_int i;
test *pTest;
for(i=0;i<This->test_List_len;++i)
{
pTest=&This->test_List_val[i];
printf("{%i,%c,%s} "
,pTest->val
,pTest->letter
,pTest->flag? "true" : "false" );
}
}
int main()
{
test_List array,array2;
test template={1,'A',false};
u_int i;
test_List_ConstructFill(&array,12,&template);
TestDisplay(&array);printf("\n\n");
for(i=0;i<array.test_List_len;++i)
array.test_List_val[i].val=42-i;
TestDisplay(&array);printf("\n\n");
test_List_Copy(&array2,&array);
TestDisplay(&array2);printf("\n\n");
test_List_Resize(&array2,6);
TestDisplay(&array2);printf("\n\n");
test_List_Assign(&array2,&array);
TestDisplay(&array2);printf("\n\n");
test_List_Destroy(&array2);
test_List_Destroy(&array);
return 0;
}
However, i don't like much the generated names:
It is useless to prefix fields with the structure name, it justs complicate the program if the structure must be renamed, and adds more typing!
I don't like the term list which is abusively used for an array. It should be used for linked-list.
Code:
typedef struct {
u_int len;
test *val;
} test_Array;
This structure is preferable.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|