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

  • SEARCH
  • Home
  • 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 8671203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:53:31+00:00 2026-06-12T18:53:31+00:00

I am using a global array (I know they are evil). This is not

  • 0

I am using a global array (I know they are evil). This is not so much a problem, but I don’t know why it works one way and not the other, as they should be the same. (that and otherwise I need to deallocate memory after in the currently working way).

Anyway, I have this Snake class. I want to make, this being the global array:

Snake snakes[8];

I have several (or will anyway) several classes and function that interact with this. The problem in question occurs with a setter function in the Snake class. It compiles fine, but segfaults. It turned out, this was a null pointer (0x0). Not sure why. The stack trace in this case came from a function call from another file, which includes the snake header and has this:

extern Snake * snakes;

An array’s name is a pointer, so I figured this should work. Oddly enough, it does not. I have no idea why.

But, when I change the declaration from what it was to:

Snake * snakes;

And later allocate it like so:

snakes = new Snake [8];

It works! But, then I need to deallocate something else I didn’t want to, when the number of snakes is constant. (8).

Also, the function call in question accesses the 0th element, where there were 8 elements each time.

Any idea what causes this?

On a side note, are global variables still evil when in a named namespace? (not anonymous). I ask because I am trying to get into the OO habit and logically structure things, which I am certain will pay off compared to my last endeavors, the encapsulation, organization, etc, all make sense. (and maybe in a few years, compile time). Still, having to include a scoped variable in a function call for setting seems… bad. Is that proper or is there a better way? (then global, namespace or otherwise)

EDIT: The error is a segfault, at the line of the member function.

Snake member function:

void setValue(Some value here...) {this-> value = input)

The call from the file that externed the global array:

snakes[0].setValue(some value here...)
  • 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-12T18:53:33+00:00Added an answer on June 12, 2026 at 6:53 pm

    An array’s name is not a pointer. In most contexts it decays into a pointer, but when it doesn’t, it doesn’t. So if you have:

    Snake snakes[8];
    

    in one source file and

    extern Snake *snakes; // don't do this
    

    in another, you’ll get all kinds of mysterious problems, including crashes. For an array, the extern declaration must declare an array:

    extern Snake snakes[];
    

    Incidentally, I know this because I had exactly the same problem many years ago, and that’s when I figured out how to do this.

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

Sidebar

Related Questions

I don't really know much PHP. But anyways I am not able to upload
I don't really know much PHP. But anyways I am not able to upload
I'm using this approach to store data in a global array hosting an http
I know how to pass a variable from one activity to another using global
I always hear using global variables are dangerous. Does this apply to Drupal? Take
Isn't there another way of using global variables in a Java stlye manner in
I've tried using a global hook but the hookprocedure only recieved window procedure messages
I'm using a global keyboard hook class. This class allows to check if keyboard
Is there a way to handle the application_start event without using the global.asax? The
I am using this code: def startThreads(arrayofkeywords): global i i = 0 while len(arrayofkeywords):

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.