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
some_admin
Oct 11, 2011

Grimey Drawer
Asking for assistance -

Anyone with c+ & robotics feel like earning karma?
Daughter works with underfunded school in rural area; somehow they have robot lab and robot arm controlled by c+.
They have some questions, I haven't thought about c at all for 19 years since programming 101, and I am not a coder in any event.
I don't have details- will put you in touch with teacher/instructor.

I'll do someing nice, write you a song or something if you can make a difference.
If it matters, daughter is on SA.
chughes2002 at gmail if need to contact
Apologies if inappropriate or wrong thread.

some_admin fucked around with this message at 23:15 on Oct 27, 2012

Adbot
ADBOT LOVES YOU

MrMoo
Sep 14, 2000

Post a new thread with the questions? Plenty of people are likely to help out. Robots are cool after all.

Hot Yellow KoolAid
Aug 17, 2012
I'm reviewing a practice exam for my Op Sys class, and I am having trouble answering some of the questions. They all concern semaphores, and I need to make sure I understand them.

The first one concerns the canonical Dining Philosophers problem (code):

Given the test method...
code:
void test(int ph_num)
{
    if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
    {
        state[ph_num] = EATING;
        sleep(2);
        printf("Philosopher %d takes fork %d and %d\n",ph_num+1,LEFT+1,ph_num+1);
        printf("Philosopher %d is Eating\n",ph_num+1);
        sem_post(&S[ph_num]);
    }
}
and the take_fork method...
code:
void put_fork(int ph_num)
{
    sem_wait(&mutex);
    state[ph_num] = THINKING;
    test(LEFT);
    test(RIGHT);
    printf("Philosopher %d putting fork %d and %d down\n",ph_num+1,LEFT+1,ph_num+1);
    printf("Philosopher %d is thinking\n",ph_num+1);
    sem_post(&mutex);
}
What would be the effect of setting the philosopher's state to thinking after the two calls to the test() method in the take_fork() method? I've been staring at the code for over an hour and I've tried adding some diagnostic code, but so far I'm not really sure. The best I can figure is that it will cause the same philosopher to occasionally eat 2 or 3 times in a row.


Edit: Also (for a different problem), what's the easiest way to measure the amount of system time that has elapsed during a given segment of code?

Hot Yellow KoolAid fucked around with this message at 03:17 on Oct 28, 2012

Deffon
Mar 28, 2010

Hot Yellow KoolAid posted:

I'm reviewing a practice exam for my Op Sys class, and I am having trouble answering some of the questions. They all concern semaphores, and I need to make sure I understand them.

The first one concerns the canonical Dining Philosophers problem (code):

Given the test method...

and the take_fork method...

What would be the effect of setting the philosopher's state to thinking after the two calls to the test() method in the take_fork() method? I've been staring at the code for over an hour and I've tried adding some diagnostic code, but so far I'm not really sure. The best I can figure is that it will cause the same philosopher to occasionally eat 2 or 3 times in a row.

I'm assuming that you mean the 'put_fork' method(since you quoted that part)?
If so, then the left and right philosophers will both try to eat if they are hungry. If so, they will fail, because the philosopher in question hasn't put down his forks yet.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
Have any of you found a cross-platform library for generating exceptions that include a stack trace? I see OS-specific methods of acquiring the call stack to put in a custom exception class, but I wonder if anybody ever went the extra mile and made a library for it. One thing I do wish I could have in C++ is a nice stack trace.

Lprsti99
Apr 7, 2011

Everything's coming up explodey!

Pillbug
All right, at my wit's end here. Working on an assignment where I'm to take a previous assignment, and implement it with ADT's (classes). Most of the files have already been converted, leaving one header and one cpp file for me to do. However, I'm hitting some errors that I have no idea how to deal with, so any information would be hugely appreciated.

