473,288 Members | 2,350 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,288 software developers and data experts.

defines and structure instantiation

Suppose the following:

typedef void (*funcptrs)(void);

typedef struct {
unsigned int *in;
unsigned int *out;
unsigned int *overrun;
funcptrs myptrs[1]; /* >=1 for ansi */
} yada;

Is it possible to write a #define in which I can vary the storage array
size? What I mean is suppose you had:

mystruc(10, name)
mystruc(20, name)

and a #define mystruc(A,B) ...

The A would set the size of the myptrs array and the B would be the name of
the declared structure. Perhaps a union might allow the define to create
the appropriate size array? The issues is that for the hardware I'm
targeting the unsigned int *s will be 2 bytes (point to RAM) and the void
(*)(void) will be 3 bytes (point to code space in flash). If they were
both two bytes it would be easier (at least for me!). Any good ideas on
this?

Thanks

PS: I cross posted this to the moderated forum inadvertantly...sorry!
May 27 '06 #1
5 5208
Rich wrote:
Suppose the following:

typedef void (*funcptrs)(void);

typedef struct {
unsigned int *in;
unsigned int *out;
unsigned int *overrun;
funcptrs myptrs[1]; /* >=1 for ansi */
} yada;

Is it possible to write a #define in which I can vary the storage array
size? What I mean is suppose you had:

mystruc(10, name)
mystruc(20, name)

and a #define mystruc(A,B) ...

The A would set the size of the myptrs array and the B would be the name of
the declared structure.


#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(10, test);

Robert Gamble

May 27 '06 #2
Robert Gamble wrote:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(10, test);


Thanks! I'm still having some difficulty:

I have this in a header file:

#define mystruc(A,B) struct { \
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A+1]; \
} B = {(unsigned int *)&B.funcptrs[0],(unsigned int *)&B.funcptrs[0]
(unsigned int *)&B.funcptrs[A]}

This is in code:
mystruc(10, myq);

Now, when I try to use myq as in the following:
myfunc((unsigned int *)myq);

where myfunc is defined as:
myfunc(unsigned int *qu){
Qtype *q = (Qtype *)qu;
q->remove = q->insert = (unsigned int *)&q->mem[0];
}
I receive errors about myq being unable to cast from an anonymous stuct to
unsinged int. I'm trying to create unsigned int pointers that store
pointers into RAM space where 3 byte function pointers are stored.

Thanks again.

May 27 '06 #3
Rich wrote:
Robert Gamble wrote:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(10, test);
Thanks! I'm still having some difficulty:

I have this in a header file:

#define mystruc(A,B) struct { \
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A+1]; \
} B = {(unsigned int *)&B.funcptrs[0],(unsigned int *)&B.funcptrs[0]
(unsigned int *)&B.funcptrs[A]}


I'm not sure what you are trying to do in this last line, it doesn't
make much sense to me. It looks like you are trying to make the
initializer part of the macro but the structure doesn't have a member
named funcptrs and even if it did the initializer would be invalid
because it tries to initialize part of itself to parts that have not
been initialized yet. I would do the initialization seperately if I
were you.
This is in code:
mystruc(10, myq);

Now, when I try to use myq as in the following:
myfunc((unsigned int *)myq);
You are casting the value of the structure to a pointer which doesn't
make any sense, you probably want to cast the address of the structure:

myfunc((unsigned int *)&myq);
where myfunc is defined as:
myfunc(unsigned int *qu){
Qtype *q = (Qtype *)qu;
q->remove = q->insert = (unsigned int *)&q->mem[0];
}
I receive errors about myq being unable to cast from an anonymous stuct to
unsinged int.


I don't know what a Qtype is so that doesn't mean much to me. Instead
of summerizing the errors you get, just copy and paste the error
message itself. Making the change above should fix the error you
describe.

Robert Gamble

May 27 '06 #4
Robert Gamble wrote:
Rich wrote:
Robert Gamble wrote:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(10, test);


Thanks! I'm still having some difficulty:

I have this in a header file:

#define mystruc(A,B) struct { \
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A+1]; \
} B = {(unsigned int *)&B.funcptrs[0],(unsigned int *)&B.funcptrs[0]
(unsigned int *)&B.funcptrs[A]}


I'm not sure what you are trying to do in this last line, it doesn't
make much sense to me. It looks like you are trying to make the
initializer part of the macro but the structure doesn't have a member
named funcptrs and even if it did the initializer would be invalid
because it tries to initialize part of itself to parts that have not
been initialized yet. I would do the initialization seperately if I
were you.


