Function Warnings in C - c

Hello guys i have threefunctions for which i get 4 warnings...!!
The first one is this
void evaluatearxikos(void)
{
int mem;
int i;
double x[NVARS+1];
FILE *controlpointsarxika;
controlpointsarxika = fopen("controlpointsarxika.txt","r");
remove("save.txt");
for(mem = 0; mem < POPSIZE; mem++)
{
for(i = 0; i < NVARS; i++)
{
x[i+1] = population[mem].gene[i];
}
rbsplinearxiki();
XfoilCall();
population[mem].fitness = FileRead();
remove("save.txt");
}
fclose(controlpointsarxika);
}
For this one the compiler warns me tha variable x is set but not used...!! But actually i am using the variable x...!!!
The second function is this one...
void elitist(void)
{
int i;
double best,worst;
int best_mem,worst_mem;
best = population[0].fitness;
worst = population[0].fitness;
for(i = 0; i < POPSIZE - 1; i++)
{
if(population[i].fitness > population[i+1].fitness)
{
if(population[i].fitness >= best)
{
best = population[i].fitness;
best_mem = i;
}
if(population[i+1].fitness <= worst)
{
worst = population[i+1].fitness;
worst_mem = i+1;
}
}
else
{
if(population[i].fitness <= worst)
{
worst = population[i].fitness;
worst_mem = i;
}
if(population[i+1].fitness >= best)
{
best = population[i+1].fitness;
best_mem = i+1;
}
}
}
if(best >= population[POPSIZE].fitness)
{
for(i = 0; i < NVARS; i++)
{
population[POPSIZE].gene[i] = population[best_mem].gene[i];
}
population[POPSIZE].fitness = population[best_mem].fitness;
}
else
{
for(i = 0; i < NVARS; i++)
{
population[worst_mem].gene[i] = population[POPSIZE].gene[i];
}
population[worst_mem].fitness = population[POPSIZE].fitness;
}
}
For this one i get two warnings that the variables worst_mem and best_mem may be used uninitialized in this function..!! But i initialize values to both of them..!!
And the third function is this...
void crossover(void)
{
int mem,one;
int first = 0;
double x;
for(mem =0; mem < POPSIZE; mem++)
{
x = rand()%1000/1000;
if(x < PXOVER)
{
first++;
if(first%2 == 0)
{
random_Xover(one,mem);
}
else
{
one = mem;
}
}
}
}
For which i get that the variable one may be used unitialized..!! But it is initialized..!
Can you please tell me what is wrong with these functions...??
Thank you in advance