game.cpp and game.h (the only two I'm allowed to modify)
http://pastebin.com/sut4yYTL
http://pastebin.com/sdYRwEYY

The rest of the files.
http://pastebin.com/jDEG8Jpn
http://pastebin.com/5HuSpAv2
http://pastebin.com/Qg1WJscd
http://pastebin.com/hGwvm15h
http://pastebin.com/N1e8M06E

The error I'm getting:

code:
||=== Hangman ADT, Debug ===|
C:\...\game.h|Line 38|error: field 'provider' has incomplete type|
C:\...\game.h||In member function 'Provider Game::getProvider()':|
C:\...\game.h|Line 29|error: return type 'struct Provider' is incomplete|
C:\...\game.h|Line 29|error: 'provider' was not declared in this scope|
||=== Build finished: 3 errors, 8 warnings ===|

The Gripper
Sep 14, 2004
i am winner
Your game.cpp and game.h files are the same on pastebin (guessing you pasted the wrong thing).

Lprsti99
Apr 7, 2011

Everything's coming up explodey!

Pillbug

The Gripper posted:

Your game.cpp and game.h files are the same on pastebin (guessing you pasted the wrong thing).

:doh: Fixed. 2:30 am does not agree with me.

The Gripper
Sep 14, 2004
i am winner
Few things wrong, nothing super serious though. In game.h you should just be #including provider.h and guesser.h rather than using forward declaration (see: http://stackoverflow.com/questions/553682/when-to-use-forward-declaration). That will sort out your incomplete type errors.

After that you'll need to use an initialization list for Game::Game in game.cpp to initialize your guesser (because there is no default constructor, see: http://www.cprogramming.com/tutorial/initialization-lists-c++.html). That's as easy as changing line 11 to:
code:
Game::Game(int wordLength, const char* wordListFilename, Provider provider) : guesser(Guesser(wordLength, wordListFilename))
There's some other issues on lines 38 and 43 because those functions haven't been defined in main.cpp (I didn't check to see if they'd been defined elsewhere).

That should get you on your way!

e: there was something else in Game::guessHasBeenMade as well (not calling getResponseToGuess on the instance of Provider you're creating) but that should be pretty easy to fix once the errors you're already getting are resolved.

The Gripper fucked around with this message at 08:56 on Oct 29, 2012

Lprsti99
Apr 7, 2011

Everything's coming up explodey!

Pillbug
Never mind, me being dumb here.

Thanks for the info!

The Gripper posted:

e: there was something else in Game::guessHasBeenMade as well (not calling getResponseToGuess on the instance of Provider you're creating) but that should be pretty easy to fix once the errors you're already getting are resolved.

Yeah, caught that, and the characterIs/IsNotInWord functions are a part of the Guesser class, caught that too. Now everything's working, other than when the computer guesses a correct letter, it then repeats that letter again. Just gotta figure that out.

e: Think I've got that too, might be something to do with my wordlist.

Nope.

Well, actually, yeah, it doesn't seem to be reading the wordlist at all. Hmm.

Okay, it is reading the wordlist, but something is hosed up with the characterIsInWord function, I think. And if I run it's guesses out and type in the word (which I've verified is on the list), it says it doesn't know that word. :suicide:

Gotta sleep. If someone sees what's causing the wordlist to get hosed or whatever, please point me in the right direction. And thanks again for the help, Gripper.
Link to the current files: https://dl.dropbox.com/u/11481768/Hangman%20ADT.zip

Lprsti99 fucked around with this message at 09:57 on Oct 29, 2012

Lprsti99
Apr 7, 2011

Everything's coming up explodey!

Pillbug
Quote is not edit :v:

The Gripper
Sep 14, 2004
i am winner
It's just that you're passing a copy of your guesser in getGuesser() (and I suppose getProvider too, though I don't think your provider does anything that would make that a problem..) so changes to charactersTried[] are being discarded.

You could either change your Guesser and Provider class members to be pointers, and getGuesser and getProvider to return pointers (will require you to change game.cpp and hangman.cpp to use pointer dereference -> instead of . for calls), or change Guesser and Provider to references outright.

I don't know which of those people here would suggest, though I think in general pointers are preferred for reasons that aren't particularly applicable in this assignment. If hangman.cpp is a file that was provided to you in the state it's in now then the second (reference) option is the solution, given it's not using any pointer dereferencing.

Lprsti99
Apr 7, 2011

Everything's coming up explodey!

Pillbug

The Gripper posted:

It's just that you're passing a copy of your guesser in getGuesser() (and I suppose getProvider too, though I don't think your provider does anything that would make that a problem..) so changes to charactersTried[] are being discarded.

You could either change your Guesser and Provider class members to be pointers, and getGuesser and getProvider to return pointers (will require you to change game.cpp and hangman.cpp to use pointer dereference -> instead of . for calls), or change Guesser and Provider to references outright.

I don't know which of those people here would suggest, though I think in general pointers are preferred for reasons that aren't particularly applicable in this assignment. If hangman.cpp is a file that was provided to you in the state it's in now then the second (reference) option is the solution, given it's not using any pointer dereferencing.

That did it, and it works perfectly now. Thank you so very much.

raminasi
Jan 25, 2005

a last drink with no ice

rjmccall posted:

The first question is whether you really need separate Point2 and Point3 templates instead of, say, a Point template with a dimension argument. Even if the types are significantly different for different dimensions, making them partial specializations of the same template makes a lot of the type relationships trivial to compute. In C++11, you can even make template aliases for things like Line2 and Point3.

Anyway, if you want to make separate templates, it's pretty straightforward to write a template metafunction which separates a class template from its template arguments. You can probably use this same technique to kill off your point-to-traits and line-to-traits metafunctions. I'm going to write this in C++03; in C++11, it'd be cleaner to make line_template_for_point_template export a template alias member instead of a typedef member which applies that template.

C++ code:
template <class T> struct line_for_point;
template <template <class T> class A, class T>
struct line_for_point<A<T> > {
  typedef typename line_template_for_point_template<A, T>::type type;
};
You then just need a database that maps Point templates to Line templates. Again, as long as the relationship between Point2 and Point3 is just a similarity in names, this is pretty much obligatory.

C++ code:
template <template <class> class, class> struct line_template_for_point_template;
#define LINE_FOR_POINT(LineT, PointT) \
  template <typename T> struct line_template_for_point_template<PointT, T> { typedef LineT<T> type; };
LINE_FOR_POINT(Line2, Point2)
LINE_FOR_POINT(Line3, Point3)

I just tried implementing this, and Visual Studio 2010 isn't happy with even this:
C++ code:
template <class T> struct line_for_point;
template <template <class T> class A, class T>
struct line_for_point<A<T> > {
	typedef typename line_for<A, T>::type type; // this is the first line with an error
};
code:
1>error C2143: syntax error : missing ';' before '<'
1>see reference to class template instantiation 'line_for_point<A<T>>' being compiled
1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>error C3205: argument list for template template parameter 'A' is missing
1>error C2238: unexpected token(s) preceding ';'
I'm assuming your code is on the cutting edge of template specialization wizardry and outsmarted my compiler.

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.

GrumpyDoctor posted:

I just tried implementing this, and Visual Studio 2010 isn't happy with even this:
C++ code:
template <class T> struct line_for_point;
template <template <class T> class A, class T>
struct line_for_point<A<T> > {
	typedef typename line_for<A, T>::type type; // this is the first line with an error
};
code:
1>error C2143: syntax error : missing ';' before '<'
1>see reference to class template instantiation 'line_for_point<A<T>>' being compiled
1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>error C3205: argument list for template template parameter 'A' is missing
1>error C2238: unexpected token(s) preceding ';'
I'm assuming your code is on the cutting edge of template specialization wizardry and outsmarted my compiler.

No you're using visual studio 2010, which is like 10 years behind the rest of the world in terms of supporting the C++ spec.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

GrumpyDoctor posted:

I'm assuming your code is on the cutting edge of template specialization wizardry and outsmarted my compiler.

It's entirely possible. Do note that the "database" needs to come before line_for_point (or you at least have to forward-declare line_template_for_point_template), despite me confusingly presenting it the other way around.

oRenj9
Aug 3, 2004

Who loves oRenj soda?!?
College Slice
Maybe this is a bit of a noobie question, but which of these is preferable?

code:
// Type A
typedef struct {
	int foo;
	double bar;
} foobar_a;

foobar_a a;

// or Type B
struct foobar_b {
	int foo;
	double bar;
};

struct foobar_b b;
I wasn't aware that type b existed before until I discovered its use in <bits/shm.h>.

b0lt
Apr 29, 2005

oRenj9 posted:

Maybe this is a bit of a noobie question, but which of these is preferable?

code:
// Type A
typedef struct {
	int foo;
	double bar;
} foobar_a;

foobar_a a;

// or Type B
struct foobar_b {
	int foo;
	double bar;
};

struct foobar_b b;
I wasn't aware that type b existed before until I discovered its use in <bits/shm.h>.

It's personal preference, and in C++, they're equivalent (which means use type B). There's also a third option, which allows you to use use both struct foobar_c and foobar_c to refer to the struct:

code:
typedef struct foobar_c {
    int foo;
    double bar;
} foobar_c;

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Note that some compilers may barf on that last one. I typically use:

C++ code:
typedef struct _FooRectangle FooRectangle;

struct _FooRectangle {
  double x;
  double y;
  double width;
  double height;
};

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Suspicious Dish posted:

Note that some compilers may barf on that last one.

Not that I'm doubting you, but what terrible compilers are you using that still barf on that?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

rjmccall posted:

Not that I'm doubting you, but what terrible compilers are you using that still barf on that?

SunPro

Combat Pretzel
Jun 23, 2004

No, seriously... what kurds?!
Any hints as to what math functions are particularly expensive? Despite SSE intrinsics and such?

I just managed to cut the runtime of my oscillator to 45% of the original by replacing a single pow(2,x) with a lookup table based replacement function. Which is surprising how that one function can make such an impact, considering it's the only 5% that isn't additions and multiplications.

Volte
Oct 4, 2004

woosh woosh
pow is pretty much always insanely expensive. pow(2,x) is completely unnecessary anyway since you can just insert the exponent directly into the bits of the floating point number. In fact any time that you are doing pow(C,x) with constant C, you can replace it with exp(x * log(C)) where log(C) is precomputed for a much cheaper evaluation. Chances are that pow is implemented with some combination of exp and log anyway, but computed in higher precision so it'll be even more expensive.

code:
float exp2(int x) {
    int a = (x + 127) << 23;
    return *(float *)&a;
}

int main() {
    // 8.000000 0.500000 32.000000
    printf("%f %f %f\n", exp2(3), exp2(-1), exp2(5));
    return 0;
}

taqueso
Mar 8, 2004


:911:
:wookie: :thermidor: :wookie:
:dehumanize:

:pirate::hf::tinfoil:

Power, trig functions, sqrt. Anything that has to be iterated. You should be wary of math stuff that is more complicated than basic arithmetic and bit-shifting.

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

taqueso posted:

Power, trig functions, sqrt. Anything that has to be iterated. You should be wary of math stuff that is more complicated than basic arithmetic and bit-shifting.

Most trig functions are reasonably cheap on recent x86 (I did a project about a year and a half ago that involved writing fast approximations; the tradeoffs were painful). Also, for the special but useful case of inverse sqrt, there is an SSE intrinsic based on a hardware LUT that costs like 2 cycles (you should never do the Numeric Recipes/quake 3 inverse sqrt routine :( ).

Combat Pretzel
Jun 23, 2004

No, seriously... what kurds?!

quote:

pow(2,x) is completely unnecessary anyway since you can just insert the exponent directly into the bits of the floating point number.

Forgot to mention, the exponents are floating point and can be negative.

Combat Pretzel fucked around with this message at 06:09 on Oct 31, 2012

floWenoL
Oct 23, 2002

Volte posted:

code:
float exp2(int x) {
    int a = (x + 127) << 23;
    return *(float *)&a;
}

This is undefined behavior.

(Use a union.)

an skeleton
Apr 23, 2012

scowls @ u

floWenoL posted:

This is undefined behavior.

(Use a union.)

What exactly does that function return...? I'm not familiar with the syntax used there in the return statement. Like, why are there parentheses and 2 asterisks.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Don't worry about it for now. You'll get around it to eventually.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

an skeleton posted:

What exactly does that function return...? I'm not familiar with the syntax used there in the return statement. Like, why are there parentheses and 2 asterisks.

The stuff in parentheses is casting to float *.

(It's also the wrong way to do that thing)

an skeleton
Apr 23, 2012

scowls @ u
Just realized I may have been unclear. I know what a pointer is and that * is used to make a pointer, but not sure why there was double * and a &.

Anyways, thanks, googling "casting" now :)

The Gripper
Sep 14, 2004
i am winner
If it makes you feel any better I know what pointers, casts and references are and still have no clue what it's supposed to be doing since I don't know the math behind it.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

an skeleton posted:

I know what a pointer is and that * is used to make a pointer

Not quite. There's two different uses of *, there. One is part of a type declaration, like float *a, and the other is known as the dereference operator. In C (C++ is kind of mean and silly about this with references), type syntax dictates usage.

If I have an int i, &i will give me a pointer pointing to i. The type of that is a pointer: int *p = &i. I can get the value back with the * operator: *p.

Casting is an override mechanism. The compiler will note that the variable is one type, and you say "oh compiler, I'm smarter than you. The type of this variable is actually this".

If you cast an integer to a float, there's actually a special rule mandated by C that says that you have to convert it to the appropriate float representation, so (float)3 will give you back a float that represents 3.0 as close as it can possibly can. Note that the bytes that make up these two values are completely different: one is simply 00 00 00 03, and the other is some [url=http://en.wikipedia.org/wiki/IEEE_floating_point]fancy weird version of binary scientific notation[/fixed] where you have a decimal part and then an exponent.

What Volte is trying to do is trick the compiler into using the raw bytes of the integer into the appropriate floating point number. Technically, that's undefined by the C standard, but every compiler and platform will work with it anyway, so it's a common trick. I don't know why.

an skeleton
Apr 23, 2012

scowls @ u

Suspicious Dish posted:

Not quite. There's two different uses of *, there. One is part of a type declaration, like float *a, and the other is known as the dereference operator. In C (C++ is kind of mean and silly about this with references), type syntax dictates usage.

If I have an int i, &i will give me a pointer pointing to i. The type of that is a pointer: int *p = &i. I can get the value back with the * operator: *p.

Casting is an override mechanism. The compiler will note that the variable is one type, and you say "oh compiler, I'm smarter than you. The type of this variable is actually this".

If you cast an integer to a float, there's actually a special rule mandated by C that says that you have to convert it to the appropriate float representation, so (float)3 will give you back a float that represents 3.0 as close as it can possibly can. Note that the bytes that make up these two values are completely different: one is simply 00 00 00 03, and the other is some [url=http://en.wikipedia.org/wiki/IEEE_floating_point]fancy weird version of binary scientific notation[/fixed] where you have a decimal part and then an exponent.

What Volte is trying to do is trick the compiler into using the raw bytes of the integer into the appropriate floating point number. Technically, that's undefined by the C standard, but every compiler and platform will work with it anyway, so it's a common trick. I don't know why.

So the first * is a type declaration and the second one (in the parentheses) is the dereference operator, which casts?

The Gripper
Sep 14, 2004
i am winner
Other way around.

e; the being in brackets part is what makes it cast, not the *, to clarify. (float)6 will cast the integer 6 to a float 6.0000....

The Gripper fucked around with this message at 07:38 on Oct 31, 2012

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

an skeleton posted:

So the first * is a type declaration and the second one (in the parentheses) is the dereference operator, which casts?

The first one is the dereference operator. Let's take this one step at a time. Given an int foo:

&foo - Make a pointer to the integer, so the type of this value is int *

(float *)&foo - Take the int * we got, and cast it into a float *

*(float *)&foo - Dereference the float *, giving us a float with the same byte pattern as foo, according to every real-world compiler out there. According to C, the cast from int * to float * is undefined, meaning it could make a program which writes a funny knock knock joke to stdout, it could call the Mayans and tell them to blow up the world, or anything else it wants to.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
The correct way to "reinterpret" the integer bits as a float is to use a union - which instead of being undefined behavior is merely implementation defined.

an skeleton
Apr 23, 2012

scowls @ u
Ahhh, I see. So the dereferencer is like a trick to make it work? I'm not certain what giving it the same "byte pattern" means.

The Gripper
Sep 14, 2004
i am winner
It's probably best to not try and learn about dereferencing and casting from that example since it exists only to do something you're probably not going to ever want to do.

Adbot
ADBOT LOVES YOU

an skeleton
Apr 23, 2012

scowls @ u
Ok, cool, just seemed like some interesting concepts. I remember my teacher doing casting in a different way, although it is possible I remember wrongly.

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