Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hiowf
Jun 28, 2013

We don't do .DOC in my cave.

No Gravitas posted:

import performance is pretty darn slow

Do you understand what transactions are, what autocommit is, and why it will kill your SQLite performance?

Adbot
ADBOT LOVES YOU

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

No Gravitas posted:

poo poo supervisor, poo poo budget, poo poo skills, poo poo politics and poo poo requirements.

This is all a given considering academia but what is the actual technical problem you are attempting to solve.

I feel there is a very good chance of an X/Y issue here so taking a step back to analyze what's actually going on is important.

SQLite is a very powerful tool and casting it aside is a very strong indicator that something's very wrong.

Ika
Dec 30, 2004
Pure insanity

I've been working with templates, and have a few questions: Is there a better way to work around the enclosing scope limit for objects captured by reference than what I am doing? Also, is there a way to capture the object pointed to by a variable by reference? I know creating a reference to a pointer object sounds weird, but currently I have lamdas shared between many functions which are setup for capture by reference which I don't want to duplicate for capturing the pointer by value.

For example

code:
class Foo
{
public:
	HRESULT DoSomething(__in const size_t size)
	{
		return S_OK;
	}
};

class Foo2
{
protected:
	Foo fooInstance;
	Foo *fooRef;
public:
	HRESULT Test(void)
	{
		Foo &fooInstance = this->fooInstance; // I want to eliminate this temp variable
		auto func = [&fooInstance](__in const size_t size) -> HRESULT{ return fooInstance.DoSomething(size); };
		return func(10);
	}

	HRESULT Test2(void)
	{
		Foo &fooInstance = *this->fooRef; // I want to eliminate this temp variable
		auto func = [&fooInstance](__in const size_t size) -> HRESULT{ return fooInstance.DoSomething(size); };
		return func(10);
	}
};
Alternatively is there a way to use std::addressof in the capture list so I don't need temp variables everytime I call the lambda. So something like this:

code:
	HRESULT Test3(void)
	{
		Foo fooInstance;
		Foo *fooRef = std::addressof(fooInstance); // I want to eliminate this temp variable
		auto func = [fooRef](__in const size_t size) -> HRESULT{ return fooRef->DoSomething(size); };
		return func(10);
	}

Praseodymi
Aug 26, 2010

How exactly does using the Android NDK to run C++ code on an Android device work? I thought it would just allow me to run a C++ program but reading about it seems like it's just used for running snippets and requires mainly Java code anyway, which left me thinking it would probably be really slow. Have I missed the point of it? Do you really need to code Android games in Java?

Hiowf
Jun 28, 2013

We don't do .DOC in my cave.

Praseodymi posted:

How exactly does using the Android NDK to run C++ code on an Android device work? I thought it would just allow me to run a C++ program but reading about it seems like it's just used for running snippets and requires mainly Java code anyway, which left me thinking it would probably be really slow. Have I missed the point of it? Do you really need to code Android games in Java?

You need a Java UI to interact with the user, because all of Android's UI system is in Java. That said, that Java UI doesn't need to do much more than throw up a Surface to draw upon and then pass that Surface back to C++.

Because you probably don't want your game to suck, you'll have to deal with a bunch more things in the Java code (like onPause/onResume at least), but that's the gist of it.

Technically you can also use the NDK to crosscompile pure console applications.

Praseodymi
Aug 26, 2010

So you only have to use Java for stuff that's unique to the device? Cool, how does SDL's touchscreen stuff work? Or is that actually for PC touch devices and still requires some abstraction for Android?

Hiowf
Jun 28, 2013

We don't do .DOC in my cave.

Praseodymi posted:

So you only have to use Java for stuff that's unique to the device?

For stuff that's unique to Android.

quote:

Cool, how does SDL's touchscreen stuff work? Or is that actually for PC touch devices and still requires some abstraction for Android?

It almost certainly contains a small Java shim that sends all the input events to native code.

FWIW:
http://hg.libsdl.org/SDL/file/704a0bfecf75/README-android.txt
(which unsurprisingly also has a part about dealing with onPause/onResume)

Hughlander
May 11, 2005

Praseodymi posted:

How exactly does using the Android NDK to run C++ code on an Android device work? I thought it would just allow me to run a C++ program but reading about it seems like it's just used for running snippets and requires mainly Java code anyway, which left me thinking it would probably be really slow. Have I missed the point of it? Do you really need to code Android games in Java?

You can code android games with various levels of Java depending on how much you want to rely on the first party APIs. The two you should look at are OpenGLActivity where you get a surface and a callback once per tick, and NativeActivity where you never see Java directly and even touch events come into the JNI interface.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Praseodymi posted:

