SDL2 Threads C++ pointer corruption - multithreading

So, i have the following problem which may seem pretty strange or too elementary. This code snippet demonstrates my problem.
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include "SDL2/SDL.h"
#include <iostream>
using namespace std;
int doSTH(void* data){
int* data2 = (int*)data;
cout << data2 << endl;
return 0;
}
int main(){
SDL_Init(SDL_INIT_EVERYTHING);
int* data = new int(2);
cout << data << endl;
SDL_CreateThread(doSTH, "sth", (void*)data);
SDL_Delay(1);
delete data;
SDL_Quit();
}
Output is
0x2479f40
0x400c05
That means that somehow the function i call doesn't get the pointer i give it, am i missing something?
I am using Linux Ubuntu 14.04, g++ 4.8 and codeblocks.
Please tell me if i should give any more info.
Thanks in advance.

Nevermind, somehow the build of SDL2 was screwed up. I just uninstalled libx11-dev, rebooted and then reinstalled libsdl2-dev and now it works correctly.

Related

Question about this error: ‘bdget’ was not declared in this scope

trying to figure out why I’m getting this error about ’bdget’ (when on Linux Ubuntu, info below) and how to get rid of this error, maybe you could suggest some specific steps for me to try and/or ask me to provide you some additional info about the error, thank you for any help.
error: ‘bdget’ was not declared in this scope
b_dev = bdget(st.st_rdev);
The code (for a short sample/tester program put together quickly to demonstrate the problem here to you) that produces this error is below, here’s other info: When I comment out that ‘bdget’ line, the program (seen below) compiles, otherwise it doesn’t and produces the error. I understand from other stackoverflow.com questions that using bdget requires #include <linux/fs.h>, which I’ve done here as you can see in the code below.
I think this is my first question here at stackoverflow.com, haven’t yet learned what question-related actions I need to take here at stackoverflow.com about your answers such as accept, and the steps for taking those actions, so bear with me a bit.
Regards,
Yaman Aksu,PhD
The build used this g++ version (full path: /usr/bin/g++) :
g++ (Ubuntu 7.3.0-27ubuntu1-18.04) 7.3.0 <snip>
and was simple as follows:
g++ -Wall -03 tester.cpp -o tester
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <linux/fs.h>
#define FNABS_SIZE 1024
char prog[FNABS_SIZE];
using namespace std;
int main(int argc, char *argv[])
{
int rv=0;
struct dirent *de;
struct stat st;
struct block_device *b_dev;
char direc[FNABS_SIZE], fnabs[FNABS_SIZE], fnabs_bfile[FNABS_SIZE], hd_serial[NAME_MAX], hd_model[NAME_MAX];
int status;
unsigned int maj_rdev, min_rdev;
ios::sync_with_stdio();
strcpy(prog,argv[0]); argc--;
strcpy(direc,"/dev/disk/by-id");
for (DIR *p = opendir(direc); (de=readdir(p)); ) {
if (de->d_type==DT_LNK) {
sprintf(fnabs,"%s/%s",direc,de->d_name);
cout << "[glk] Now running 'stat' on this: " << fnabs << endl;
status = stat(fnabs, &st);
cout << "[glk] 'stat' results:" << endl;
cout << "[glk]\t return value:" << status << endl;
cout << "[glk]\t st.st_rdev:" << st.st_rdev << endl;
cout << "[glk]\t st.st_dev=" << st.st_dev << endl;
cout << "[glk]\t st.st_ino=" << st.st_ino << endl;
maj_rdev = major(st.st_rdev); min_rdev = minor(st.st_rdev);
printf("[glk]\t major:minor = %u:%u\n",maj_rdev, min_rdev);
if ((st.st_mode & S_IFMT) == S_IFBLK) {
printf("[glk]\t This is a 'block device' !!\n");
}
b_dev = bdget(st.st_rdev);
}
}
}

Uninitialized local variable 'state' used

So I want to create a program which allows users to map buttons to keyboard presses using c++ with Visual Studio 2015. I have been having a ton of trouble with Xinput and I was hoping someone could help me with one simple problem which makes no sense seeing as I have defined it.
So my problem is I get one error which says unresolved external symbol _XinputGetState#8 referenced in function _main.
Here is my code:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <Xinput.h>
using namespace std;
int main() {
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
if (XInputGetState(0, &state) == ERROR_SUCCESS)
{
cout << "It worked!" << endl;
}
bool A_button_pressed = ((state.Gamepad.wButtons & XINPUT_GAMEPAD_A) != 0);
cout << A_button_pressed << endl;
}
In general unresolved external symbols means that a library needed for the function is not linked.
In this case:
XInputGetState() requires XInputLib.lib and Xinput9_1_0.lib.
This can be resolved by adding the libraries in the project settings or via:
#pragma comment(lib,"XInput.lib")
#pragma comment(lib,"Xinput9_1_0.lib")

How to calculate the intersection of a line segment and circle with CGAL

