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
ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Gvaz posted:

Someone said "it cant resolve printf because somehow its not linking in standard c library" :(

That person is wrong. It's not working because you're trying to run the source code, instead of the program that is created when you compile it. Additionally: you're dumb, and so is that person.

The program being created is probably called a.out. Run that.

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
It works for me.

code:
$ cat test.c
#include <stdio.h>
main()
{
printf("Joe Shmoe\n");
}
$ gcc test.c
$ ./a.out 
Joe Shmoe

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

ShoulderDaemon posted:

It's not working because you're trying to run the source code, instead of the program that is created when you compile it.

Hahaha, I didn't even notice that. Adding this to cobol.txt. :3:

PT6A
Jan 5, 2006

Public school teachers are callous dictators who won't lift a finger to stop children from peeing in my plane
I find it troubling that a source file was created with execute permission on what appears to be a UNIX system of some sort.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

Gvaz posted:

Someone said "it cant resolve printf because somehow its not linking in standard c library" :(

I cant vm on this machine or whatever. :[ And I can't get Msys to work (it just opens up a billion windows then crashes)

It works for me, so it's something on your side. And I think it would tell you it couldn't find stdio.h.

e:f,b

Cosmopolitan fucked around with this message at 03:59 on Jan 20, 2009

Hung Yuri
Aug 29, 2007

by Tiny Fistpump
I'm using cygwin and i got it to work by doing "gcc chall3.c -o chall3.exe" instead of just "gcc chall3.c" under ubuntu. I forgot I wasn't supposed to do .h and it was .c :downs:

VVV Yeah using vista64, thank you for the link.

Hung Yuri fucked around with this message at 04:01 on Jan 20, 2009

ehnus
Apr 16, 2003

Now you're thinking with portals!

Gvaz posted:

I cant vm on this machine or whatever. :[ And I can't get Msys to work (it just opens up a billion windows then crashes)

Vista 64? Try this: http://www.nabble.com/Installing-MSYS-on-Windows-Vista-x64-td16904988.html

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Good lord, can people not write out "I don't know" anymore?

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

Dijkstracula posted:

Good lord, can people not write out "I don't know" anymore?

That would be a pretty worthless post.








:downs:

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
I have a function that I would like to optimize as much as possible. It contains a number of branches based on a few bools, which I'd like to unroll. So, I can make the bools template arguments like so:
code:
template<bool p1, bool p2>
void Process()
{
  doStandardStuff();
  if (p1) doFancyStuff();
  if (p2) doExtraFancyStuff();
}

// Do fancy and extra fancy processing
Process<true, true>();
Now, I want these parameters to be configurable by the user. So the code to call the Process() function will end up looking a bit like this:
code:
if ((checkBoxFancy.isChecked() == false)&&(checkBoxExtraFancy.isChecked() == true) Process<false, true>();
if ((checkBoxFancy.isChecked() == true)&&(checkBoxExtraFancy.isChecked() == true) Process<true, true>();
...
I can ofcourse replace those if/then's with switches and so on, but when I'm going to have 5 parameters it will already be quite the combinatorial explosion.

Is there some pattern to clean this up? Ideally I want to simulate this:
code:
Process<checkBoxFancy.isChecked(), checkBoxExtraFancy.isChecked()>();
Ofcourse this is not possible, but is there some way of at least approaching such a solution?

newsomnuke
Feb 25, 2007

Something that came up as a result of a bug, which I don't understand:

code:
struct Obj
{
	int x;
};

void blah(void *bar)
{
	Obj *foo = static_cast<Obj *>(bar);
	cout << foo->x << endl;
}

Obj *donk = new Obj;
donk->x = 5;

blah(&donk);
Obviously I shouldn't be passing the address of donk to blah(), but I don't understand the behaviour when I did. What seems to happen is that, in blah(), foo is cast to some kind of phantom Obj* which has not been constructed, but doesn't segfault when used.

edit: ignore, I'm a moron.

newsomnuke fucked around with this message at 16:33 on Jan 21, 2009

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
Edit: Ah, you knew what you were doing was wrong. Doh. :)

Sagacity fucked around with this message at 16:22 on Jan 21, 2009

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

ultra-inquisitor posted:

Obviously I shouldn't be passing the address of donk to blah(), but I don't understand the behaviour when I did. What seems to happen is that, in blah(), foo is cast to some kind of phantom Obj* which has not been constructed, but doesn't segfault when used.

Why would it segfault? On most platforms an Obj and a pointer to an Obj are the same size, so you're passing blah an allocated chunk of memory of the correct size. If Obj had more than one field or if on your platform ints and pointers were different sizes this wouldn't "work".

newsomnuke
Feb 25, 2007

Plorkyeran posted:

Why would it segfault? On most platforms an Obj and a pointer to an Obj are the same size, so you're passing blah an allocated chunk of memory of the correct size. If Obj had more than one field or if on your platform ints and pointers were different sizes this wouldn't "work".
Sorry guys, brainfart. I haven't had enough coffee.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

Sagacity posted:

Is there some pattern to clean this up? Ideally I want to simulate this:
code:
Process<checkBoxFancy.isChecked(), checkBoxExtraFancy.isChecked()>();
Ofcourse this is not possible, but is there some way of at least approaching such a solution?
code:
inline void Process(bool p1, bool p2)
{
  doStandardStuff();
  if (p1) doFancyStuff();
  if (p2) doExtraFancyStuff();
}
// ...
  Process(checkBoxFancy.isChecked(), checkBoxExtraFancy.isChecked());
If you're going to be figuring out what to call at runtime anyhow, templatizing that function isn't going to give you anything.

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.

Mustach posted:

If you're going to be figuring out what to call at runtime anyhow, templatizing that function isn't going to give you anything.
Sure it will. Specifying the booleans as a template argument allows the compiler to optimize out the branches in my Process() function at compile-time. The generated assembly code doesn't contain any branches.

What I would like is for the compiler to generate all possible permutations at compile-time and then allow me to select which of the permutations to use at runtime (and even changing which one to use at runtime as well, based on user input).

Maybe it helps to add that the Process() innerloop is going to be called a lot of times, so maybe I should point out that Process() is more likely to look like this:
code:
void Process(int numSamples)
{
  for (int t=0;t<numSamples;t++)
  {
    doStandardStuff();
    if (p1) doFancyStuff();
    if (p2) doExtraFancyStuff();
  }
}
I can therefore also move the p1/p2 branches up above the for-loop, but then I would have to explicitly write code for every permutation of the inner-loop.

Sagacity fucked around with this message at 17:43 on Jan 21, 2009

TheSleeper
Feb 20, 2003

Sagacity posted:

Sure it will. Specifying the booleans as a template argument allows the compiler to optimize out the branches in my Process() function at compile-time. The generated assembly code doesn't contain any branches.

Uh, how exactly does the generated assembly decide which version to call at runtime without branches?

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

TheSleeper posted:

Uh, how exactly does the generated assembly decide which version to call at runtime without branches?
He's saying that he wants a function for each combination of *FancyStuff() calls, so that the conditions are only evaluated once instead of inside of the loop.

How much work do the function calls do?

Mustach fucked around with this message at 18:10 on Jan 21, 2009

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.

Mustach posted:

He's saying that he wants a function for each combination of *FancyStuff() calls, so that the conditions are only evaluated once instead of inside of the loop.
This is precisely what I want to do, yes.

Mustach posted:

How much work do the function calls do?
Well, basically the Process() function I just described would be a DSP innerloop, so it's doing a LOT of work. I suppose 95% of the time spent would be in this loop, which is why I want to eliminate branches as much as possible.

Also, I'm using simple boolean parameters as branches in this example, but it might also extend to different types of parameters (which can be handled by a templating scheme without any problems) of course.

Finally, in the 'final' product these doFancyStuff() calls will be inlined as well, so there's no calling overhead (and if there is some overhead I may be able to just take the code of doFancyStuff and make it part of Process(), but that would still make me want to avoid the branching).

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
First: have you actually looked at the assembly output? Your compiler might pull the conditionals out of the loop anyway. Generally speaking, if you have to ask how to outsmart the compiler, you probably shouldn't be trying to.

Also, whatever happened to people just writing self-modifying code?

Vanadium
Jan 8, 2005

Not threadsafe :colbert:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Vanadium posted:

Not threadsafe :colbert:

Hm yase I too expect that DSP code will be run from multiple threads at the same time.

Vanadium
Jan 8, 2005

You just know this is going to be an insidiously hard-to-track-down bug in a couple of years when someone is hooking the whole thing up to a web app.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
That's where the magic of comments come into play.

// gently caress you guys do not run this code in multiple threads I will loving gut you!!!

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Vanadium posted:

You just know this is going to be an insidiously hard-to-track-down bug in a couple of years when someone is hooking the whole thing up to a web app.

In that case they deserve everything they get!

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.

Avenging Dentist posted:

First: have you actually looked at the assembly output? Your compiler might pull the conditionals out of the loop anyway. Generally speaking, if you have to ask how to outsmart the compiler, you probably shouldn't be trying to.
The compiler is not pulling the conditionals out of the loop. The reason for this is that it won't just go ahead and unroll every permutation of the loop simply because it 'may' be useful. So what I'm trying to do, basically, is explicitly telling the compiler to unroll every permutation and let me pick one.

If we were talking about a function containing one or two branches it might be doing this, but I want to extend this a bit more than that. Total code-size is not an issue for me, but I want every permutation of the innerloop to be as tight as possible.

Avenging Dentist posted:

Also, whatever happened to people just writing self-modifying code?
Self-modifying code is extremely cache unfriendly and will gently caress up the instruction reordering that the CPU is trying to do. So that's not an option.

Oh and thread-safety is irrelevant, this will be run from one thread :)

Vanadium
Jan 8, 2005

Avenging Dentist posted:

That's where the magic of comments come into play.

// gently caress you guys do not run this code in multiple threads I will loving gut you!!!

It seems to me that a way more "C++" approach would be to parametrise the whole thing on a singleton mutex type.

Then you just need the compiler to do some whole-program analysis to figure out when the function is only being used from one thread and remove the locking overhead.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Sagacity posted:

Self-modifying code is extremely cache unfriendly and will gently caress up the instruction reordering that the CPU is trying to do. So that's not an option.

Not if you modify it only when the settings change (which, I gather, is fairly infrequently). The modification of the code wouldn't occur inside the processing function.

Also if you're a huge fag for templates (I know I am), you can do it like this: http://codepad.org/aeQWDinF

I don't recommend using anything more complicated than bools as template arguments, since code size will explode otherwise. 2^32 specializations of a function would be awful, and would have the side-effect of storing 2^32 function pointers, which isn't even addressable on a 32-bit system.

You can wrap up the code in a class to make it look just like a function, but I got bored before I got that far in typing.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
By now we've spent way more time thinking about how to optimize this than whatever time could be saved by the ideal optimization.

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
I've got it working now anyway, thanks to AD's code. I had nearly the same code, except my 'doit' was part of a class, which doesn't work that well with plain old function pointers.

Thanks, guys :) Now back to SSE intrinsics, whee!

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
I don't know how many of you guys are taking classes in programming right now, but is it common to get teachers who say that you can't use any methods for assignments that he hasn't taught the whole class yet? Or is my teacher just a douche? One of the assignments happened to be the 3n + 1 problem, which I had already done for UVa, and I had to rewrite a major portion of it because we could only use while loops, if statements, and no functions. :bang:

awesmoe
Nov 30, 2005

Pillbug

Anunnaki posted:

I don't know how many of you guys are taking classes in programming right now, but is it common to get teachers who say that you can't use any methods for assignments that he hasn't taught the whole class yet? Or is my teacher just a douche? One of the assignments happened to be the 3n + 1 problem, which I had already done for UVa, and I had to rewrite a major portion of it because we could only use while loops, if statements, and no functions. :bang:

Seems fairly reasonable, otherwise you get the situation where the assignment is "write a program to sort this list of elements" and some joker solves it by calling std::sort.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Absolute restrictions on programming methods are extremely rare outside of intro courses, though it sometimes happens in paradigm or discrete-math classes where the professor wants to force you to consider novel ways to approach the problem.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
(Note: not really a question.)

Some of you might remember how I was writing a memoizer for C++. Well, I now have a version that makes only a single copy of the arguments when the function call hasn't been memoized yet, and no copies when it's already been memoized. It also supports type conversion (when you pass types that don't match the function signature) and full move-construction support. The code is probably a little bit fragile, and it still isn't as generic as it could be, but it's a fair bit more "interesting" than it used to be. Here's the code in full: http://www.teamboxel.com/misc/memoizer.tar.gz

This is probably the most interesting block of code:
code:
namespace detail
{
	template<typename T>
	struct remove_const_ref :
		std::remove_const<
			typename std::remove_reference<T>::type
		>
	{};

	template<typename T,typename U>
	struct is_similar : 
		std::is_same<
			typename detail::remove_const_ref<T>::type,
			typename detail::remove_const_ref<U>::type
		>
	{};
}

// ...

// in memoizer::operator()
call<
	typename boost::mpl::if_<
		typename detail::is_similar<CallArgs,Args>::type,
		CallArgs&&,
		typename detail::remove_const_ref<Args>::type&&
	>::type...
>(std::forward<CallArgs>(params)...);
The above has the effect of forcing any necessary type conversions while maintaining the rvalue-ness where appropriate (including rvalueness of the temporary converted params).

Comments and criticisms are welcome. However, I'm already aware that upgradable_tuple.hpp is full of bad juju (especially potential alignment issues). :)

Ari
Jun 18, 2002

Ask me about who Jewish girls should not marry!

Anunnaki posted:

I don't know how many of you guys are taking classes in programming right now, but is it common to get teachers who say that you can't use any methods for assignments that he hasn't taught the whole class yet? Or is my teacher just a douche? One of the assignments happened to be the 3n + 1 problem, which I had already done for UVa, and I had to rewrite a major portion of it because we could only use while loops, if statements, and no functions. :bang:

I had a C++ intro class last semester with a professor I knew outside of school. The first problem he presented to the class was to get the square root of some (positive) float accurate to within 5 decimal places, without using the math library. He hadn't taught functions, just basic console I/O, arithmetic, and if and while statements. I was very curious how to do it, so I just did it with Newton's method (get closer and closer until it's close enough). It turns out that he wanted something like this:

code:
float sq_rt(float x) 
{
    float y = 0.0;
    while (y*y < x)
        y += 0.00001;
    return y;
}
:doh:

Jo
Jan 24, 2005

:allears:
Soiled Meat
It's late and I'm getting retarded. Is there an obvious flaw with this function?

code:
Image * convolutionMatrix_5x5_kernel( Image * img, double matrix[] ) {
	Image * distImg = new Image( img->getWidth(), img->getHeight() );
	
	for( char v=0; v < 3; v++ ) {
		for( int y=2; y < img->getHeight()-2; y++ ) {
			for( int x=2; x < img->getWidth()-2; x++ ) {
				int value = ( 
					(((double)img->getPixel(x-2,y-2,v)* matrix[ 0]) + ((double)img->getPixel(x-1,y-2,v)* matrix[ 1]) + ((double)img->getPixel(x+0,y-2,v)* matrix[ 2]) + ((double)img->getPixel(x+1,y-2,v)* matrix[ 3]) + ((double)img->getPixel(x+2,y-2,v)* matrix[ 4])) +
					(((double)img->getPixel(x-2,y-1,v)* matrix[ 5]) + ((double)img->getPixel(x-1,y-1,v)* matrix[ 6]) + ((double)img->getPixel(x+0,y-1,v)* matrix[ 7]) + ((double)img->getPixel(x+1,y-1,v)* matrix[ 8]) + ((double)img->getPixel(x+2,y-1,v)* matrix[ 9])) +
					(((double)img->getPixel(x-2,y+0,v)* matrix[10]) + ((double)img->getPixel(x-1,y+0,v)* matrix[11]) + ((double)img->getPixel(x+0,y-0,v)* matrix[12]) + ((double)img->getPixel(x+1,y-0,v)* matrix[13]) + ((double)img->getPixel(x+2,y+0,v)* matrix[14])) +
					(((double)img->getPixel(x-2,y+1,v)* matrix[15]) + ((double)img->getPixel(x-1,y+1,v)* matrix[16]) + ((double)img->getPixel(x+0,y+1,v)* matrix[17]) + ((double)img->getPixel(x+1,y+1,v)* matrix[18]) + ((double)img->getPixel(x+2,y+1,v)* matrix[19])) +
					(((double)img->getPixel(x-2,y+2,v)* matrix[20]) + ((double)img->getPixel(x-1,y+2,v)* matrix[21]) + ((double)img->getPixel(x+0,y+2,v)* matrix[22]) + ((double)img->getPixel(x+1,y+2,v)* matrix[23]) + ((double)img->getPixel(x+2,y+2,v)* matrix[24])) );
				distImg->setPixel(x,y,v,value);
			}
		}
	}

	return distImg;
}

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
Okay, this is driving me up a wall. Can somebody tell me why this is giving a seg fault? It happens on line 18.

http://pastebin.com/d66a3c60f

POKEMAN SAM
Jul 8, 2004

Jo posted:

It's late and I'm getting retarded. Is there an obvious flaw with this function?


Are you sure those values are going to fit into an int?

POKEMAN SAM
Jul 8, 2004

Anunnaki posted:

Okay, this is driving me up a wall. Can somebody tell me why this is giving a seg fault? It happens on line 18.

http://pastebin.com/d66a3c60f

Let's see how box is defined and passed into the function.

Adbot
ADBOT LOVES YOU

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

Ugg boots posted:

Let's see how box is defined and passed into the function.

Here's the entire source code.

http://pastebin.com/d457e3136

If you need to know what it actually is, it's this problem:
http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=39

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