Solved

What is a good solution to prevent duplicate variables?

Posted on 2016-10-31
8
64 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 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
I don't think we have any solution to restrict same variable names.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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 32

Accepted Solution

by:
sarabande earned 250 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now