How exactly does using the Android NDK to run C++ code on an Android device work? I thought it would just allow me to run a C++ program but reading about it seems like it's just used for running snippets and requires mainly Java code anyway, which left me thinking it would probably be really slow. Have I missed the point of it? Do you really need to code Android games in Java?

The NDK is horrible to work with, especially when you want to debug, and especially if you're not already familiar with Android's semantics. Write your stuff in Java and optimize it there; if you hit an actual case where you can't get acceptable performance, then consider narrowly using the NDK.

seiken
Feb 7, 2005

hah ha ha

Ika posted:

I've been working with templates, and have a few questions: Is there a better way to work around the enclosing scope limit for objects captured by reference than what I am doing?

Unless I'm mistaken, you just need to put this in the capture list, and then you can refer to the member variables directly.

Ika
Dec 30, 2004
Pure insanity

seiken posted:

Unless I'm mistaken, you just need to put this in the capture list, and then you can refer to the member variables directly.

Yes, but I'm trying to avoid that, because the lambda is defined in a shared macro and the class it operates on sometimes is a member variable and sometimes is on the stack or a function parameter. We can't modify the class itself for reasons. I'm not exactly sure why it is in a macrod lambda and not a static template function, it may have been to avoid multiple definitions of the same template function in a global unit.

Ika fucked around with this message at 16:52 on Aug 4, 2014

xgalaxy
Jan 27, 2004
i write code

Subjunctive posted:

The NDK is horrible to work with, especially when you want to debug, and especially if you're not already familiar with Android's semantics. Write your stuff in Java and optimize it there; if you hit an actual case where you can't get acceptable performance, then consider narrowly using the NDK.

This is great advice if your only concern is Android, but if you need multiplatform support then you will have to suck it up and live in the hell that is NDK. Or write your stuff twice - which honestly, if it is a small code base may be preferable to using the NDK - it's that bad.

seiken
Feb 7, 2005

hah ha ha

Ika posted:

Yes, but I'm trying to avoid that, because the lambda is defined in a shared macro and the class it operates on sometimes is a member variable and sometimes is on the stack or a function parameter. We can't modify the class itself for reasons. I'm not exactly sure why it is in a macrod lambda and not a static template function, it may have been to avoid multiple definitions of the same template function in a global unit.

You're going to need to post your actual code then because this is a completely different situation from the code in the original question. Can't you capture bar where bar is the member variable/stack variable/function parameter and access bar->foo inside the lambda?

Edit: if the problem is the distinction between dereferencing/not-dereferencing the thing you want to capture, then you could define a template like
C++ code:
template<typename T> struct foo_remove_ptr_t;
template<> struct foo_remove_ptr_t<Foo> {Foo& operator()(Foo& f) const {return f;}};
template<> struct foo_remove_ptr_t<Foo*> {Foo& operator()(Foo* f) const {return *f;}};
template<typename T> Foo& foo_remove_ptr(T& t) {return foo_remove_ptr_t<T>()(t);}
and use foo_remove_ptr(bar->member)

seiken fucked around with this message at 17:24 on Aug 4, 2014

The Gay Bean
Apr 19, 2004
I'm writing a program to solve for the elements of a translation matrix given two sets of associated (X, Y, Z) measurements. This problem naturally takes the form:

p_(frame 1)_i = T_(frame_1)_(frame_0)*p(frame_0)_i

Where T is a homogenous matrix and p is a point of the form transpose([x, y, z, 1])

You can then form a linear system and solve for the 12 rotational/translational elements of T.

This is just overconstrained linear least squares, so it can be solved with any gradient descent or Cholesky factorization, but it also comes with the implicit constraint that sqrt(T_i_1^2 + T_i_2^2 + T_i_3^2) = 1 for i = 1, 2, 3. However, that makes the problem nonlinear. My questions are:

1. Is it worth it to model the sqrt(T_i_1^2 + T_i_2^2 + T_i_3^2) = 1 for i = 1, 2, 3 constraint, given that it makes the problem nonlinear and I often get working results anyway?

2. If it is, what is the best open source C/C++ tool for modeling such things that you have come across?

3. I'm currently trying to use Ceres-solver (and running into a couple problems) with a cost function of the form:

residual = ||(Ax-b)*(1/covariances matrix)|| + abs(sqrt(T_11^2 + T_12^2 + T12^2)-1)*1/covariance + abs(sqrt(T_21^2 + T_22^2 + T_22^2)-1)*1/covariance + abs(sqrt(T_31^2 + T_32^2 + T32^2)-1)*1/covariance

