Posting issues

Recommended Posts

Forum members please forgive me as I've had a few problems trying to post a question with some code. Something about the code causes the forum to lock-up for me and my post to appear empty. I have saved the post in a txt file and placed it at http://home.comcast.net/~phantoms/temp/GameDev_post.txt If some of you could take a look at it there, I'd be greatful. And if anyone knows why it causes my GameDev.net session to lock-up, please tell. (And yes, I've cleared the cache, cookies, rebooted, etc.). -James

Share this post

Share on other sites
Hello,

Personally I do not know why who ever made that code declare the array global. It works perfectly fine by putting the array in the main function. Even if they had include a function that alters the data they could always have declare it in the main function and pass it along in the function as a reference or they could have made a pointer and done it like that. I am not sure, but I believe that by declaring it global will not effect it initialization at all. To my understanding it will still be uninitialized.

[opinion] Also, I feel that variables should not be global, unless they are const. [/opinion]

I hope someone else can come around and tell you the answer for sure.

~Carl J. Loucius

Share this post

Share on other sites
Quote:
 Original post by acddklr07I am not sure, but I believe that by declaring it global will not effect it initialization at all. To my understanding it will still be uninitialized.

Actually, I'm pretty sure that global variables will be initialized to zero by the compiler, whereas local variables will be full of stack garbage. The code he posted does not seem to initialize the hits array, so it seems to rely on this behavior.

Even thought the compiler does this, it may not be standard (maybe a guru can answer this), and it sure is a bad reason to make the array global.

Just make it local and initialize it to zero.

Share this post

Share on other sites
I tried to insert the post in a quote and it didn't work for me, either. Very strange.

Anyway, there's no reason to use a global here, and you shouldn't - just initialize the array.

int main() {  int hits[10] = {}; // should be all you need.  // etc...

Also, I'd like to point out that one of the calculations is incorrect:

static_cast<double>(hits[i]) / (n / 10);

Because this requests the 'n/10' division first, and 'n' is an integer, integer division will be performed for that step. This is not what you want - when n is less than 10, you'll end up dividing by zero.

As a general rule, do multiplications before divisions. This tends to improve accuracy (except of course where the multiplication result might overflow), and sometimes allows you to do micro-optimizations (which are not available to the compiler here because the semantics differ - due to the difference between integer and FP division). I propose a bit of algebraic rearrangement:

10 * hits[i] / static_cast<double>(n);

Share this post

Share on other sites
Quote:
 Original post by ZahlmanAnyway, there's no reason to use a global here, and you shouldn't - just initialize the array.

I didn't think so, but this is a beginning book and I figured it was done for simplicity. However, I am not interested in simplicity if it developes bad programming habits.

Quote:
 Also, I'd like to point out that one of the calculations is incorrect:static_cast(hits[i]) / (n / 10);Because this requests the 'n/10' division first, and 'n' is an integer, integer division will be performed for that step. This is not what you want - when n is less than 10, you'll end up dividing by zero.

I think this was another simplification (or rather assumption). It assumes the user will enter at least 10 and we all know what happens when you ass-u-me something. Thank you for pointing this out.

I know I'm just learning and I can understand a beginning C++ book will take some shortcuts, etc., but I do not want to pick up any bad habits if I do not have to. By the time I understood why that line was wrong, I would have probably been much further along and and the code much stricter. The Array thing though, I noticed right away. Do not declare anything globally unless it's required for a reason. I read in a couple chapters before on how global variables are initialized with 0, and local ones are with garbage. Then I remembered that while trying to figure out why it was being declared outside main.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

• Forum Statistics

• Total Topics
627700
• Total Posts
2978695

• 21
• 14
• 12
• 10
• 12