In your first function, you set (assign) x, but you never read it, hence you are not using it... you're only wasting CPU cycles by writing to it. (Note also that because you index it as i+1 you write beyond the space you've allocated for it).
In the second function, your initializations to those variables are in conditional blocks. You can see that (perhaps? I didn't verify) in all conditions they are initialized but your compiler isn't that smart.
In your third function, it does appear that one could be refered to without having first been initialized.

First: You set x but do not use it. It's a local variable that gets set but it's dropped as soon as the function returns.
Second: There might be values that makes it so that your best_mem/worst_mem never gets set in your if/else, but you are using them later on. If they haven't been set, they contain garbage if not initialized.
Third: While it shouldn't happen that you try to use an uninitialized variable in your code, it still looks weird and compiler doesn't see that it won't happen first time.
When you get compiler warnings, treat is as you are doing something wrong or rather not recommended and that it could be done in a better way.

The x variable is only used on the left hand side (i.e. assigned a value). You are not using that value on the right hand side or pass it to a function.
It may be possible to get to the end of the loop for(i = 0; i < POPSIZE - 1; i++) without those variables given a value. Why not set them in the declaration.
The call to random_Xover(one,mem); could be called when one is not set. Change the line int mem,one; to int mem,one = <some value>;

Related

Can a function remember variables between numerous calls?

I am trying to create a function that will take in the positions of numerous bodies moving in circular motion, and output their orbital periods. Each body is stored in a struct which contains its X, Y and Z co-ordinate (as well as some other information I don't need for this specific task).
My current function for doing this is:
double calc_period(Body *bodies, double t, int Nbodies, int i)
{
double orbit_angle[Nbodies];
double initial_angle[Nbodies];
double last_angle[Nbodies]
double last_time[Nbodies];
double half_step[Nbodies];
double running_total[Nbodies];
double orbitN[Nbodies];
double average_period[Nbodies];
orbit_angle[i] = atan(bodies[i].r[Y] / bodies[i].r[X]);
if (t==0) {
//Initialise all the variables to 0 the first time through
last_angle[i] = 0;
initial_angle[i] = orbit_angle[i];
orbitN[i] = 0;
half_step[i] = 1;
}
if (last_angle[i] < initial_angle[i] && orbit_angle[i] > initial_angle[i]) {
if (half_step[i] == 0) {
if (orbitN[i]==0) {
last_t[i] = t;
running_total[i] = t;
} else {
running_total[i] += t - last_t[i];
last_t[i] = t;
}
orbitN[i]++;
average_period[i] = running_total[i] / (DAYS_TO_SECS * orbitN[i]);
half_step[i] = 1;
} else if (half_step[i] == 1) {
half_step[i] = 0;
}
}
last_angle[i] = orbit_angle[i];
return average_period[i];
}
and this function is called in main like so:
for (double j = 0; j < max_time; j += timestep) {
update_positions(bodies, Nbodies, j);
for (int i = 0; i < Nbodies; i++) {
average_period[i] = calc_period(bodies, j, Nbodies, i);
if (j > max_time - timestep) {
printf("%s average period: %lg\n", bodies[i].name, average_period[i]);
}
}
}
and the problem that I'm having is that of course when the calc_period function finishes, the variables within are destroyed, so it cannot remember what initial_angle, last_angle or last_t were, so doesn't work. However I'm struggling to come up with a solution for this. If anyone can give any guidance it would be much appreciated.
Any data in the stack is volatile within function calls. The space in the stack you are using now will be replaced on future function calls, so functions on the stack should be used just until the function ends.
For your problem different methods exists.
You can create a struct as a global variable which will be stored in BSS, you can create a global pointer pointing to your struct on dynamic memory, allocated with functions like malloc().
Also you can create a static variable so on future calls to that function you can use it.
The best solution would be either defining a static pointer to malloc() where your struct resides, or doing the same on a global variable as a pointer.

Tools to expand or inline sub-functions in C-code

Background:
Often I have to deal with C source code without any documentation during firmware development/maintenance. When the code deals with big data structures where the data members initialization scattered all over the place, it becomes very challenging to jump around different source and header files and often I got lost.
So my current solution is to spend several days to "flatten out" the code. I don't know what the appropriate term, but basically what I do is expanding the sub-functions and replacing the input parameters variable names with the actual variable names that passed into the sub-function. (Please see an oversimplified example below)
The outcome of this exercise is very helpful, but it is a very manual and tedious process. Since I'm doing firmware, on some chips that supports trace capability, it is possible to do this using JTAG debugger. But this set up is not always available.
I know with macros I can use the preprocessor to do this, but I couldn't figure out how to do it with sub-functions, if such a tool exists. I tried Googling 'subfunction expansion' etc., so far no luck. Please let me know if you know such a tool exists or the proper term(s) of what I'm trying to do so I can search it better on the Internet. Thanks a lot!
int myGlobal;
void increment(int input)
{
input++;
}
void decrement(int input)
{
input--;
}
int doSomething(int input, int op)
{
if (op)
{
increment(input);
}
else
{
decrement(input);
}
}
int main(void)
{
int i, currMax;
int myOp = 1;
myGlobal = 0;
currMax = 5;
for (i = 0; i < currMax; i++)
{
doSomething(myGlobal, myOp);
}
currMax = 4;
myOp = 0;
for (i = 0; i < currMax; i++)
{
doSomething(myGlobal, myOp);
}
}
BECOMES
int main(void)
{
int i, currMax;
int myOp = 1;
myGlobal = 0;
currMax = 5;
for (i = 0; i < currMax; i++)
{
// doSomething(myGlobal, myOp);
if (myOp)
{
// increment(myGlobal);
myGlobal++;
}
else
{
decrement(myGlobal);
myGlobal--;
}
}
currMax = 4;
myOp = 0;
for (i = 0; i < currMax; i++)
{
// doSomething(myGlobal, myOp);
if (myOp)
{
// increment(myGlobal);
myGlobal++;
}
else
{
// decrement(myGlobal);
myGlobal--;
}
}
}

Need help understanding logic of function

monthly->maxTemperature = yearData[i].high;
monthly->minTemperature = yearData[i].low;
I just can't seem to understand the logic of what the iterations will look like or how to access the proper elements in the array of data to get the proper data for each month.... without corrupting data. Thanks!
You're on the right track:
void stats(int mth, const struct Data yearData[], int size, struct Monthly* monthStats)
{
// These are used to calc averages
int highSum = 0;
int lowSum = 0;
int days = 0;
// Initialize data
monthly->maxTemperature = INT_MIN;
monthly->minTemperature = INT_MAX;
monthly->totalPrecip = 0;
for (int i = 0; i < size; ++i) {
// Only use data from given month
if (yearData[i].month == mth) {
days += 1;
if (yearData[i].high > monthly->maxTemperature) monthly->maxTemperature = yearData[i].high;
if (yearData[i].low < monthly->minTemperature) monthly->minTemperature = yearData[i].low;
highSum += yearData[i].high;
lowSum + yearData[i].low;
monthly->totalPrecip += yearData[i].precip;
}
}
if (0 != days) {
monthly->avgHigh = highSum / days;
monthly->avgLow = lowSum / days;
}
}
Before working on the assignment it's a good idea to examine the API that you need to implement for clues. First thing to notice is that the reason the struct Monthly is passed to your function by pointer is so that you could set the result into it. This is different from the reason for passing struct Data as a pointer*, which is to pass an array using the only mechanism for passing arrays available in C. const qualifier is a strong indication that you must not be trying to modify anything off of the yearData, only the monthStats.
This tells you what to do with the min, max, average, and total that you are going to find in your function: these need to be assigned to fields of monthStats, like this:
monthStats->maxTemperature = maxTemperature;
monthStats->minTemperature = minTemperature;
...
where maxTemperature, minTemperature, and so on are local variables that you declare before entering the for loop.
As far as the for loop goes, your problem is that you ignore the mth variable completely. You need to use its value to decide if an element of yearData should be considered for your computations or not. The simplest way is to add an if to your for loop:
int maxTemperature = INT_MIN; // you need to include <limits.h>
int minTemperature = INT_MAX; // to get definitions of INT_MIN and INT_MAX
for(int i = 0; i<size; ++i) {
if (yearData[i].month < mth) continue;
if (yearData[i].month > mth) break;
... // Do your computations here
}
* Even though it looks like an array, it is still passed as a pointer

Initialize an "eye" (identity) matrix array in C

int eye[3][3] = {
{ 1,0,0 },
{ 0,1,0 },
{ 0,0,1 }
};
Is there a shorter way to initialize it? It's so regular that there must be a smarter way to initialize it, especially if it's more than 3x3, say 10x10 or more.
In c99 you can write:
int eye[][3] = { [0][0] = 1, [1][1] = 1, [2][2] = 1 };
all other elements are zeroed, moreover the compiler figures out the size of the array for you. Just don't skip the second size (3).
Btw. in your code you don't have to use the double braces, this would be fine too:
int eye[3][3] = {
1,0,0,
0,1,0,
1,0,1,
};
In c99 you can also leave the trailing comma, just for symmetry and future refactorings
Other solutions probably require you to write some code, which may indeed save you some time/space in file. But note that this way you're splitting declaration and "initialization", which in case of e.g. globals can make a difference.
You can use designated initializers:
int eye[3][3] = { [0][0]=1, [1][1]=1, [2][2]=1};
All the other elements will be initialized to 0 as per C standard's guarantee.
You may try the following:
#define SIZE 3
int eye[SIZE][SIZE] = {0};
for (int i = 0; i < SIZE ; ++i)
{
eye[i][i] = 1;
}
If you want to store {{ 1,0,0 }, { 0,1,0 }, ...} this style of values in square matrix means, you can write a simple logic as below.
#define SIZE 3
int eye[SIZE][SIZE] = {0};
int *p = (int *)eye;
for (i = 0; i < (SIZE * SIZE); i = i + (SIZE + 1))
{
p[i] = 1;
}
or
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (i == j)
{
eye[i][j] = 1;
}
else
{
eye[i][j] = 0;
}
}
}
Note : Above logic is only for the sample value you have given. So try to find similar logic if your values are having some relation. If not so, then no other way to initialize it directly even if size of matrix is 1000x1000.