with the covariance on the unit row constraints being relatively high. Does anyone see any issues with this model?

PS. Love the cabin
Dec 30, 2011
Bee Lincoln
Is there a way to call functions at an address including passing arguments without having to write a function prototype for each one?

b0lt
Apr 29, 2005

PS. Love the cabin posted:

Is there a way to call functions at an address including passing arguments without having to write a function prototype for each one?

Write an assembly stub to stick your arguments in the proper place for your calling convention. This requires you to know what the function prototype is.

Edison was a dick
Apr 3, 2010

direct current :roboluv: only

PS. Love the cabin posted:

Is there a way to call functions at an address including passing arguments without having to write a function prototype for each one?

As b0lt said, yes, but the alternative is worse.

You can use typedefs to make it less of a hassle though.

code:
typedef int (*flatulator)(int butt, int gas);

int fart(int butt, int gas){
    return butt * gas;
}

struct farter {
    flatulator flatulator;
    int butt;
}

struct farter *farter_factory(int butt){
    struct farter *farter = malloc(sizeof(struct farter));
    if (farter != NULL){
        farter->flatulator = fart;
        farter->fart = butt;
    }
    return farter;
}

int do_fart(struct farter *farter, int gas){
    flatulator flatulator = farter->flatulator;
    return flatulator(farter->butt, gas);
}

Ika
Dec 30, 2004
Pure insanity

seiken posted:

You're going to need to post your actual code then because this is a completely different situation from the code in the original question. Can't you capture bar where bar is the member variable/stack variable/function parameter and access bar->foo inside the lambda?
I was trying to simplify the problem with the example code, consider the three instances of the lambda as what is left after the preprocessor replaces the macro.

I can't capture bar if it is a member variable, the compiler throws an error and wants me to capture this instead, which I can't do because the same macro/lambda is also used to manipulate stack variables. If there actually is a way of capturing this->foo by reference or &this->foo by value without capturing this it would solve most of my problem.

I can't call my macro with &foo, *foo, std::addressof(foo) etc because that ends up being replaced in the capture expression, so currently I use foo as an additional parameter to the lambda function and have to use the exact type of foo in the macro so it can be expanded into a lambda with the correct parameter type in the definition. The alternative is using a temp variable to move foo into the local scope and then capturing it, then I need a temp variable but can capture it instead of using a parameter.

seiken posted:

Edit: if the problem is the distinction between dereferencing/not-dereferencing the thing you want to capture, then you could define a template like
C++ code:
template<typename T> struct foo_remove_ptr_t;
template<> struct foo_remove_ptr_t<Foo> {Foo& operator()(Foo& f) const {return f;}};
template<> struct foo_remove_ptr_t<Foo*> {Foo& operator()(Foo* f) const {return *f;}};
template<typename T> Foo& foo_remove_ptr(T& t) {return foo_remove_ptr_t<T>()(t);}
and use foo_remove_ptr(bar->member)
I don't see how this would change anything, I still couldn't capture the result of that function for use in my lambda without a temp variable, and with a temp variable I could just use std::addressof or whatever directly.


I was hoping there was a some special syntax which would allow me to capture the result of std::addressof(foo) etc without using an explicit temp variable to hold the value. So basically

