Differences between global and pointer access - c

Wondering what the differences between the following two bits (labeled Method 1, and Method 2) are, any benefits / risks associated with either, and finally, is there a better way.
typedef struct
{
int iAnInteger;
} s_Thing;
s_Thing sMine;
void SomeFunction(int myInt)
{
/* Method 1 */
s_Thing *pMine = &sMine;
pMine->iAnInteger = 0;
/* Method 2 */
sMine.iAnInteger = 0;
}
As stated, what are the advantages / disadvantages, best practices?
Thanks!

Method 3:
void SomeFunction(int myInt, s_Thing* pWho)
{
pWho->iAnInteger = 0;
}
is even better since will be more maintainable if you make changes to how instances of s_Thing are created.
Your other two methods both suffer from localisation issues: try to limit the number of functions that modify data held at global scope.

Method 1 and Method 2 basically do the same thing, Method 1 just adds an extra level of indirection for no real reason or benefit.
Both methods modify the contents of a global object, leading to tight coupling, re-entrancy and thread safety issues.
The right thing to do is for SomeFunction to take an s_Thing as a writable parameter:
void SomeFunction( int myInt, s_Thing *myThing )
{
...
myThing->iAnInteger = 0;
...
}
which would get called something like
int main( void )
{
s_Thing sMine;
...
SomeFunction( anInt, &sMine );
...
}
As a rule, functions should communicate via parameters and return values, rather than by setting globals. There are times when using a global is
necessary (such as with errno), but life will be easier if you can avoid it.

There is no difference, except readability.
Consider this: if you got struct, which has struct as one of its members, which in turn has a struct as one of its members, which in turn... Well, you got the point.
Then if you need to compare value A with value B of the very inner struct, you'll end up with a line like
Struct1.Struct2.Struct3.Struct4.Struct5.A == Struct1.Struct2.Struct3.Struct4.Struct5.B
Alternatively, you could put
MY_STRUCT* pMeaningfulName = &Struct1.Struct2.Struct3.Struct4.Struct5;
pMeaningfulName.A == pMeaningfulName.B;
which is much more readable and shorter, especially if you have to do many repetitive operations with the same struct.
As far as the rest goes, most compilers nowadays should produce the same code for both methods.

Related

Is there a downside to keeping static duration variables in arrays for easy processing?

I'm writing a program which uses a number of structures that must be cleaned up/updated/initialized and so on. I'm currently using something like the following code to deal with them:
typedef struct Thing { ... } Thing;
typedef void (*ProcessThing)(Thing * target);
const int ThingCount = 3;
Thing ListOfThings[ThingCount];
Thing * ThingA = ListOfThings[0];
Thing * ThingB = ListOfThings[1];
Thing * ThingC = ListOfThings[2];
int DoStuffToThings(ProcessThing Action){
int i;
for(i = 0; i < ThingCount; i++){
(*Action)(ListOfThings[i]);
}
}
ThingA, ThingB, and ThingC (and so on) are known at compile time, though the exact number/nature of them is changing as I develop the program.
The main benefit of this setup is that I can easily add more ThingN if needed by adding another definition line and incrementing ThingCount, and can easily process the list of things in new ways with more ProcessThing's.
This isn't a technique I often see used in example code, even when it seems like it would be useful. I don't have a lot of practical experience with C, and am concerned that there may be some problem with this method that I am not seeing. Is there such a problem, or am I being overly nervous?

Long arguments list, how to process them nicely, now I have many if/else statements ... pure C

