Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why are there so many nested definitions

Reply
Thread Tools

Why are there so many nested definitions

 
 
fl
Guest
Posts: n/a
 
      11-22-2012
Hi,

I once read someone's code. That experienced programmer used a nested definition of simple integer. Now, I obtain a source code from Matlab Simulink Coder, see below please. It is in the same fashion.

For the integer "Out", why they define a struct here?

Could you explain it to me? Thanks.



...............
typedef struct {
int32_T Out; /* '<Root>/Out' */
} ExternalOutputs_Hcic1;
 
Reply With Quote
 
 
 
 
Andrew Cooper
Guest
Posts: n/a
 
      11-22-2012
On 22/11/2012 23:20, fl wrote:
> Hi,
>
> I once read someone's code. That experienced programmer used a nested definition of simple integer. Now, I obtain a source code from Matlab Simulink Coder, see below please. It is in the same fashion.
>
> For the integer "Out", why they define a struct here?
>
> Could you explain it to me? Thanks.
>
>
>
> ..............
> typedef struct {
> int32_T Out; /* '<Root>/Out' */
> } ExternalOutputs_Hcic1;
>


I cant really explain what the above is trying to do, but we the same
technique for memory management code in Xen.

With page table management, and guests VMs with different memory
management models[1], it is very easy to get one "type" of unsigned long
mixed up with a different "type" of unsigned long.

As a result, we have hidden one of the many types of memory address in a
structure like this, and provided token accesser methods.

The result is more work for the compiler (although identical compiled
output), and a helpful error from the compiler when you accidentally mix
one type with another, as opposed to a subtle memory corruption bug.

[1] Depending on your type of guest, you can choose between PV,
Autotranslate, HAP (EPT/NPT), Shadow and now nested HAP). It leads to
interesting code internally.

~Andrew
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-23-2012
fl <> writes:
<snip>
> For the integer "Out", why they define a struct here?
>
> Could you explain it to me? Thanks.
>
> typedef struct {
> int32_T Out; /* '<Root>/Out' */
> } ExternalOutputs_Hcic1;


It's almost certainly to get some more type checking form the compiler.

Arithmetic types freely convert from one to another in C, so you can
assign a float to a char, or pass long double to function that expects
an int. But every struct is different (It's is a little more complex
than for structs in separate compilation units, but let's put that to
one side for now) so even if ExternalInputs_Hcic1 were to be declared to
contain only one int32_t, you would get an error trying to pass one to a
function that expected an ExternalOutputs_Hcic1.

--
Ben.
 
Reply With Quote
 
Heikki Kallasjoki
Guest
Posts: n/a
 
      11-23-2012
On 2012-11-23, Ben Bacarisse <> wrote:
> fl <> writes:
>> For the integer "Out", why they define a struct here?
>>
>> Could you explain it to me? Thanks.
>>
>> typedef struct {
>> int32_T Out; /* '<Root>/Out' */
>> } ExternalOutputs_Hcic1;

>
> It's almost certainly to get some more type checking form the compiler.


Given the situation -- C code automatically generated from a Simulink
model, which can have multiple outputs -- I'd think the major reason is
likely to be simple consistency: all the outputs of the model are
grouped in one structure type, no matter whether there were one or
several of them.

--
Heikki Kallasjoki
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-23-2012
Heikki Kallasjoki <fis+> writes:

> On 2012-11-23, Ben Bacarisse <> wrote:
>> fl <> writes:
>>> For the integer "Out", why they define a struct here?
>>>
>>> Could you explain it to me? Thanks.
>>>
>>> typedef struct {
>>> int32_T Out; /* '<Root>/Out' */
>>> } ExternalOutputs_Hcic1;

>>
>> It's almost certainly to get some more type checking form the compiler.

>
> Given the situation -- C code automatically generated from a Simulink
> model, which can have multiple outputs -- I'd think the major reason is
> likely to be simple consistency: all the outputs of the model are
> grouped in one structure type, no matter whether there were one or
> several of them.


Yes, that sound quite probable. I did not know that the code was
generated -- the reference to an "experienced programmer" led me astray.

--
Ben.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-24-2012
On Fri, 2012-11-23, Ben Bacarisse wrote:
> Heikki Kallasjoki <fis+> writes:
>
>> On 2012-11-23, Ben Bacarisse <> wrote:
>>> fl <> writes:
>>>> For the integer "Out", why they define a struct here?
>>>>
>>>> Could you explain it to me? Thanks.
>>>>
>>>> typedef struct {
>>>> int32_T Out; /* '<Root>/Out' */
>>>> } ExternalOutputs_Hcic1;
>>>
>>> It's almost certainly to get some more type checking form the compiler.

>>
>> Given the situation -- C code automatically generated from a Simulink
>> model, which can have multiple outputs -- I'd think the major reason is
>> likely to be simple consistency: all the outputs of the model are
>> grouped in one structure type, no matter whether there were one or
>> several of them.

>
> Yes, that sound quite probable. I did not know that the code was
> generated -- the reference to an "experienced programmer" led me astray.


Not quite astray -- the OP seemed to mention two times he had seen
this (in code from "an experienced programmer" and in generated code)
and asked a general question.

FWIW, when you see it in /my/ code it's like BB says: to get better
type safety -- so an ExternalOutputs_Hcic1 cannot be confused with
random integer variables.

Part of the reason I do it is my C++ background. This wrapping is
even *more* useful there.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Arrays in Method Definitions--a puzzle Jonah Bloch-Johnson Ruby 3 08-20-2008 02:38 AM
Nested Parameter Definitions Paddy Python 10 02-26-2007 08:20 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Nested class definitions Tony Johansson C++ 1 05-19-2005 02:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57