const argument changes after array definition

I have what I consider a really strange problem. I have a function with the following prototype:
void generateNodes(const int maxX, const int maxY, node nodes[]);
As one of the first things in this function I define a 2d array of shorts, which i use as boolean values. But when I call this function the value of maxY changes to a large value. The code in question is below:
void generateNodes(const int maxX, const int maxY, node nodes[]){
int i, currentX, currentY;
short used[MAX_NODES][MAX_NODES];
//Generate the nodes
for(i = 0; i < MAX_NODES; i++){
currentX = randomNumber(0,maxX);
currentY = randomNumber(0,maxY);
nodes[i].color = 0;
nodes[i].numberOfConnections = 0;
nodes[i].id = i;
nodes[i].distanceFromStart = NOT_SET;
nodes[i].parent = NULL;
if(!used[currentX][currentY]){
nodes[i].x = currentX;
nodes[i].y = currentY;
used[currentX][currentY] = 1;
} else {
i--;
}
}
int numberOfConnections, j, currentNeighbor;
//Generate the connections
for(i = 0; i < MAX_NODES; i++){
numberOfConnections = randomNumber(1,5); //Between one and five outgoing connections
for(j = 0; j < numberOfConnections; j++){
currentNeighbor = randomNumber(0,19); //Select the neighbor
while(currentNeighbor == i){
currentNeighbor = randomNumber(0,19); //Try again while the selected is self
}
nodes[i].canReach[++(nodes[i].numberOfConnections)] = &nodes[currentNeighbor];
nodes[currentNeighbor].canReach[++(nodes[currentNeighbor].numberOfConnections)] = &nodes[i];
}
}
}
MAX_NODES is defined to 20.
Does anyone know why this might happen?
Very probably the code in ... is accessing beyond the end of used, causing arguments to be smashed. Without the code, it's of course impossible to say.
Since you do not seem to initialize the array used, it may well be that some elements are considered used (!= 0), since an array on stack is not initialized to zero, but takes whatever was in that memory area before.
An if an X,Y pair is considered used, you decrement the loop counter, possibly beyond zero into the negative realm, possibly overwriting - on the next iteration - part of the stack. This may also change the parameters, since they also reside on the same stack, before the local array.
Start with initializing used, and consider rewriting the loop to not change the loop variable except in the for statement.

Resources