Ignore what I said in the second sentence. The reason it would not be
valid is because the address of the array elements is not available
during the initialization. Something like this should work:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B; B.in = B.out = (unsigned int *)&B.myptrs[0]; \
B.overrun = (unsigned int *)&B.myptrs[A];

Make sure you copy&paste your code instead of re-typing it as you
obviously did here (you were missing a comma in the initializer and the
member name is myptrs, not funcptrs which your compiler should have
caught). I assume that you can safely convert a function pointer to a
pointer to unsigned int on your platform, the ability to safely do this
is not guaranteed by the Standard.

Robert Gamble

May 27 '06 #5
Assuming I got all the ">>>"s right:
Robert Gamble wrote:

#define mystruc(A, B) struct {\
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A]; \
} B

mystruc(10, test);

Note that this creates an anonymous type, so the only thing
you can do with the object is work directly with it. (In
particular you cannot create a pointer that points to this
type, because you have no associated name.)

You can, however, apply the "struct hack" (or C99's flexible
arary member technique) with reasonable hope for portability.
Rich wrote:

Thanks! I'm still having some difficulty:

I have this in a header file:

#define mystruc(A,B) struct { \
unsigned int *in; \
unsigned int *out; \
unsigned int *overrun; \
funcptrs myptrs[A+1]; \
} B = {(unsigned int *)&B.funcptrs[0],(unsigned int *)&B.funcptrs[0]
(unsigned int *)&B.funcptrs[A]}

This is ... suspicious at best.
Robert Gamble wrote:
I'm not sure what you are trying to do in this last line, it doesn't
make much sense to me. It looks like you are trying to make the
initializer part of the macro but the structure doesn't have a member
named funcptrs

This is one of the big problems ...
and even if it did the initializer would be invalid because it tries
to initialize part of itself to parts that have not been initialized
yet.
but this is not: &B.funcptrs[0] would be valid (as an "address
value" for an initializer, which is not a constant but is a valid
initializer even for objects with static duration) provided that
"B" itself has static duration. (If "B" has automatic duration,
this is only valid in C99.)
I would do the initialization seperately if I were you.


That should only help if B has automatic duration and the compiler
only handles C89 (or C90 or C94) instead of C99.

A bigger problem, to me anyway, is the use of "unsigned int *".
"funcptrs" (which was snipped) is an alias for "void (*)(void)",
i.e., pointer to function (of no arguments) returning void.
If B is an object (of unname-able type) containing a member
of type "array N of (void (*)(void))", then &B.member[i] has
type "pointer to (void (*)(void))" or "void (**)(void)":

#define DECLARE(n, typename, objectname) \
struct typename { \
void (**in)(void); \
void (**out)(void); \
void (**overrun)(void); \
void (*funcs[(n)+1])(void); \
} objectname = { \
&objectname.funcs[0], \
&objectname.funcs[0], \
&objectname.funcs[n] \
}

which now needs no casts. Of course you still need to fill
in the actual function pointer values.

In article <11*********************@j33g2000cwa.googlegroups. com>,
Robert Gamble <rg*******@gmail.com> wrote:Ignore what I said in the second sentence. The reason it would not be
valid is because the address of the array elements is not available
during the initialization.
Only true for objects with automatic duration, in which case the
entire brace-enclosed contents are not valid initializers for an
object with static duration -- but we just said that the object has
automatic duration! In C99, the brace-enclosed list becomes a
compound literal, which has a lot of latitude. (Only compound
literals that are outside functions require constant-expressions,
and if this declaration were outside a function, the object would
have static duration.)

[snippage]
Make sure you copy&paste your code instead of re-typing it as you
obviously did here (you were missing a comma in the initializer and the
member name is myptrs, not funcptrs which your compiler should have
caught). I assume that you can safely convert a function pointer to a
pointer to unsigned int on your platform, the ability to safely do this
is not guaranteed by the Standard.


I left all this in for emphasis, as it were. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 3 '06 #6

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

Similar topics

3
by: Jean-Claude Gervais | last post by:
Hi, I'm trying to write a template class that knows its own name. Specifically, I want it to behave this way - Say I instantiate a template class like this: my_vector<int> foo;
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
2
by: WittyGuy | last post by:
Hi My class looks something like this: class Base { public: Base () {} ~Base () {} // No virtual dtor in the base class private: };
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
8
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.