473,666 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*******@comca st.net
Nov 14 '05 #1
2 1435
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(voi d *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 "introspect ion" 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*********@su n.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
11946
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# using memory mapped file. We already have C++ code for handling memory mapped file. I am working on converting code for memory mapped file in C#. Now I have to pass pointer to the above structure. I converted this structure to C# as follows:
8
3318
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. However, the DEBUG_EVENT structure is defined as a union, and the size and contents vary depending on the event code contained in the header. typedef struct _DEBUG_EVENT { DWORD dwDebugEventCode; DWORD dwProcessId;
15
8226
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 also a minor change in class Mypropertyinfo, which I have commented out. When using a structure, however, the second call to GetValue returns "Default caption". Can anyone tell me why, and how I can make this work? <code> Imports System
3
8897
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 End Structure
10
4984
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 get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
6
3487
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 cracking software is getting harder. So, how can I write a superb cracking program in C++ that will be able to remove any copy-protection???
5
3788
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);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
8449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8876
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8784
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8642
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.