• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 118
  • Last Modified:

What is a good solution to prevent duplicate variables?

Our language is embedded 'C'.  

We have global static unsigned int variable called vehicle_speed.  A lower level function is called to fetch vehicle speed and assign to this variable.

We are adding a new software module that also needs vehicle speed.  In a new file, I can create another global static unsigned int variable called vehicle_speed_motor2.  Then, I call same low level function to fetch vehicle speed and assign it to vehicle_speed_motor2.

To avoid Two variables, I can create global variable but we think that would be too visible.

Are there any other solutions?
0
naseeam
Asked:
naseeam
2 Solutions
 
Pawan KumarDatabase ExpertCommented:
I don't think we have any solution to restrict same variable names.
0
 
ZoppoCommented:
Hi naseeam,

I'm not sure if I really understand your problem, especially I don't know what you mean with 'I can create global variable but we think that would be too visible'.

Why don't you use a real global variable? A variable declared as static in global scope is only global within the file in which it is declared, but if you declare it without static it is really global and can be accessed in other files too. So you could use one variable variable i.e. like this:
// file a.c
unsigned int vehicle_speed = 0;

// file b.c
extern unsigned int vehicle_speed;

Open in new window


ZOPPO
0
 
sarabandeCommented:
Adding to Zoppo's answer:

yes, you may use the same static unsigned int variable vehicle_speed in your second module by declaring it as extern.

//module2.c
...
extern int vehicle_speed;

Open in new window


the above also can be put into a header file.

variables defined as 'extern' may not initialized. there msut be one and only one c (or cpp) file where they were defined without extern. here you always should have an initialization.

another way to access global variables is to use a static and global GET function.

//global.h

static int  GET_VEHICLE_SPEED(int * pi);


//global.c
...

int  GET_VEHICLE_SPEED(int * pvs)
{
      static int vehicle_speed = 180;
      if (pvs != NULL)
      {
            vehicle_speed = *pvs;
      }
      return vehicle_speed;
}

Open in new window


you could get the variable everywhere from your project by calling GET_VEHICLE_SPEED(NULL);

you could assign a new value by calling

{
    int vs = 200;
    GET_VEHICLE_SPEED(&vs);
}

Open in new window


note, if using a c++ compiler, you could put the static get function into a class as a static member function. and you could return a reference to int what spares the pointer Argument.

Sara
0
Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

 
phoffricCommented:
Not so, unless embedded C is different w.r.t. static qualifiers than ANSI C. In ANSI C, the program in the first code of the accepted post would not build.
0
 
sarabandeCommented:
phoffric is right.

the following builds:

//global.h
#ifndef GLOBAL_H
#define GLOBAL_H

#include <stdio.h>
#include <conio.h>

int  GET_VEHICLE_SPEED(int * pi);

#endif

Open in new window


//tmain.c


#include "global.h"

int  GET_VEHICLE_SPEED(int * pvs)
{
      static int vehicle_speed = 180;
      if (pvs != NULL)
      {
            vehicle_speed = *pvs;
      }
      return vehicle_speed;
}

int f();

int main()
{
    int vs = GET_VEHICLE_SPEED(NULL);
    printf("tmain.c:  vs = %d\n ", GET_VEHICLE_SPEED(NULL));
    vs = 200;
    GET_VEHICLE_SPEED(&vs);

    f();
    return 0;
}

Open in new window


//tsecond.c

#include "global.h"

int f()
{
    int vs = GET_VEHICLE_SPEED(NULL);
    printf("tsecond.c: vs = %d\n", vs);
    return vs;
}

Open in new window


in ansi c static functions only can be used in the module where they are defined.

Sara
0
 
phoffricCommented:
Preventing duplicate variable names is a standard problem.

Since C-modules may represent some entity, such as a service or object having properties and state (as in a highly watered-down OOD), it is sometimes beneficial to add a short prefix (i.e., 2-3 chars) before global static variable names, where the prefix is related to the module. (And, BTW and OT, in a multi-threaded environment, we also prefix them with a special letter so that code reviewers will pay extra attention to them to make sure that all code references to them will not be subject to race conditions.)

The prefix isn't needed, of course, but it can be useful in the debugger when you are monitoring both global static variables to see the distinction clearly. And this is especially true if functions from both modules may happen to be concurrent in a multithreaded program.

Just to make sure we are on the same page, in ANSI C, two identical global static unsigned int variables both named vehicle_speed in two separate modules are distinct variables; that is, there will be two separate memory locations for these two variables; and their scope will be only in their respective module. Changing vehicle_speed in one module will not affect the value of vehicle_speed in the other module since global static as denoted by the ANSI-C qualifier, static, means that the vehicle_speed in one module is only known to that module from its definition point and downward (assuming no extern declaration exists higher up in that module).


Caveat: Although I have worked with ANSI C in embedded environments, I have not worked with the "Embedded C" language, so, this ANSI C information may possibly not apply to you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now