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
seiken
Feb 7, 2005

hah ha ha

schnarf posted:

Issues that only appear when optimization is on usually mean a bug in your code that didn't manifest itself without optimizations. For example, in this fragment of code:
code:
void f(int* p) {
    *p= 5;
    g();
}
If the optimizer inlines a call to f(), and can prove that f() is getting called with a null pointer, it's entitled to never call g(), because it knows that first it dereferences a null pointer, which is undefined behavior. If you're interested in this, check out this series of articles: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html

It shouldn't be doing optimizations that are only valid on your local machine, so I doubt it's that -- you should totally be able to compile on your machine.

Yeah, I'm aware of that. But I don't think that's the issue because I can literally compile this code
code:
#include <iostream>

int main()
{
    std::cout << "Content-type: text/plain" << std::endl << std::endl << "hello world" << std::endl;
    return 0;
}
And get
Output on my machine without optimization: hello world
Output on my machine with optimization: hello world
Output on server without optimization: hello world
Output on server with optimization: 500 internal server error

Adbot
ADBOT LOVES YOU

Vanadium
Jan 8, 2005

Have you tried it running manually instead of through CGI?

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!
And have you tried static linkage? It's not a great solution but it's often a good diagnostic step.

seiken
Feb 7, 2005

hah ha ha
Thanks for the suggestions, guys.

Vanadium posted:

Have you tried it running manually instead of through CGI?
Unfortunately I don't have shell access to the server and I don't think I can do this without that (unless I'm dumb and there's a way).

roomforthetuna posted:

And have you tried static linkage? It's not a great solution but it's often a good diagnostic step.
Aha, linking everything statically does indeed make it run properly on the server with any optimization settings. The program doesn't use anything except the standard libraries so I guess the server has different versions of libgcc / libc++? I probably can't change those so I guess I'll just leave it statically-linked. The program does some CPU-intensive stuff so the optimization is probably more important than the extra 1mb or so size (unless I'm dumb and this is a Bad Idea). Thanks.

seiken fucked around with this message at 22:19 on Jun 16, 2012

unixbeard
Dec 29, 2004

Did the server logs say anything? 500 errors usually have something meaningful there

seiken
Feb 7, 2005

hah ha ha
Nothing relevant (just logged the fact that it couldn't find a customised 500 error page because I haven't made one).

Paniolo
Oct 9, 2007

Heads will roll.
Try running the program directly instead of through the web server and see if you get any useful output. Also, use ldd to verify any shared library dependencies are being found.

e: Also, if you're doing anything with threads, some race conditions don't manifest in debug builds. The number of processors can also affect how these bugs manifest. It's not likely, especially if it's failing 100% of the time, but I've run into similar bugs before.

Paniolo fucked around with this message at 06:16 on Jun 17, 2012

unixbeard
Dec 29, 2004

If its apache set the loglevel to debug

Also see if it is running chroot'ed or with less privileges, though why that would be affected by optimization I could not say. If static linking works it could well be something to do with access or the environment of the web server process.

enthe0s
Oct 24, 2010

In another few hours, the sun will rise!
Why am I getting a warning saying my member functions aren't initialized?

code:
Company::Company(string name, vector<Region> regions) : name(name), regions(regions)
{

}
Isn't the section after the : initializing them?

nielsm
Jun 1, 2009



enthe0s posted:

Why am I getting a warning saying my member functions aren't initialized?

code:
Company::Company(string name, vector<Region> regions) : name(name), regions(regions)
{

}
Isn't the section after the : initializing them?

Well do you have any other fields in the class?

enthe0s
Oct 24, 2010

In another few hours, the sun will rise!

nielsm posted:

Well do you have any other fields in the class?

Those are my only two variables. However, I do have an empty constructor. Do I also have to initialize it for that?

pseudorandom name
May 6, 2007

x(x) doesn't do what you think it does.

Vanadium
Jan 8, 2005

yes it does

xgalaxy
Jan 27, 2004
i write code

enthe0s posted:

Why am I getting a warning saying my member functions aren't initialized?

