Hello guys I am making a kind of snake game where you hit a piece of fruit your score gets incremented but I still have to implement the snakes body
anyway I have added a new feature which is a time bonus when the score is in multiples of 10 you get 5 seconds to hit a bonus fruit but I used 2 seconds for debugging,anyway when I comment out the printTimer() function the code works fine but when I try to print the timer with the function the program crashes,
is it because it ran out of memory or is it something wrong with my logic,please bare with me guys my codes pretty long and just having fun :)
when I comment this out
1 2 3 4
if(score == 2 && !timeUp){
printTimer(); // WHEN COMMENTED OUT CODE WORKS FINE
}
As you mentioned, inlining the code from printTimer() to where you call it (you're only calling it from one place anyway) should fix your current runtime crash, but it is not really a thorough solution...
I didn't personally run your code, but instinct tells me that your struct tm *theTime is most likely being dereferenced before it has been initialized.
If you're coming from a C background, think of all global variables in C++ as having an implicit "static" prefix because that's what they're treated as. The compiler is initializing theTime to zero, which is the same thing as setting it to NULL.
What I see is essentially just C code with a few ostream << operators used. If you're writing in C++ then use the functionality in <chrono> rather than <ctime> (unless you specifically need something from ctime). Encapsulate global variables in an object or you'll have to let them be constrained to some local scope.
Bottom line, get rid of your globals, or at least initialize them as part of their declaration.
I must clarify that you can only initialize static variables once and once only per program, whether it's C or C++. The only reason why this should work here is that this program is not split into separate compilation units (i.e. everything's in one file).
A missing return type is undefined behaviour, so anything can happen.
yeah I got rid of the printTimer function
Why go so far, just make it void should work.
I didn't personally run your code, but instinct tells me that your struct tm *theTime is most likely being dereferenced before it has been initialized.
Good point.
It's always good to check if a pointer is not null.
Try this.
> also wondering how my printTimer() function is causing a crash
Get into the habit of compiling C++ code with
a. specifically demanding standard conformance to the extent possible
(this is particularly important for GNU/GNU compatible compilers)
b. enabling (almost) all warnings
(-Weverything with clang++ and -Wall with Microsoft generate a lot of noise; so you may want to avoid these.)
For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <string>
std::string printTimer() {
// ...
// no return statement
// GNU: *** warning: no return statement in function returning non-void (ok: we are warned)
// LLVM: *** warning: control reaches end of non-void function (ok: we are warned)
// Microsoft: *** error: 'printTimer': must return a value (excellent: generate an error if the code is clearly erroneous)
}
int main() {
}
thanks guys very good points,I edited and tidied it up a little it works fine albeit quite laggy and buggy but its working so its a start,
anyway I noticed every time I call the initTimer() function I'm guessing a new block of memory is assigned to theTime?
so I try to save myself some memory and delete that block of memory before it is assigned to a new block of memory but when I do this the game crashes,why is this?
also JL that is interesting it makes sense that the compiler would throw an error if a return type was not made I wonder why the compiler I use mingW doesn't implement this as an error as opposed to a warning
I know also it is not a good idea to have so many globals well actually globals at all but I will tidy this up soon
I noticed every time I call the initTimer() function I'm guessing a new block of memory is assigned to theTime?
No, that's not what localtime does. It returns, to quote http://en.cppreference.com/w/cpp/chrono/c/localtime , a pointer to a static internal std::tm object on success, or null pointer otherwise. That pointer is what you're assigning to theTime
when I do this the game crashes,why is this? delete theTime; // source of crash
You are not supposed to delete the pointer returned from localtime. It probably points to some static variable. NEVER EVER delete a pointer until
a) you allocated it
or
b) the documentation of the function tells you to do it.
BTW Why don't you learn about classes? This type of game easily leads to an object-oriented design.
I thought localtime() returned a object because we declare tm* theTime as a pointer so I thought we would have implemented it the same way as if we called the new keyword
and thanks Thomas very good point,I actually know a bit about OOP coming from a Java background and use it sometimes but for small projects I prefer to use a more functional style(probably not good practice)
Though with the LLVM compiler, it does crash at run-time.
I suppose the GNU crowd thought that it would not be politically expedient to generate an error for undefined behaviour such as this, and immediately make a massive chunk of open source code un-compilable.
I suppose the GNU crowd thought that it would not be politically expedient to generate an error for undefined behaviour such as this, and immediately make a massive chunk of open source code un-compilable.