I have an application where the arguments list cant be reeeealy long. I can run my app like this:
./app -operations a b c d e f g h i j ...
And so on. My a,b,c ... are algorithms which I would like to run (functions defined in my code). To be able to execute them, I have something like this:
if(a)
funA();
if(b)
funB();
if(c)
funC();
...
It does not look nice, does it? I must say, there's much more calls than just 26, since my application grows and grows, my arguments list grows too. I'm looking for a fancy way to make it simpler/prettier. Is it possible, anyone with an idea?
I dont want to use C++ nor external libraries for making it simpler. Can it be done in pure C?
Here is a very simplified possible option:
#include <stdio.h>
// create a common structure to hold all your
// function parameters;
typedef struct Parameters
{
int p1;
int p2;
} Param_Type;
// sample function 1
void func1( Param_Type *params ) {
printf("hi from func1: %d\n", params->p1 );
}
// sample function 2
void func2( Param_Type *params ) {
printf("hi from func2: %d\n", params->p2 );
}
int main() {
Parameters p;
// parse the command line and populate the parameters struct;
p.p1 = 1;
p.p2 = 1;
//create a lookup table with pointers to each function.
void (*F_A[2])(Param_Type *) = {func1, func2};
//You will still need some function, that given a set of arguments, can
// derive and return an index into the array that maps to the correct
/ function.
int func_idx = your_mapping_function(...) // todo
// dispatch the correct function call.
(*F_A[func_idx])(&p);
return 0;
}
You can use use getopt() to read the command line parameters.
And I don't see any optimization in the way you are deciding what action to take depending upon the arguments. I'd say it's just a bad design of doing things. You could try changing your approach.
You could use enums and function pointers to define handlers for every different set of arguments.
Something in the lines of:
typedef enum {
ARG_A,
ARG_B,
ARG_C,
// etcetera
MAX_ARGS
} CmdArgId;
bool cmdArgStates[MAX_ARGS];
typedef void (*CmdHandler_f)();
CmdHandler_f cmdHandlers[MAX_ARGS] = {
// TODO: set function pointers
};
int main()
{
// set cmdArgStates[] indexes to true or false,
// according to the program command line.
ParserCmdArgs();
for (int i = 0; i < MAX_ARGS; ++i)
{
if (cmdArgStates[i] == true)
{
cmdHandlers[i]();
}
}
}
If you need the handler functions to have different numbers of arguments, you can define a common function with a fixed number of args and just ignore them when they are not needed. I.e.: typedef void (*CmdHandler_f)(); could also de something like typedef void (*CmdHandler_f)(int, int, int);
One idea would be to create a structure that can be used to abstract each command line option. Here is a possible method you could use to implement that idea :
Create a structure that can represent each function you need to
support, and have it hold a pointer to the function and a string for the search key.
Create an array of this structure and initialize the members accordingly
Use an algorithm, such as a binary search, to find the key in the structure array when looping through the command line arguments.
Once you have the structure from the key, you can then call the function which will handle the behavior desired in the option.
It's possible this won't apply to your situation, but this really is an implementation specific problem. If you want a more specific solution, you should probably post more details about your functions and the implementation behind them.

Resuming a recursive function in C (gcc)?

I use gcc (gnu99) under linux.
Assume I have a simple function that computes (say, prints) some values.
To fix ideas without resorting to factorial, let me propose this little toy function:
void rec(int val, int nd)
{
val *= 10; nd++;
for (int u=0; u<=9; u++, val++)
if (val && 0==(val%(nd*nd)))
{
printf("%d\n",val);
rec(val,nd);
}
}
which called as rec(0,0), will print the 86 positive numbers with the property that every their prefix of length k is divisible by k^2 (largest is 6480005).
Q: is there a standard way to turn it into a routine which can be called repeatedly and each time returns a new value until it signals in some way that there are no more values ?
In practice, I need a way to call rec(), getting back a value, and then be able to resume execution from where it was, to get next value and so on.
I thought about using a combination of setjmp(), longjmp(), setcontext() and the like, but the examples on these topics confused me a little.
Thanks.
Without resorting to setjmp() and its likes, I would think of two possibilities to achieve your objective.
Define a structure and functions that operate on it
You can define a structure that contains the state you are interested in:
struct S {
int * numbers;
size_t length;
size_t current;
}
and a set of functions that operate on it:
// Basically your rec(val,nd) function
int initialize(int val, int nd, S* s);
// Get the next value
int getNextValue(const S* s);
...
// Free memory
void Sfree(S* s);
Embed a state into your own function
In this case you embed a persistent state (using some static variable) directly into your function rec(). Being persistent this state will be preserved across different calls to the function, and permit you to keep track of how many times the function was called and of any other information you may need. Beware that this solution requires a particular care if used inside multi-threaded environments.
In C++ this is done via a class that holds a the state (variables).
In C, the preferred method of operation is to pass this state or context to the function.
Do not use a global state as later on you'll find that it causes too many issues.
Example:
int foo(struct fooContext* c, int arg1, int arg2) {
}

Arguments against static int pointer

I'm about to debug someone else's code and I stumbled across a certain 'way' of handling with global arrays which I consider deeply bad, but the one who first used it swears to it.
I need to find arguments against it.
Here is the code written simplified (this is not the original code, just an abstracted version)
So my question: which arguments would you bring against (or maybe some code which brings down this method) this?
int test(int i, int v, int type, int** t)
{
static int *teeest;
int result = 0;
switch(type)
{
case (1):
{
int testarr[i];
teeest = testarr;
}
break;
case (2):
result = teeest[i];
break;
case (3):
teeest[i] = v;
break;
}
if (t != NULL)
{
*t = teeest;
}
return result;
}
int main()
{
int *te = (int*)1;
test(5, 0, 1, &te);
printf("%p\n", te);
int i=0;
for(;i<5;i++)
{
test(i, i, 3, NULL);
printf("Value: %d\n", test(i,0,2, NULL));
}
return 0;
}
local variables are dead after the block they declared in, so this code is undefined behavior. Like every accessing random address, it may work, but it also may not work.
Note that if you use malloc instead of int testarr[i], (and worry to free the previous array, and to initialize teeest), it will be correct. the problems of this code have nothing about static pointers.
This is really bad. Just because the pointer is static doesn't mean the data it points to will be around. For example, testarr disappears when the function exits and the returned pointer, if used, might cause dragons to appear.
It seems to me the big downfall of this style is that you are hiding the fact that you are accessing a locally declared array which is on the stack. Then you persist a pointer to your stack which will persist through calls, which will have different stacks each call.
Another thing I was thinking about is that you have hidden from the developer what the data structure is. Indexing an array is a normal operation. Indexing a pointer makes the developer acknowledge it is an array and not a more complex data type. This also adds confusion to bounds checking.
Another thing is, that all disadvantages of global variables apply directly. The code is not reentrant, and hard to make thread-safe (if that's a concern).

How to reset static variables within a function

Is there a way to reset variables declared as static within a function? The goal is to make sure that the function is not called with lingering values from an unrelated call. For example, I have a function opearting on columns of a matrix.
int foo(matrix *A, int colnum, int rownum){
static int whichColumn;
static int *v; //vector of length A->nrows
if (column != whichColumn){
memset(v,0,size);
whichColumn = which;
}
//do other things
}
The function is called n times, once for each column. Is this a proper way of "re-setting" the static variable? Are there other general fool-proof ways of resetting static variables? For example, I want to make sure that if the call is made with a new matrix with possibly different dimensions then the vector v is resized and zeroed etc. It seems the easiest way may be to call the function with a NULL pointer:
int foo(matrix *A, int colnum, int rownum){
static int whichColumn;
static int *v; //vector of length A->nrows
if (A == NULL){
FREE(v);
whichColumn = 0;
}
//do other things
}
Use an idempotent initializer function and global variables instead.
For example:
int foo;
int *m = NULL;
static void InitVars() {
foo = 0;
if (m != NULL) {
free(m);
}
m = malloc(sizeof(int)*5);
memset(m, 0, sizeof(int)*5);
}
If your initializer is really idempotent, you can call it again to reset the variables.
If you need this to be called automagically, use __attribute__((constructor)) (for GCC) like so:
static void InitVars __attribute__((constructor)) ();
However, you should note that if you need to do this, you should reconsider using in-function static variables and instead use passed-in fresh ones that are returned/written and passed to subsequent related calls.
I'd recommend turning it into a struct and writing a small helper function for managing the semantics of what you're trying to do. It could return the buffer if the request is appropriate for its size, or create a new one on demand (and free the old one) if necessary.
One approach I've seen used when a C module was imported to C++ was to surround the whole module with a class wrapper, and replace all static variables inside functions with uniquely-named "global" varaibles outside the functions. I don't know any good way to achieve a similar effect for projects involving multiple source files, though I'd like to know if one exists. I have some embedded system code in C, which I simulate by adding some C++ wrappers in VS2005. For example, I have I/O registers defined so that something like TX1CON = 0x5C; would translate into something like IOMAP(0x251).P = 0x5C; IOMAP is a property which would send "write 0x5C to address 0x251" to a hardware-simulation program. This approach works well, but I can't do a clean reset. Any ideas?
An approach which can sometimes be helpful if one needs a "reset" method which can hit an unknown number of functions or modules is to have a global counter for how many times that reset method has been called, and then have each function or module include code like:
extern unsigned long global_reset_count;
void do_something(int whatever)
{
static ... this, that, the other, etc. ...;
static unsigned long my_reset_count;
if (my_reset_count != global_reset_count)
{
my_reset_count = global_reset_count;
... initialize this, that, the other, etc ...
}
}
In some multi-threading contexts, if the initialization of the static variables may depend upon some global variables, one may wish to replace the "if" with a "while"; in such a case; memory barriers may also be needed in such a case, though the exact requirements would vary depending upon the operating environment.
Also, an alternative pattern that may be useful within embedded systems would be to have a modules_initialized global variable which gets set to 0 by the global reset method, and then have each module start with something like:
if (!atomic_bit_test_and_set32(&modules_initialized, FOOBOZZ_MODULE_ID))
{
... Initialize module FOOBOZZ ...
}
This would require that there be no more than 32 module ID's, and would require that they be uniquely allocated somehow, but some systems can handle that pretty nicely. For example, a linker may allow one to define a "data section" from address 0-31 of an address space independent from any other; if each module declares a single-byte variable within that address space, the linker could generate the appropriate addresses for those variables.
you could build your function in such a way that if you call it with zero parameters, then it will reset its internal static variables
here is an example :
int foo(matrix *A = NULL, int colnum = 0, int rownum = 0)
{
static int whichColumn;
static int *v; //vector of length A->nrows
if (A == NULL){
FREE(v);
whichColumn = 0;
}
//do other things
}
You actually just have to call function to reset like this:
foo(); // internal values would then be reset
Make sure that all your parameters to the function has default values, if for example you pass an optional, then make sure it has = boost::none as default value

Resources