code:
Company::Company(string name, vector<Region> regions) : name(name), regions(regions)
{

}
Isn't the section after the : initializing them?

Come again?
You mean your member variables?

Plorkyeran
Mar 22, 2007

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

enthe0s posted:

Those are my only two variables. However, I do have an empty constructor. Do I also have to initialize it for that?
Yes, you need to initialize your variables in all of the constructors.

xgalaxy
Jan 27, 2004
i write code

Plorkyeran posted:

Yes, you need to initialize your variables in all of the constructors.

Can't wait for delegating constructors.
Unfortunately I use Visual Studio a lot and based on their C++11 feature set for VS2k12 I'll be waiting for another 20 years.

enthe0s
Oct 24, 2010

In another few hours, the sun will rise!

xgalaxy posted:

Come again?
You mean your member variables?

Yeah sorry, that's what I meant to say.

Plorkyeran posted:

Yes, you need to initialize your variables in all of the constructors.

Do I do this the same way as I did my other constructor?
e: Yes you do. Thanks everyone.

enthe0s fucked around with this message at 23:16 on Jun 18, 2012

CPFinnit
Jun 5, 2008
Part of my assignment this week involves calcuating the sum of a series of consecutive even numbers using a formula, iterative and recursive methods.

I got it all done, but I wonder if there isn't a more efficient way to do the recursive method. Here is the function I came up with

code:
int recursiveEvenSum (int n)
{
    int evenSum; //sum

    if (n % 2 == 1) //If an odd number is given sets n to the next lowest even int
        n = n -1;
        
        if (n <= 0)
            return 0;
            
        else
            return n + recursiveEvenSum(n - 2); //Calculates the sum recursively
}
It works but is that a good method?

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!
Apart from recursion being a bad solution to that problem because it's begging for a stack overflow, which can be ignored since it's a programming test, you could make it more efficient by having two functions - the first one is the one the "user" would call, and would do the validation of the input, then the second one would do the recursion and everything else. That way you wouldn't keep doing "if (n%2 == 1)" every time round the loop, since after the first time you know for certain that that's not going to be true.

Edit: Also you're declaring evenSum and not using it at all.

CPFinnit
Jun 5, 2008

roomforthetuna posted:

Apart from recursion being a bad solution to that problem because it's begging for a stack overflow, which can be ignored since it's a programming test, you could make it more efficient by having two functions - the first one is the one the "user" would call, and would do the validation of the input, then the second one would do the recursion and everything else. That way you wouldn't keep doing "if (n%2 == 1)" every time round the loop, since after the first time you know for certain that that's not going to be true.

Edit: Also you're declaring evenSum and not using it at all.

I didn't catch evenSum, thanks.

The program is 4 functions in total, the first verifies that the input is a non negative integer. The second finds the sum of the series using the formula, the third function uses iteration and the final uses recursion. This is the first time I've used recursion and it felt a little clunky.

xgalaxy
Jan 27, 2004
i write code
It's feels clunky because every test I've seen asks you to do recursion when it really doesn't make any sense.
I've seen very very few real world problems where recursion would have been a better idea than a simple loop. But that is probably why every test has some stupid contrived problem where they want you to utilize recursion. I guess they have to teach it somehow.

Nippashish
Nov 2, 2005

Let me see you dance!

xgalaxy posted:

I've seen very very few real world problems where recursion would have been a better idea than a simple loop.

Saying things like this is usually a pretty good indicator that you don't grok the thing you're criticizing.

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

xgalaxy
Jan 27, 2004
i write code

Nippashish posted:

Saying things like this is usually a pretty good indicator that you don't grok the thing you're criticizing.

Personal attacks sure do raise the level of good discussion and debate.

I didn't say recursion is bad. I didn't say you should never use recursion. I simply stated that I've seen or had to personally / professionally deal with very few real world problems where recursion would have been a better solution.

But if attacking me makes you feel superior than whatever.
Fire away.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
A standard problem solved by recursion is a depth-first search of a tree.

C++ code:
typedef enum {
  TYPE_TREE,
  TYPE_LEAF,
} NodeType;

struct Node {
  NodeType type;
};

struct Child {
  NodeType type;
  void *data;
};