C++ code:
auto func = [&(std::addressof(foo) as fooRef](__in const size_t size) -> HRESULT{ return fooRef->DoSomething(size); };
I think this is just something which is impossible with the current standard as there is no way of defining the name of the modified variable, but I was hoping I was wrong.

Dren
Jan 5, 2001

Pillbug

Ika posted:

I was hoping there was a some special syntax which would allow me to capture the result of std::addressof(foo) etc without using an explicit temp variable to hold the value. So basically

C++ code:
auto func = [&(std::addressof(foo) as fooRef](__in const size_t size) -> HRESULT{ return fooRef->DoSomething(size); };
I think this is just something which is impossible with the current standard as there is no way of defining the name of the modified variable, but I was hoping I was wrong.

uh

if you have the foo symbol why not

C++ code:
auto func = [&](__in const size_t size) -> HRESULT{ return foo->DoSomething(size); };

Ika
Dec 30, 2004
Pure insanity

Dren posted:

uh

if you have the foo symbol why not

C++ code:
auto func = [&](__in const size_t size) -> HRESULT{ return foo->DoSomething(size); };

Because the lambda is in a macro and the variable isn't always called foo nor is it always a pointer. Don't have the code here but a really simple case would be:


C++ code:
#define VERIFY_OBJECT(object, length) [&object](__in const size_t length) -> HRESULT { HRESULT hr; if (FAILED(hr = object->something(length))) return hr; if (FAILED(hr = object->somethingElse())) return hr; return S_OK; }(length)
Then the functions can use VERIFY_OBJECT on paramters, member variables, and locals.

Right now the code uses
C++ code:
#define VERIFY_OBJECT(object, objectType, length) [](__in const size_t length, __in objectType &object) -> HRESULT { HRESULT hr; if (FAILED(hr = object->something(length))) return hr; if (FAILED(hr = object->somethingElse())) return hr; return S_OK; }(object, length)
Replacing the entire lambda + macro stuff with template functions would be nice, but there was a good reason why it wasn't done that way. I think it was an issue with multiple defined symbols when linking if two independent libraries end up generating the same template but I may be mistaken as I'm no longer in the office and can't check.

Ika fucked around with this message at 00:02 on Aug 5, 2014

PS. Love the cabin
Dec 30, 2011
Bee Lincoln

Edison was a dick posted:

As b0lt said, yes, but the alternative is worse.

You can use typedefs to make it less of a hassle though.

code:
typedef int (*flatulator)(int butt, int gas);

int fart(int butt, int gas){
    return butt * gas;
}

struct farter {
    flatulator flatulator;
    int butt;
}

struct farter *farter_factory(int butt){
    struct farter *farter = malloc(sizeof(struct farter));
    if (farter != NULL){
        farter->flatulator = fart;
        farter->fart = butt;
    }
    return farter;
}

int do_fart(struct farter *farter, int gas){
    flatulator flatulator = farter->flatulator;
    return flatulator(farter->butt, gas);
}

I'm not sure I ever have, or will see another example as beautiful.
Thanks :D

Looks like I'll just typedef every function then, me doing stuff with assembly is a recipe for disaster.

Dren
Jan 5, 2001

Pillbug
Ika try doing a lambda inside a std::bind. Should create a copy of a member var or stack var that is captured by the lambda.

seiken
Feb 7, 2005

hah ha ha

Ika posted:

Because the lambda is in a macro and the variable isn't always called foo nor is it always a pointer. Don't have the code here but a really simple case would be:

Nothing you've said prevents default-capture from working. If you have some expression which you can access in a scope, then you can access it via exactly the same expression inside a lambda with a capture-list of [&] (default reference capture - you don't need to specify any name). Try this and if it doesn't work post a complete minimal example which exhibits the problem instead of vague comments

giogadi
Oct 27, 2009


This is actually a super interesting question but might be out of the scope of this thread. For what it's worth - I'm certain that fitting an affine transform to a dataset has been done before, but without doing any research, you might try a hybrid approach? I.e., use least squares to get an initial solution, then toss that initial solution into a local nonlinear optimizer like Levenberg-Marquardt (Ceres has this actually) where you use your constraint as your objective function.

This is fun to think about but my idea's probably horrible and this is definitely a derail so I'll stop here.

astr0man
Feb 21, 2007

hollyeo deuroga
Is there any way to get gdb to know about macro definitions when I'm compiling with clang? I know I can use -ggdb -g3 with gcc but I would like to use clang if possible. With clang-3.3 using -g3 doesn't do anything different than just -g, and there's no macro information for gdb.

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

PS. Love the cabin posted:

Is there a way to call functions at an address including passing arguments without having to write a function prototype for each one?
libffi

You know how C/C++ lets you shoot yourself in the foot with extreme ease? This library lets you do it with a bazooka. Caveat emptor.

TomTrustworthy
Aug 6, 2014
Happy I found this section, I have homework I am trying to do and I feel pretty lost. It's a class all about C and I seem to be stuck in a loop, I never have this issue with vb.net so I need help if possible.

I'm supposed to come up with a way to push grades into an array, pop them out, display the array, show the grade average and exit. Right now I can send "p" and it will ask me to enter the letter grade. But it just then keeps asking me for the letter. If i put a break after setting the array to the grade they entered then it closes the whole script.
I want it to loop again asking to scanf for a new &z value so I can hit that against the if's.

Any help would be awesome... this was due last night but I hope to get some credit. I hate C so far.

int main()
{
int x;
int y;
char z;
char grade;
int array[8][1];
printf("Send: p for Push\n");
printf("pop for Pop\n");
printf("d for Display\n");
printf("a for Average\n");
printf("x for Exit\n");

for ( x = 0; x < 8; x++ ) {
//z = " ";
scanf("%c", &z);
if (z="p"){
printf("Enter letter grade. (A/B/C/D/F)\n");
scanf("%c", &grade);
array[x][1] == &grade;
}
else if (z="d"){
printf( "Array Indices:\n" );
for ( x = 0; x < 8;x++ ) {
printf( "%d. %d\n", x, array[x][1] );
}
break;
}
}
getchar();
}

astr0man
Feb 21, 2007

hollyeo deuroga
1. use [code] tags so your code gets formatted correctly when you paste it otherwise it is an unreadable mess
2. You should double check what the difference between '=' and '==' is
3. You don't need a 2 dimensional array for what you are doing
4. We aren't going to do your homework for you

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

TomTrustworthy posted:


code:

        if (z="p"){
            printf("Enter letter grade. (A/B/C/D/F)\n");
            scanf("%c", &grade);
            array[x][1] == &grade;
        }

For starters, you want if (z=='p') there: two =s, and single quotes for characters.

I have no idea why you're putting the address of z into the array like that either. What are you trying to do there?

But your compiler should be screaming about a bunch of these things -- are you checking the warnings?

raminasi
Jan 25, 2005

a last drink with no ice
Without even getting into the semantics of your program:

1. Use [code language="C"] tags.
2. You seem to have = and == mixed up.
3. You are confusing individual characters, delimited with single-quotes, with strings of length 1, delimited with double-quotes (e.g. if (z="p"). VB.NET will let you be a little less precise about this sort of thing, but C will not.
4. There's no reason for array to be two-dimensional, especially when the second dimension isn't really another dimension. Just declare it as array[8] and don't use a second set of subscripts anywhere.

Semantic troubleshooting is going to be a whole other kettle of fish - your code is bordering on Not Even Wrong territory. Since you said you're proficient in VB.NET, it might be worthwhile to write the program in that language first so that you can get a clear sense of which problems are logic problems and which are language problems.

TomTrustworthy
Aug 6, 2014
Thanks guys I will try to fix the issues you pointed out.

Precambrian Video Games
Aug 19, 2002



code:
#include <iostream>

void print(int var) {
	std::cout << var << std::endl;
}

int main(int argc, char *argv[]) {
	int i = 0;
	print(i);
	print(i++);
}
GCC 4.7.2 and earlier print 0 and 0, not 0 and 1. Really? Stackoverflow tells me that the C++ standard is to evaluate any operators and function calls for each parameter first, then call the function. Only something like foo(i++,i++) should have undefined behaviour. I didn't find any indication that the much simpler case of foo(i++) should do anything other than increment i, then call foo. Is GCC just implementing an older standard or am I crazy?

raminasi
Jan 25, 2005

a last drink with no ice
Postincrement evaluates to the variable's old value. That's the difference between preincrement and postincrement.

Precambrian Video Games
Aug 19, 2002



Ok, so I'm merely retarded. Carry on.

I guess I may as well ask if it's true that there's a potentially meaningful performance difference between ++i and i++ in a for loop, if it's not optimized away at least.

Qwertycoatl
Dec 31, 2008

eXXon posted:

Ok, so I'm merely retarded. Carry on.

I guess I may as well ask if it's true that there's a potentially meaningful performance difference between ++i and i++ in a for loop, if it's not optimized away at least.

If i is an integer or something, then no. If i is a C++ class with some fancy overloaded operator++, then ++i may be faster.

PS. Love the cabin
Dec 30, 2011
Bee Lincoln
I had a dumb moment myself rearranging some code to have a local variable be global and wondering why later on it would all of the sudden be NULL.
As it turns out I forgot to remove the local declaration from one function so it was declared a second time and initialized to NULL.

Is that actually allowed or is it just a compiler bug?

shrughes
Oct 11, 2008

(call/cc call/cc)
(a) You should have loving renamed it and grepped so that you knew there weren't any instances of the old variable name.

(b) You shouldn't have made it a global in the first place, you're doing something completely moronic.

PS. Love the cabin
Dec 30, 2011
Bee Lincoln
Relax, I'm just loving around after not doing anything in years.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

PS. Love the cabin posted:

Is that actually allowed or is it just a compiler bug?
Yes, it is totally allowed. You can have a global x, a member variable x, a local x and a local x in a deeper scope, all of which are separate things. I think also a member variable x in a superclass too if you want, though I'm not quite so sure about that one. Plus x in a shitload of namespaces and static x'es in many files.

Adbot
ADBOT LOVES YOU

PS. Love the cabin
Dec 30, 2011
Bee Lincoln
That explains it perfectly, thanks!
I've never actually encountered that until random fuckery and chance aligned.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply