Discussion:
Some static about static
(too old to reply)
goodTweetieBird
2009-09-02 17:22:11 UTC
Permalink
I have the following struct in a C, not C++ program.


typedef packed struct
{
header_struct header;
fault_struct fault;
} faultMsgType;


It seems the use of static is different with typedefs than with built-
in types. I like to use typedefs for a number of reasons: I can put
them in headers and it makes the predictive typing work better in
slickEdit, etc. I wanted the make a variable of this type static in a
particular function as the function was called by a built in OS timer
task which seems to have a small stack. However,

static faultMsgType faultMsg;

did not make it static, meaning it was still on the stack. But this
did:

faultMsgType static faultMsg;

Is this proper compiler behavior?


Thanks,

jh
Igor Tandetnik
2009-09-02 18:10:12 UTC
Permalink
Post by goodTweetieBird
I have the following struct in a C, not C++ program.
typedef packed struct
{
header_struct header;
fault_struct fault;
} faultMsgType;
What's "packed"? It's not a C or C++ keyword. Is it a macro? If so, what
does it expand to?
Post by goodTweetieBird
static faultMsgType faultMsg;
did not make it static, meaning it was still on the stack.
How do you determine this?
Post by goodTweetieBird
faultMsgType static faultMsg;
This is not valid C, and shouldn't compile. What compiler are you using?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
goodTweetieBird
2009-09-02 19:35:02 UTC
Permalink
Thanks for posting.
Post by Igor Tandetnik
What's "packed"? It's not a C or C++ keyword. Is it a macro? If so, what
does it expand to?
We use it to jam data together in structs where elements would
otherwise fall on native size boundaries. It is useful to me when
moving data between pipes/queues and structs. For instance when
pulling data from a pipe I just give it the address of the packed
struct. If the struct were not packed I would need to use memcpy to
copy it into a struct piecewise instead of loading it directly from
the pipe. Saves some time and memory in my embedded system. It is
apparently supported by many compilers:

http://en.wikipedia.org/wiki/Data_structure_alignment
Post by Igor Tandetnik
   static faultMsgType faultMsg;
did not make it static, meaning it was still on the stack.
How do you determine this?
The struct is quite large and it caused the calling task to overflow
its stack allotment. Then I verified it by looking at memory with a
debugger.
Post by Igor Tandetnik
This is not valid C, and shouldn't compile. What compiler are you using?
Mentor Graphics Microtek compiler. Apparently it has sufficient
thrust....


thanks,

jh
Igor Tandetnik
2009-09-02 19:54:22 UTC
Permalink
Post by goodTweetieBird
Post by Igor Tandetnik
This is not valid C, and shouldn't compile. What compiler are you using?
Mentor Graphics Microtek compiler.
Then perhaps you might want to ask in a support forum for that compiler,
and/or contact its vendor. Now, do you have any issues with Microsoft
Visual C++?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
goodTweetieBird
2009-09-02 20:14:56 UTC
Permalink
Post by Igor Tandetnik
Post by goodTweetieBird
Post by Igor Tandetnik
This is not valid C, and shouldn't compile. What compiler are you using?
Mentor Graphics Microtek compiler.
Then perhaps you might want to ask in a support forum for that compiler,
and/or contact its vendor. Now, do you have any issues with Microsoft
Visual C++?
--
With best wishes,
    Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Sorry, didn't know this was not common ground with Microsoft Visual C+
+.

Continue reading on narkive:
Loading...