I've been banging my head against a wall trying to understand how to use CGAL's Circular Kernel to calculate the intersection(s) between a line segment (Line_Arc_2) and a Circle (Circle_2). Unfortunately there isn't much in the way of example code for the Circular Kernel, and I'm not finding the reference manual much help.
Here is code that I thought would work, but right now it won't even compile (Mac OS 10.9 using the latest system compiler):
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2)>::type Res;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<Res> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}
I get an error on the result_of line: "error: no type named 'result_type' in...", and a second error that "no viable overloaded '='" is available for the intersection line.
Also, since this would probably be the follow up question once this is working: how do I actually get at the intersection points that are put in the vector? CGAL's documentation suggests to me "result" should contain pairs of a Circular_arc_point_2 and an unsigned int representing its multiplicity. Is this what I will actually get in this case? More generally, does anyone know a good tutorial for using the Circular Kernel and Spherical Kernel intersection routines?
Thanks!
So it seems that result_of doesn't work here, despite being suggested in the CGAL reference manual for the CircularKernel's intersection function.
Here is a different version that seems to work and can properly handle the output:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/iterator.h>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> IsectOutput;
using namespace std;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1.0,0.0), Pt2(0.0,1.0), Pt2(-1.0, 0.0));
LineArc2 l = LineArc2( Pt2(0.0,-2.0), Pt2(0.0,2.0) );
std::vector<IsectOutput> output;
typedef CGAL::Dispatch_output_iterator< CGAL::cpp11::tuple<IsectOutput>,
CGAL::cpp0x::tuple< std::back_insert_iterator<std::vector<IsectOutput> > > > Dispatcher;
Dispatcher disp = CGAL::dispatch_output<IsectOutput>( std::back_inserter(output) );
CGAL::intersection(l, c, disp);
cout << output.size() << endl;
for( const auto& v : output ){
cout << "Point: (" << CGAL::to_double( v.first.x() ) << ", " << CGAL::to_double( v.first.y() ) << "), Mult: "
<< v.second << std::endl;
}
return 0;
}
result_of is working but the operator you are asking for does not exist, you are missing the output iterator.
However, I agree the doc is misleading. I'll try to fix it.
The following code is working fine:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef boost::variant<std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> > InterRes;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2,std::back_insert_iterator<std::vector<InterRes> >)>::type Res;
int main(){
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<InterRes> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}

atof truncates fractional parts after QApplication

I created a qt application with qt creator, the code is like,
#include "mainwindow.h"
#include <QDebug>
#include <QApplication>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double before = atof("3.1");
double x;
sscanf("3.1", "%lf", &x);
QApplication a(argc, argv);
double after = atof("3.1");
double y;
sscanf("3.1", "%lf", &y);
MainWindow w;
w.show();
qDebug() << before;
qDebug() << after;
qDebug() << x;
qDebug() << y;
return a.exec();
}
the output is
3.1
3
3.1
3
That means sscanf and atof truncates fractional parts after "QApplication a(argc, argv);". The problem only occurs in Qt5.3 under Linux Mint 17. I tested the same program in windows 8 and Mac OS 10.9, they don't have the same problem. Is it a bug in Linux Qt5.3 or it has something to do with linux c library?
The complete code can be accessed here
See QCoreApplication documentation:
On Unix/Linux Qt is configured to use the system locale settings by
default. This can cause a conflict when using POSIX functions, for
instance, when converting between data types such as floats and
strings, since the notation may differ between locales. To get around
this problem, call the POSIX function setlocale(LC_NUMERIC,"C") right
after initializing QApplication or QCoreApplication to reset the
locale that is used for number formatting to "C"-locale.
I can reproduce your problem and the following code fixes it for me:
QApplication a(argc, argv);
setlocale(LC_NUMERIC,"C");

cout in Visual Studio 2010

When I attempt to compile the following
#include <iostream>
using namespace std;
#include "stdafx.h" // This was included by Visual Studio
int _tmain(int argc, _TCHAR* argv[]) // The name _tmain was generated by Visual Studio
{
int a = 1;
cout << a << "\n";
return 0;
}
I get a compiler message:
warning C4627: '#include <iostream>': skipped when looking for precompiled header use
Add directive to 'StdAfx.h' or rebuild precompiled header
Then I'm told that cout is undefined. (It doesn't help to write std::cout.)
I'm using a default Visual Studio projects. This is the first time I've used this. Suggestions appreciated.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a=10;
cout << a << "\n";
cin>>a;
return 0;
}
Slam dear! I have written the same code, but only changing the order of first three line. It gives result at console without any error or warning. Please check it.
Put your iostream include and the std namespace declaration after the stdafx.h include. The program will then compile and run.
As to why, my guess is that precompiled headers (enabled by default) rely on the exact sequence of #include directives. Putting iostream first means that the PCH for stdafx no longer matches the actual sequence of declarations known to the compiler at that point.

Resources