struct Tree {
  NodeType type;
  Child *children;
};

static void
gather_all_children (struct Tree *tree,
                     struct List *list)
{
  struct Node *node = tree->children;

  while (node != NULL) {
    if (node->type == TYPE_TREE) {
      struct Tree *node_tree = (struct Tree *) node;
      gather_all_children (node_tree, list);
    } else {
      struct Child *node_child = (struct Child *) node;
      list_append_item  (list, node_child->data);
    }
  }
}

xgalaxy
Jan 27, 2004
i write code

Suspicious Dish posted:

A standard problem solved by recursion is a depth-first search of a tree.

This just helps to prove my point.
I never said that there are no situations where recursion is better. I said there are few.
I'm staring right now at a complex code base sitting at well over 500k lines of code.
I can count on one hand how many times recursion is used. Granted its a large code base and I may be missing a few places. But I think it speaks volumes that I can pretty much guess where it may be used in the code base and where it wouldn't be.

Paolomania
Apr 26, 2006

xgalaxy posted:

Personal attacks sure do raise the level of good discussion and debate.


Be careful of what you say around here or your tail might get called out.

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!
It does seem silly to teach recursion through making people write a terrible function that they should never write, rather than trying to come up with the simplest possible situation in which recursion is actually a good solution and teaching it with that. It's like they're teaching "there are several ways to do things but this one is stupid" rather than "there are several ways to do things and sometimes this one is appropriate".

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
There are domains that are naturally recursive and there are domains that aren't. As a compiler writer, most of the data I deal with is recursively defined and acyclic, so recursion is ideal. When I worked on object databases, it was mostly highly cyclic graphs, so recursion was less ideal. I wrote a configuration file parser once — that was technically XML, but most walks over it were fixed-path, so recursion was really just not relevant for clients.

I was also a TA once, and let me assure you, the annoyance of occasionally needing to convince students to get over the artificial setup and just focus on wrapping their heads around the underlying concepts was vastly outweighed by not having to spend thirty minutes developing, explaining, and justifying a more realistic problem statement.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Yeah, as interesting as "code me up a parser for this DSL" is as a question, it's pretty complex as an "introduction to recursion" problem.

So then we get artificial stuff like "do an in-order traversal of this tree".

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Jabor posted:

Yeah, as interesting as "code me up a parser for this DSL" is as a question, it's pretty complex as an "introduction to recursion" problem.

So then we get artificial stuff like "do an in-order traversal of this tree".

I honestly saw recursion as a toy until I stumbled across dynamic programming - which is basically recursion except you store the solutions to sub-problems in a data structure instead of recomputing them every single time. DP will produce a solution about as fast and space efficient and understandable as you can get for some kinds of problems, but good luck explaining how your code works to a front-end developer or a manager.

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

hieronymus posted:

I honestly saw recursion as a toy until I stumbled across dynamic programming - which is basically recursion except you store the solutions to sub-problems in a data structure instead of recomputing them every single time. DP will produce a solution about as fast and space efficient and understandable as you can get for some kinds of problems, but good luck explaining how your code works to a front-end developer or a manager.

Well that's the thing. Proofs of dynamic programming are really simple, they just have a step in them that looks like magic. Just introduce that step as 'trivial' and nobody will ask any questions. The emperor is wearing a fine robe!

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!

Jabor posted:

Yeah, as interesting as "code me up a parser for this DSL" is as a question, it's pretty complex as an "introduction to recursion" problem.

So then we get artificial stuff like "do an in-order traversal of this tree".
That's not what I was griping about - artificial stuff like a traversal of a tree is a fine thing to teach recursion, because if you want to traverse a tree, recursion is pretty good for that. It doesn't matter that the situation is artificial - all early programming lesson situations will be, you're not going to be solving actual unsolved problems.

What I was griping about is "use recursion to calculate a factorial" or "use recursion to calculate a sum of numbers" - both tasks for which recursion is poorly suited, which make the lesson teach, as a side-effect, "recursion is stupid and worse than iteration in every way". I think it's important to teach people to recognize when recursion is a good solution, at the same time as teaching them how to do it. If you don't do that then it seems like recursion is nothing but an artificial constraint you might impose on yourself to show off.

(Is "dynamic programming" what I would have been using for a chess AI?)

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

roomforthetuna posted:

That's not what I was griping about - artificial stuff like a traversal of a tree is a fine thing to teach recursion, because if you want to traverse a tree, recursion is pretty good for that. It doesn't matter that the situation is artificial - all early programming lesson situations will be, you're not going to be solving actual unsolved problems.

What I was griping about is "use recursion to calculate a factorial" or "use recursion to calculate a sum of numbers" - both tasks for which recursion is poorly suited, which make the lesson teach, as a side-effect, "recursion is stupid and worse than iteration in every way". I think it's important to teach people to recognize when recursion is a good solution, at the same time as teaching them how to do it. If you don't do that then it seems like recursion is nothing but an artificial constraint you might impose on yourself to show off.

(Is "dynamic programming" what I would have been using for a chess AI?)

I can see DP being used for a chess API - take a look at http://people.csail.mit.edu/bdean/6.046/dp/. If the problem you're trying to solve looks like one of these problems, it's time to whip out dynamic programming - I can see why DP gets skipped over for most undergrad programs because coming up with a novel DP solution is something that I would not expect most people to be able to do.

Paolomania
Apr 26, 2006

Fibonacci is a good intro-to-dynamic-programming example that is recursively defined, easy to analyze, and and easy to compare against a recursive solution. You don't even need to teach the undergrads about all those stack frames you are saving - just expand the recurrence a few times to show the redundant work. You can even bring up the topic of whether or not the entire solutions space needs to be stored.

WRT tree traversal, using the stack as a glorified bookmark into a data structure can get expensive. Isn't it funny that Russell & Norvig's generalized tree algorithms do everything with a queue and a loop? If your problem doesn't use all that recursive context then why keep it around? But enough algorithms chat, this is the C/C++ thread!

raminasi
Jan 25, 2005

a last drink with no ice
Is there some kind of Boost.Multibimap?

e: Let me get a little more specific. I have a bunch of binary relationships (let's call them "connections") between Foos, and I need to look up, given some Foo, which Foos are connected to it. I could make a graph, but it would be pretty sparse.

edit 2: I realized that I need a more sophisticated structure and found Boost.MultiIndex, so I'm going with that.

raminasi fucked around with this message at 21:48 on Jun 19, 2012

enthe0s
Oct 24, 2010

In another few hours, the sun will rise!
So I want to convert a hex value into a decimal value while I'm outputting to a stream. However, it seems as though I can only change a stream to hex mode at the beginning? This would be fine if I were just outputting a single value, but I also have other things I want to output in the same stream. For reference, this is what I'm currently doing:

code:
 stream<<customer.lname<<"   "<<customer.fname<<"   "<<customer.id<<"   "
    <<customer.phone<<"   "<<customer.salesTotal<<"   "<<customer.percent; 
And I want to output customer.id (given to me as a hex string) and convert it to a dec string. From what I've seen (correct me if I'm wrong), you can only change the value if you change the beginning of a stream like so:
code:
 stream<<std::hex<<customer.id; 
I also want to leave customer.phone in decimal format. Is there an elegant way of doing this or do I just have to create a separate stream?

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

enthe0s posted:

And I want to output customer.id (given to me as a hex string) and convert it to a dec string. From what I've seen (correct me if I'm wrong), you can only change the value if you change the beginning of a stream like so:
code:
 stream<<std::hex<<customer.id; 
I also want to leave customer.phone in decimal format. Is there an elegant way of doing this or do I just have to create a separate stream?
You can stream in the stream modifiers whenever you want: http://codepad.org/KQ0u3PES

enthe0s
Oct 24, 2010

In another few hours, the sun will rise!

Isilkor posted:

You can stream in the stream modifiers whenever you want: http://codepad.org/KQ0u3PES

I've tried that and it doesn't seem to affect the output at all. Does hex not work on strings?

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
It sounds like what you're wanting to do is convert the string to a number, and then output that number as hexadecimal, right?

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