473,382 Members | 1,648 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,382 software developers and data experts.

Cracking C structure

For testing purposes it would be convenient to load dummy values into
a set of C structures. These structures are complex (in the sense of
a mix of data types), they are quite large (total number of elements)
and they will have a tendency to change over time.

I would be best if the dummy data was "meaningful", real dates in a
date field, a small number in the number field, a short string in a
text field, etc.

Is there a way to load the data without having to walk through each
element of each structure. I would be time consuming and fragile to
write a special function to load each structure as there are so many
and their exact content tends to change with frequent changes in the
software.

The ideal solution would be able to take a structure, loop through all
elements, for each element be able to determine its type and
dynamically assign the elment an appropriate value.

Is this doable?

Bart Torbert
ba*******@comcast.net
Nov 14 '05 #1
2 1411
Bart Torbert wrote:
For testing purposes it would be convenient to load dummy values into
a set of C structures. These structures are complex (in the sense of
a mix of data types), they are quite large (total number of elements)
and they will have a tendency to change over time.

I would be best if the dummy data was "meaningful", real dates in a
date field, a small number in the number field, a short string in a
text field, etc.

Is there a way to load the data without having to walk through each
element of each structure. I would be time consuming and fragile to
write a special function to load each structure as there are so many
and their exact content tends to change with frequent changes in the
software.

The ideal solution would be able to take a structure, loop through all
elements, for each element be able to determine its type and
dynamically assign the elment an appropriate value.

Is this doable?


No, in the sense that it is not possible to start from

struct s { int i; float f; double d; };

and somehow deduce "int, float, double."

With a little work, though, you can prepare a generic
description of a struct and write some code to process it.
You'd use something like

struct struct_layout {
enum { INT, FLOAT, DOUBLE, ..., END } type;
size_t offset;
};

struct struct_layout s_desc[] = {
{ INT, offsetof(struct s, i) },
{ FLOAT, offsetof(struct s, f) },
{ DOUBLE, offsetof(struct s, d) },
{ END, 0 }
};

You'd prepare a description like this for each struct type
of interest. Then to generate data for a particular struct
instance, you give your code a pointer to that instance and
a pointer to the matching description:

void fill_struct(void *sp, const struct struct_layout *dp);
struct s s_instance;
fill_struct (&s_instance, s_desc);

Observe that this technique is in fact more useful than
what you asked for. If "introspection" were somehow part of
C, it might at best tell you "int, float, double." But if
you prepare your own descriptions, you can go further and
tell what kinds of data these elements represent: Instead of
"int, float, double" you can have "birth_year, weight_kg,
annual_interest_rate."

In a large project it can become tedious to keep the
struct descriptions up-to-date as programmers modify the
structs themselves. If you're going to do a lot of this sort
of thing, it might be wise to generate both the struct
declarations and their corresponding definitions from a
little language invented for the purpose.

--
Er*********@sun.com

Nov 14 '05 #2

"Eric Sosman" <Er*********@sun.com> wrote in message
news:41**************@sun.com...
Bart Torbert wrote:
For testing purposes it would be convenient to load dummy values into
a set of C structures. These structures are complex (in the sense of
a mix of data types), they are quite large (total number of elements)
and they will have a tendency to change over time.

Is there a way to load the data without having to walk through each
element of each structure. I would be time consuming and fragile to
write a special function to load each structure as there are so many
and their exact content tends to change with frequent changes in the
software.

The ideal solution would be able to take a structure, loop through all
elements, for each element be able to determine its type and
dynamically assign the elment an appropriate value.

Is this doable?


No, in the sense that it is not possible to start from

struct s { int i; float f; double d; };

and somehow deduce "int, float, double."


Not possible for what mechanism?
You can't do it from "inside" a conventional C program.

You *can* do it if you can process C programs like data.
If you have what amounts to a compiler front end, you *can*
loop thru struct contents; the deduction is trivial, the compiler
stores the types of the data in its symbol table.

The DMS Software Reengineering Toolkit offers a full C
parser front end with symbol table, and it could be use
to "loop thru the structure" pretty easily.

What is harder is that there is often an application constraint
on the data (e.g., the int must be in the range 0 to 5,
the sum of the float and the double must equal 2.71828),
not only in the struct, but between structs. This information
is almost never explicitly found in the code, but it is a requirement
that any test data generator honor them, or the initial conditions
for the program under test are violated and the test result
won't mean anything.

To solve that problem, somebody has to supply additional
data constraints, and then solve them during test data generation.
I'm not an expert, but I think a lot
of work has been done on test data generation with constraints.

Google for "Automated test data generation".
--
Ira D. Baxter, Ph.D., CTO 512-250-1018
Semantic Designs, Inc. www.semdesigns.com
Nov 14 '05 #3

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

Similar topics

2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
6
by: Carl Dau | last post by:
I'm a software pirate and I think that ALL software is public domain, only idiots do pay for software, all software should be for free. But recently, copy-protection methods got better and...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.