Solved

What is a good solution to prevent duplicate variables?

Posted on 2016-10-31
8
97 Views
Last Modified: 2016-11-10
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
Comment
Question by:naseeam
8 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41867132
I don't think we have any solution to restrict same variable names.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 41867150
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
 
LVL 34

Expert Comment

by:sarabande
ID: 41867163
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:phoffric
ID: 41867610
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
 
LVL 34

Accepted Solution

by:
sarabande earned 250 total points
ID: 41868166
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
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 41869480
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 153
Using ANSI C how to Read a .csv file 10 110
How to translate this 2-line while loop into C from Perl? 8 134
Resolve Dependency Issues 4 85
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question