Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:01:32+00:00 2026-06-09T16:01:32+00:00

I know that global variables are bad and should be avoided as far as

  • 0

I know that global variables are bad and should be avoided as far as possible. However, when writing parallel programs with MPI for example, some variables only get initialised once, and never changed (This task’s number, total number of tasks etc.). Is it still considered bad practice to have these variables as global? Since you pretty much need access to these variables all the time, it seems silly to make a class for them in main, and pass a pointer to it to 99% of all the functions in the program. My approach so far has been to hide them in a namespace mpi_glob (for the case of C++, I guess I would not bother to put them in a struct in C to emulate a namespace). I guess the question also goes for other set-only-once variables such as a system of units etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:01:34+00:00Added an answer on June 9, 2026 at 4:01 pm

    The reason that global variables “are bad” is that they introduce coupling between separate source files that can be hard to identify and trace. In particular, if a global variable has an unexpected state at some point in the program, it can be hard to figure out where it got modified.

    Replacing global variables with local variables that get passed by reference to every function in the program doesn’t eliminate that problem. In design terms, it’s “tramp data”, i.e., data that wanders around even where it’s not needed.

    Now, the point here is that, as you suggest, data that’s initialized once and never changed doesn’t have the problems that make global variables “bad”. Since the problem doesn’t exist, the solution isn’t needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know global variables are supposed to be bad but is it possible to
Yes I know global variables is a bad practice, but ease up on that
I know that global variables are bad. But if I am using node's module
I know that, in Delphi, instance variables and global variables are initialized to zero
We all know that global variables are anything but best practice. But there are
First of all, I know global variables are evil :) However, there is legit
Possible Duplicate: Why is it bad to make elements global variables in Javascript? In
I struck a little problem when learning. I know that uninitialized global variables in
It's known that big local/global variables may cause to a stack overflow. I know
So I got a lot of responses from this site that global variables are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.