Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Translating Global Variables as Structs?
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
deltatux




PostPosted: Sat Nov 29, 2008 3:42 pm   Post subject: Translating Global Variables as Structs?

Hey guys,

Apparently I made a poor design choice by declaring a global variable for my whole program and need to now "privatize" it. Would a good idea just be to translate them into structs by putting them into them and then just tell my code to just call them up whenever needed instead of going through the whole software and pass variables all over the place?

deltatux
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Sat Nov 29, 2008 3:57 pm   Post subject: RE:Translating Global Variables as Structs?

It's generally bad if you need to access something globally. So unless this is one of those rare exceptions (and it's almost certainly not) you should rewrite your code so that you don't need global variables. Or have to pass a struct to everything.
wtd




PostPosted: Sat Nov 29, 2008 5:25 pm   Post subject: RE:Translating Global Variables as Structs?

Are the variables that are currently global related? If so, you may want to use a struct. Otherwise, just pass them to functions separately.
deltatux




PostPosted: Sun Nov 30, 2008 2:52 pm   Post subject: RE:Translating Global Variables as Structs?

Well, the whole software only really rely on 7 variables (unless they are just temporary variables) so to reduce the number of lines of code, I thought it was a good idea to declare them as global variables. However, I forgot about the fact that it may lead to code security issues. Thus, I got marks docked off.

However, I'm not too sure how to rewrite the program to not use global variables without a complete rewrite by having variables being passed since it's going to use up a huge amount of time to do and which I don't have since final exam is around the corner.

How would I go about using structs to solve this problem if I was to use structs?

Thanks,
deltatux
deltatux




PostPosted: Tue Dec 02, 2008 9:50 pm   Post subject: RE:Translating Global Variables as Structs?

Alright, I quickly redid the whole program doing the old slow traditional passing method... thanks anyways guys.

xboxrulz
md




PostPosted: Tue Dec 02, 2008 11:30 pm   Post subject: RE:Translating Global Variables as Structs?

The old slow method isn't really slow, and it's a much better way of writing code Wink
Zeroth




PostPosted: Wed Dec 03, 2008 12:46 am   Post subject: Re: Translating Global Variables as Structs?

Actually, in most languages, its faster Wink I'm not sure about C, but most languages, its faster to access local variables than global. The way it usually is, is local->outer scope->global.
deltatux




PostPosted: Wed Dec 03, 2008 1:52 am   Post subject: RE:Translating Global Variables as Structs?

I agree it's much better to write it, but it's so much more typing =P.

Anyways, what's done is done now Smile!

deltatux
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: