Okay, I'm sorry. It seems that Visual Studio just goes crazy and the values are REALLY fine. As to why this happens and when, I have no idea.
There is a complete sample at the end, I'd like to know if it does this crazy thing for you as well. Try it with x86 and x64 builds and look at what the debugger says the value is.
Maybe this is a good thing though, now I won't miss needless copying.
I find this really odd. Whenever I copy a glm::i64vec2 it gets corrupted.
But if i do just this line:
glm::i64vec2 copy(glm::i64vec2(1, 1));
In my main, it just works.
I have deleted all object files and rebuilt the program from scratch.. any ideas what might be wrong?
The only one I can think of is that I have included headers in different order in different places and that somehow messed this up.
What I really should use here is const reference, but still this scares the crap out of me. One of these could easily stay hidden as a bug and cause lots of headache for me.
I just found another bug this caused. This time it happens inside a single class.
This fails, but the same works in main
bool test(glm::i64vec2 p_worldPos)
{
return p_worldPos.x == 1;
}
void QuadTree::getObjectsHitInPriority(glm::i64vec2 const & p_worldPos, std::vector<Object*> & p_objects)
{
test(p_worldPos);
Um, I don't know what this means, but the following observation has been made. In my program main.cpp this fails
#include <glm/glm.hpp>
#include <iostream>
struct whatthehell
{
bool test(glm::u64vec2 p_test)
{
return p_test.x == 1;
}
};
int main(int argc, char ** argv)
{
whatthehell hell;
if (hell.test(glm::u64vec2(1, 1)) == false)
{
std::cout << "What the hell\n";
}
return 0;
}
Ok, update. This works with x86 but not on x64 with my VS2015. Can anyone test this?
Related
I've been attempting to figure out this bug for about an hour now. It's probably a really obvious syntax thing I'm overlooking. This is my first C++ project, and I don't have a good handle on the structure of the language.
Here's my header file:
#pragma once
#include <vector>
class BoardState
{
private:
std::vector<int> numbers;
int SIZE;
public:
BoardState();
std::vector<int> getState();
bool isZero();
};
And here's the implementation, in a separate file:
#include "BoardState.h"
BoardState::BoardState(){
SIZE = 4;
numbers.push_back(1);
numbers.push_back(3);
numbers.push_back(5);
numbers.push_back(7);
}
std::vector<int> BoardState::getState() { return numbers; }
bool BoardState::isZero() {
for (int i = 0; i < numbers.size(); i++) {
if (numbers[i] != 0) { return false; }
}
return true;
}
This code is really simple, so I have no clue what could be going wrong to produce the errors. However, on every method call, push_back and size, I am getting errors, saying that class "std::vector<int, allocator>" has no member "method_name_here".
My background is Java, so my first thought was that I wasn't able to call these methods because numbers is not initialized. However, any attempt I made to initialize numbers in the header file resulted in an error as well. I tried std::vector<int> numbers = { 1,3,5,7 };, I tried std::vector<int> numbers(4,0);, I even tried creating an array and constructing the vector from that. Not only did all those attempts cause errors, they also didn't fix the method calls either.
What am I missing? Do I need to initialize the vector, or is what I have in the header file enough? Any advice would be helpful here, since I can't find anything online about similar errors. I've even copy-pasted code from StackOverflow answers about similar problems, and that produced errors as well.
EDIT: I've pared down the code as much as possible while keeping the error:
#include <vector>
class BoardState
{
std::vector<int> numbers;
BoardState() { numbers.push_back(1); }
int getSize() {
int i = numbers.size();
return i;
}
};
On the line numbers.push_back(1);, my compiler underlines the token "push_back", and highlighting it reads:
class "std::vector<int, allocator>" has no member "push_back"
On the line int i = numbers.size();, the token "size" is underlined, and the error reads:
class "std::vector<int, allocator>" has no member "size"
I still have no clue what's going on.
Edit 2: Put the method calls into a constructor and a function. This changed the error message associated with push_back().
Edit 3: I have discovered something very disconcerting. This code works perfectly fine in a different compiler. I copy-pasted in the exact code from Edit 1 and it ran with no issues. I think the problem must be with Visual Studio rather than the actual code. Thank you all for helping me out with this. I think I'm just going to switch to a different compiler and hope for the best.
Edit 4: Just to prove to pm100 that my code is exactly as I've said, here's a screenshot from visual studio.
Here it is.
Aside from the main method, this is character-for-character what I've put in this question. I have a guess as to why this doesn't work, though. I modified my version of Visual Studio 2019 to run .386 assembly code for a college class. While I think I followed the guide to do that without affecting anything else, it may have screwed up parts of the C++ compiler.
I suggest that you could select Tools->Import and Export Settings->Reset all settings-> Visual C++ to restore the default settings.
If it does not work, you could reinstall VS.
int listenPort()
{
//if (server)
//{
// return server->port();
//}
//std::cout << server->port() << std::endl;
//return 0;
//add below 2 lines only to make it work right under Realease.
//std::fstream f("Z:/fsfasjlfjal.txt");
//f.close();
if (_listenPort != -1)
{
return _listenPort;
}
return 0;
}
I have one function named listenPort, variable _listenPort has been set to -1 in construct function, I want to check its value. When it changes return it or return 0.
I use Visual Studio 2010 to compile the code, DEBUG everything is OK. But when I change to Release(/O2), function always return 0. I tried add two lines code: fstream open and close. Now it seems everything is right.
But this solution is ugly, I just open and close some file. What should I do? Thanks.
One not recommended solution is to make replace int _listenPort; with volatile int _listenPort;. Read this to understand why this solution is not recommended.
A good solution would use synchronized writing and reading of _listenPort.
Or As I suggested before move definitions of class to a different file. This way, compiler won't inline your code and function will return expected value.
You're probably trying to run a debugger on an optimised (/O2) code and set breakpoints in your function. This is not going to work. Compiler is free to change order of your code as it sees fit, provided that the outcome of the code is the same as if it were untouched.
If you really want to test some values with optimised code, you need to log the values somewhere (a file) or print them out in the console.
Okay, probably a dumb question to you guys but I can't figure it out.
So I'm taking a c++ basics course in class and so far I'm struggling/crying.
I can't show you guys my code because I'm not allowed/there are consequences if I'm caught but I could probably give a example.
I'm using xcode. So when I compile, I get two errors below (image provided).
I searched for similar questions, but those seem too complex compared to what I'm doing. In addition, the only includes I have are iostream and string.
I know the problem occurs when I add an if statement in my main function. I know this because when I delete it, everything compiles as expected. Yet when I add it again to the main function, these errors occur.
So my question is, based on what I know, is it proper to add an if statements whenever in the main function?
Below is an example. I wrote the functions below and called above.
#include <iostream>
#include <string>
using namespace std;
// example functions that I just made up to explain the structure of my actual code.
//Don't bother trying to understand it. It's just to explain that
//I wrote my functions at the
// bottom and called it at the top.
int getNumberofWins(param1, param2);
string getTheName(int player1);
int executeCycle(string p1_name, string p2_name);
void stateWinner(string winner_name);
int main {
playerOne = getTheName(1);
playerTwo = getTheName(2);
r1 = executeCycle(playerOne, playerTwo);
r2= executeCycle(playerOne, playerTwo);
totalWin1 = getNumberOfWins(1, r1, r2);
totalWin2 = getNumberOfWins(2, r1, r2);
cout << totalWin1;
//This is the where I get the errors. When I delete the if statement,
//Everything compiles. When I add it, an error occurs.
if (totalWin1 == 2){
stateWinner(playerOne);
}
return 0;
}
string getTheName(int player1){
string playerOne;
string playerTwo;
if(player_number == 1){ code code code
}
}
int getNumberofWins (int param1, int param2){
code code code
}
int executeCycle(string p1_name, string p2_name){
code code code
}
void stateWinner(string winner_name){
if(!winner_name.empty()){
code code code
}
I hope it's fine if the code above isn't accurate. I think the point is that once I add my if statement to the main function, the two errors show up.
actually...now that I look at it, they both seem like similar errors. I just don't know why they both appear...
Sorry if this is an obvious answer or if it isn't clear.
The "announceWinner" function is not defined anywhere, ie there's no
void announceWinner () {
// code
}
anywhere. Either you haven't written it yet, or the file that contains it is not being compiled & linked with the main program.
I'm getting errors in my program between two (theoretically) consecutive lines of code, and have no idea what could cause this.
My whole code is huge, so here's the basics;
int playRoom(std::string currentRoom = "Storage_room", std::string entryDoor = "NULL"){
log("Starting playRoom()");
// code to play the level
// includes setting up box2d world
// and playing the level
if(playerWantsRestart){
log("Restart level");
return playRoom(savedData.roomName, savedData.entryDoor);
}
log("Leaving playRoom()");
return 0;
}
int main( int argc, char* args[] ){
// Set up SDL etc..
playRoom();
log("Back in main()");
// Close SDL
return 0;
}
If I never use the restart option, everything is fine.
If I do use it, the program exits with status 3 and the log file reads:
Starting playRoom()
Restart level
Starting playRoom()
Leaving playRoom()
So the error appears to be in "return 0;"?? I don't think status 3 is an overflow, and it's only recursed (?) once anyway, so...
I'm using Codeblocks 12.11, compiling with GNU GCC. Any help or ideas would be great!
int playRoom(std::string currentRoom = "Storage_room", std::string entryDoor = "NULL")
Why are you using int's? I would use Boolean.
Either way read this.
You are calling two different types of methods not sure if they updated java to include this syntactic sugar yet. I've been out of the loop but.
playRoom();
This function should have a set currentRoom and an entryDoor in the method by default.
Then overload it with.
playRoom(string currentRoom , string entryDoor);
Try removing the return statement from the if condition.
I have several configuration flags that I am implementing as structs. I create an object. I call a method of the object with a flag, which eventually triggers a comparison between two flags. However, by this time, one of the flags has been overwritten somehow.
To clarify, here's a VERY simplified version of the code that should illustrate what I'm seeing:
class flag_type { unsigned int flag; /*more stuff*/ };
flag_type FLAG1
flag_type FLAG2
class MyObject {
public:
void method1(const flag_type& flag_arg) {
//conditionals, and then:
const flag_type flag_args[2] = {flag_arg,flag_arg};
method2(flag_args);
}
void method2(const flag_type flag_args[2]) {
//conditionals, and then:
method3(flag_args[0]);
}
void method3(const flag_type& flag_arg) { //Actually in a superclass
//stuff
if (flag_arg==FLAG1) { /*stuff*/ }
//stuff
}
};
int main(int argc, const char* argv[]) {
//In some functions called by main:
MyObject* obj = new MyObject();
//Later in some other functions:
obj->method1(FLAG1);
}
With a debugger and print statements, I can confirm that both FLAG1 and flag_arg/flag_args are fine in both "method1" and "method2". However, when I get to method3, "FLAG1.flag" has been corrupted, so the comparison fails.
Now, although I'm usually stellar about not doing it, and it passes MSVC's static code analysis on strictest settings, this to me looks like the behavior of a buffer overrun.
I haven't found any such error by looking, but of course one usually doesn't. My question isA: Am I screwing up somewhere else? I realize I'm not sharing any real code, but am I missing something already? This scheme worked before before I rewrote a large portion of the code.
B: Is there an easier way than picking through the code more carefully until I find it? The code is cross-platform, so I'm already setting it up to check with Valgrind on an Ubuntu box.
Thanks to those who tried to help. Though, it should be noted that the code was for clarification purposes only; I typed it from scratch to show generally was was happening; not to compile. In retrospect, I realize it wasn't fair to ask people to solve it on so little information--though my actual question "Is there an easier way than picking through the code more carefully" didn't really concern actually solving the problem--just how to approach it.
As to this question, on Ubuntu Linux, I got "stack smashing" which told me more or less where the problem occurred. Interestingly, the traceback for stack smashing was the most helpful. Long story short, it was an embarrassingly basic error; strcpy was overflowing (in the operators for ~, | and &, the flags have a debug string set this way). At least it wasn't me who wrote that code. Always use strncpy, people :P