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
ehnus
Apr 16, 2003

Now you're thinking with portals!
Sounds like it's a legitimate warning. Any particular reason you narrowed those indices?

Adbot
ADBOT LOVES YOU

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

ehnus posted:

Sounds like it's a legitimate warning. Any particular reason you narrowed those indices?

It didn't take much work to change them from int to char, due to the way my source is laid out. All of these variables are small numbers and could never be more than about 100, so there was no worry about them getting bigger than 127 and wrapping around into negative numbers. And the reason why I chose to do it was just that there are quite a lot of them and it seemed prudent not to use four times as much memory for them as was needed. For example, one of them is an eight by five by forty-three tridimensional array of integers. That's quite a lot of numbers, none of them (in this case) bigger than about 25, so making them char instead of int seemed a good idea.

ehnus
Apr 16, 2003

Now you're thinking with portals!
I'd be willing to bet that due to member padding within data types that the savings you see by narrowing those types won't actually be realized

Scaevolus
Apr 16, 2007

ehnus posted:

I'd be willing to bet that due to member padding within data types that the savings you see by narrowing those types won't actually be realized
This.

If your "optimization" causes a bunch of warnings, saves less than 1KB of RAM, and is probably slower, it's not worth it.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
"Premature optimization is the root of all evil." - Donald Knuth

magicalblender
Sep 29, 2007
I want to populate a set with objects, but I'm getting unusual errors.

code:
#include <set>

class coordinate{
	int x;
	int y;
};

void main(){
	std::set<coordinate> mypoints;
	coordinate foo;
	mypoints.insert(foo); //Note: the error does not appear when I comment this out.
}
The errors:

code:
Error E2093 c:\Borland\Bcc55\include\function.h 169: 'operator<' not implemented in
type 'coordinate' for arguments of the same type in function 
less<coordinate>::operator ()(const coordinate &,const coordinate &) const
Error E2093 c:\Borland\Bcc55\include\function.h 169: 'operator<' not implemented in
type 'coordinate' for arguments of the same type in function 
less<coordinate>::operator ()(const coordinate &,const coordinate &) const
Warning W8057 c:\Borland\Bcc55\include\function.h 169: Parameter 'x' is never used 
in function less<coordinate>::operator ()(const coordinate &,const coordinate &) const
Warning W8057 c:\Borland\Bcc55\include\function.h 169: Parameter 'y' is never used 
in function less<coordinate>::operator ()(const coordinate &,const coordinate &) const
*** 2 errors in Compile ***
I overloaded the < operator...
code:
coordinate::operator<(coordinate right){
	return (x < right.x);
}
... But the errors are unchanged. What is the cause of this problem?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
operator< needs to be a const member function.

magicalblender
Sep 29, 2007
Ah, so it's "bool coordinate::operator<(const coordinate &right) const{" Of course! I see now that the error message basically told me what I needed to implement. Too bad my brain skipped over that last "const" every time I read it. Thanks :)

Avenging Dentist
Oct 1, 2005

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

magicalblender posted:

Ah, so it's "bool coordinate::operator<(const coordinate &right) const{" Of course! I see now that the error message basically told me what I needed to implement. Too bad my brain skipped over that last "const" every time I read it. Thanks :)

Correct. Technically it's not required to make right a const reference, but it's good practice since it eliminates a superfluous copy.

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.

Hammerite posted:

Hey again.

I recently changed some of the variables I am using from int to char. Everything works fine but every time it compiles, the compiler spits out hundreds of lines that say "(filename).cpp:(line number): warning: array subscript has type `char'". A preliminary google search suggests that this is to warn the programmer in case he hasn't realised that a signed char can be a negative number.

I apologise for asking what is probably something very simple to fix, but is there a way to stop it warning me about this?
What's already been said about this is true. The reason for the warnings is that char is not guaranteed to be signed or unsigned, unlike int. If you still want char variables, declare them as either signed or unsigned (or static_cast to one of those when you do the indexing) and the warnings will go away.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Mustach posted:

What's already been said about this is true. The reason for the warnings is that char is not guaranteed to be signed or unsigned, unlike int. If you still want char variables, declare them as either signed or unsigned (or static_cast to one of those when you do the indexing) and the warnings will go away.

Thank you very much. I made them all "unsigned char" instead of "char" and the warnings have disappeared.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
I have a new question, about member functions of classes. I have worked with constructors before, but I have not worked with other member functions of classes.

In the file classes.h there is the following definition of a class, "button":

code:
class button {
    int positionx;
    int positiony;
    int sizex;
    int sizey;
    bool exists;
    public:
    HWND bhwnd;
    button (int,int,int,int,bool);
    void ensurebutton(int,int,int,int);
    void destroybutton();
};
and in the file functions.cpp, which has classes.h #included, there are the following definitions of the constructor and the two member functions of "button":

code:
button::button (int a, int b, int c, int d, bool x) {
    positionx = a;
    positiony = b;
    sizex = c;
    sizey = d;
    exists = x;
}

void button::ensurebutton (int x,int y,int p,int q) {
    [ do some stuff ]
}

void button::destroybutton () {
    [ do some other stuff ]
}
Further down functions.cpp there are functions that include lines like the following:

code:
buttons[BUTTONX].destroybtn();
and

code:
buttons[BUTTONX].ensurebtn(0,0,0,0);
BUTTONX in each case is a macro equal to some integer. buttons is an array of objects of the class "button".

Yet when I try to compile this, for each line like the above I get the following error messages:

"'class button' has no member named 'destroybutton'"
"'class button' has no member named 'ensurebutton'"

Why does it say that there's no such function when there clearly is, and I've even defined the function further up in the same file?

POKEMAN SAM
Jul 8, 2004

Hammerite posted:

Why does it say that there's no such function when there clearly is, and I've even defined the function further up in the same file?

I can't see any obvious problems, but would you mind sticking the .CPP and .H files on Pastebin or something so we can see it all in context?

That Turkey Story
Mar 30, 2003

Hammerite posted:

In the file classes.h there is the following definition of a class, "button":

code:
class button {
    int positionx;
    int positiony;
    int sizex;
    int sizey;
    bool exists;
    public:
    HWND bhwnd;
    button (int,int,int,int,bool);
    void ensurebutton(int,int,int,int);
    void destroybutton();
};
...

Further down functions.cpp there are functions that include lines like the following:

code:
buttons[BUTTONX].destroybtn();
and

code:
buttons[BUTTONX].ensurebtn(0,0,0,0);

destroybtn != destroybutton

ensurebtn != ensurebutton

Also, why are you using a macro for BUTTONX? Just use a constant if you need a constant.

POKEMAN SAM
Jul 8, 2004

That Turkey Story posted:

destroybtn != destroybutton

ensurebtn != ensurebutton

Bah, how'd I miss that :(

KaeseEs
Feb 23, 2007

by Fragmaster

Ugg boots posted:

Bah, how'd I miss that :(

Upgrade your copy of vdiff

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Oh god, how stupid :cry: well, thanks!

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

That Turkey Story posted:

destroybtn != destroybutton

ensurebtn != ensurebutton

Also, why are you using a macro for BUTTONX? Just use a constant if you need a constant.

There are lots of different macros, about 50 in all. I am using them so I can insert a new entry somewhere in the array "buttons", and then just update all of the macros above the appropriate number instead of finding everywhere they're used in the file.

The reason why I am using an array is that there are some times where I have a line that looks like

code:
for (m=0;m<5;m++) { buttons[ GAMEYESNOSETTINGSBTNS + m ].destroybtn(); }
where I want to do something to several of the "buttons" at once using a single command.

KaeseEs
Feb 23, 2007

by Fragmaster

Hammerite posted:

There are lots of different macros, about 50 in all. I am using them so I can insert a new entry somewhere in the array "buttons", and then just update all of the macros above the appropriate number instead of finding everywhere they're used in the file.

Using a constant instead of a macro doesn't in any way affect your ability to do this

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

KaeseEs posted:

Using a constant instead of a macro doesn't in any way affect your ability to do this

But it does, because if I have lines in lots of different places that say, for example,

code:
buttons[PAGE2BTN].ensurebtn(0,0,0,0);
then at the moment PAGE2BTN might be equal to 17, because it's the 18th element of the array "buttons" that I want to operate on using this command. But if I want to insert a new element of the array before the 18th, then suddenly PAGE2BTN ought to be equal to 18, not 17. So I just go and change what PAGE2BTN stands for.

But if I just had

code:
buttons[17].ensurebtn(0,0,0,0);
in lots of different places, I'd have to go and change them all.

KaeseEs
Feb 23, 2007

by Fragmaster
What? How is
code:
const int PAGE2BTN = 0xdefec8ed;
any harder to change than
code:
#define PAGE2BTN 0xdeadbeef
I think you might be confusing constants with literals.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

KaeseEs posted:

What? How is
code:
const int PAGE2BTN = 0xdefec8ed;
any harder to change than
code:
#define PAGE2BTN 0xdeadbeef
I think you might be confusing constants with literals.

I guess I must be, at least I definitely have the impression now that I'm confusing some terms.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
An array may not be the ideal data structure for this application.

Doctor Goat
Jan 22, 2005

Where does it hurt?
I am working on continuing/forking a game and I have a severe problem.

The original devs were, quite possibly, on crack.

Is there any recommended program out there to clean code with? The program's written in C++ very erratically but I really, really don't wanna rewrite it.

KaeseEs
Feb 23, 2007

by Fragmaster

The Cursed Seeker posted:

I am working on continuing/forking a game and I have a severe problem.

The original devs were, quite possibly, on crack.

Is there any recommended program out there to clean code with? The program's written in C++ very erratically but I really, really don't wanna rewrite it.

Depends on what kind of crack they were smoking.

If the brace/indent style is psychotic, there are a million pretty-printers that will fix it for you (as well as most editors).

If there are some issues with the design and naming of classes and functions, many editors (esp. Java and C# editors, for whatever reason) have refactoring tools that may assist you with this.

If the architecture is bad, or there's a mishmash of stupid decisions sprinkled through the code, fixing that is AI-hard, and you'll have to do it yourself.

cowboy beepboop
Feb 24, 2001

I wrote a simple little c program that forks and the child execs another little program. Before the fork, I set up an alarm and signal handler. Now, I thought the child was entirely replaced (in memory) by whatever program it execs, yet the child still seems to be terminated by the alarm I set before the fork.

Can someone set me straight please?

TSDK
Nov 24, 2003

I got a wooden uploading this one

my stepdads beer posted:

I wrote a simple little c program that forks and the child execs another little program. Before the fork, I set up an alarm and signal handler. Now, I thought the child was entirely replaced (in memory) by whatever program it execs, yet the child still seems to be terminated by the alarm I set before the fork.

Can someone set me straight please?
This page does confirm that the alarm should have been cancelled:
http://www.opengroup.org/onlinepubs/000095399/functions/fork.html

Are you sure you haven't got the parent/child processes mixed up?

cowboy beepboop
Feb 24, 2001

TSDK posted:

This page does confirm that the alarm should have been cancelled:
http://www.opengroup.org/onlinepubs/000095399/functions/fork.html

Are you sure you haven't got the parent/child processes mixed up?

Well they both terminate at the same time (both are writing to stdout continuously). I can't find the child process running after either. Have I written a zombie? :(

TSDK
Nov 24, 2003

I got a wooden uploading this one

my stepdads beer posted:

Well they both terminate at the same time (both are writing to stdout continuously). I can't find the child process running after either. Have I written a zombie? :(
I seem to remember (but my memory on this is a bit dusty) that forked child processes terminate when their parents do. I also seem to remember that there are calls you can make to detach a child so that this doesn't happen, but someone else with more recent experience will have to chime in on that one.

tef
May 30, 2004

-> some l-system crap ->

TSDK posted:

I also seem to remember that there are calls you can make to detach a child so that this doesn't happen.

I think you can also fork twice if you want to detach from the parent.

Edit: that said, if you kill the parent the child should be adopted by init. Something is going wrong here,

tef fucked around with this message at 14:06 on Aug 21, 2008

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.

tef posted:

I think you can also fork twice if you want to detach from the parent.
Who forks twice? That doesn't make any sense; it's possible to have a whole family tree of processes, each having up to some big number of children.

my stepdads beer posted:

Before the fork, I set up an alarm and signal handler.
What does the handler do and what program does the child exec?

cowboy beepboop
Feb 24, 2001

Mustach posted:

What does the handler do and what program does the child exec?

The handler catches SIGALRM and just exits. The child process is just an infinite loop counting from 0 to 9 and writing to stdout.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Mustach posted:

Who forks twice? That doesn't make any sense; it's possible to have a whole family tree of processes, each having up to some big number of children.
What does the handler do and what program does the child exec?

Because when you fork you are responsible for cleaning up your child processes, no ifs ands or buts, but if you end before the child does then the init process takes over. So if you want to just fire-and-forget a child process, what you do is:

code:
fork
in the parent process (A), wait for B to complete and then continue on your merry way
in the child process (B):
  fork again
  in process B, exit immediately
  in the grandchild process (C):
    congratulations!  you're now running with no parent!
You could see process B as a launcher process - its only job is to launch the daemon process C and then exit. That way process A can be blocked waiting for only a very short time and then continue - as far as A's concerned it created a process which then finished its work, so all's ok. As far as B's concerned it finished its task of launching C and exited cleanly. As far as C's concerned, it got launched and then its parent went away, so init takes over and does the right thing.

EDIT: screwed up my terminology a bit, C is not a "daemon" it's just a process running in the background with no parent. daemons have a bunch more conditions they have to meet. Setting up a daemon also involves forking twice, but for a different reason.

JoeNotCharles fucked around with this message at 06:38 on Aug 22, 2008

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.
I get what you mean, but I guess I don't think of that as "forking twice" since each process only forks once.

my stepdads beer posted:

The handler catches SIGALRM and just exits. The child process is just an infinite loop counting from 0 to 9 and writing to stdout.
Okay, I'll try to recreate this later today, unless somebody thinks of a reason for what you're getting.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
I updated the OP with some more links from here: http://forums.somethingawful.com/showthread.php?noseen=0&threadid=2779598&pagenumber=21#post348190891

The Red Baron
Jan 10, 2005

Is there an elegant way of handling the issue of using template templates whose parameter count is not "technically" equal to the template parameter count of the class supplied to it, but where this other class provides default parameters for the non-considered parameters? Okay, that's a pretty lovely description, so consider this instead:

code:
template <template <typename, typename> class Map>
struct foo
{
  typedef Map<std::string, std::string> map_type;
};

typedef foo<std::map> teh; // error: template parameter count mismatch
Now, one could argue that that stuff like predicates and allocators should be included in the typelist, but that won't work for classes such as tr1::unordered_map that expect a different number and/or ordering of parameters.

Until C++0x brings along template typedefs, the only thing I can think of is indirecting the whole thing through some dummy class that lets default parameters actually be used, like so:
code:
template <template <typename, typename> class MapAdapter>
struct foo
{
  typedef typename MapAdapter<std::string, std::string>::type map_type;
};

template <typename Key, typename Value>
struct std_map_adapter
{
  typedef std::map<Key, Value> type;
};

typedef foo<std_map_adapter> teh;
Am I missing an obvious solution? Any thoughts?

That Turkey Story
Mar 30, 2003

The Red Baron posted:

Until C++0x brings along template typedefs, the only thing I can think of is indirecting the whole thing through some dummy class that lets default parameters actually be used, like so:
code:
template <template <typename, typename> class MapAdapter>
struct foo
{
  typedef typename MapAdapter<std::string, std::string>::type map_type;
};

template <typename Key, typename Value>
struct std_map_adapter
{
  typedef std::map<Key, Value> type;
};

typedef foo<std_map_adapter> teh;
Am I missing an obvious solution? Any thoughts?

This method is what is used right now, even in the STL -- that's pretty much how allocator passing and some_allocator::rebind works (allocator instantiations basically double as their own wrapper by design). STL containers don't take template template parameters for their allocator argument even though they logically could. This was done both because template template support was not common in 1998 and because it would mean that your allocator template would require the exact same amount of arguments as was specified in the template parameter list. Both of these problems are solved by adding a level of indirection as in your example.

Avenging Dentist
Oct 1, 2005

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

That Turkey Story posted:

This method is what is used right now, even in the STL -- that's pretty much how allocator passing and some_allocator::rebind works (allocator instantiations basically double as their own wrapper by design). STL containers don't take template template parameters for their allocator argument even though they logically could. This was done both because template template support was not common in 1998 and because it would mean that your allocator template would require the exact same amount of arguments as was specified in the template parameter list. Both of these problems are solved by adding a level of indirection as in your example.

Do you know if variadic templates will resolve this issue, or are variadic templates qualitatively different from n-ary template types?

That Turkey Story
Mar 30, 2003

Avenging Dentist posted:

Do you know if variadic templates will resolve this issue, or are variadic templates qualitatively different from n-ary template types?

Do mean will you be able to pass variadic templates as non-variadic template arguments? I don't immediately see a reason why you shouldn't be able to, but whether you actually can is a different story.

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

That Turkey Story posted:

Do mean will you be able to pass variadic templates as non-variadic template arguments? I don't immediately see a reason why you shouldn't be able to, but whether you actually can is a different story.

Well, I mean that, and the inverse: using a variadic template as a template parameters to take non-variadic template types as arguments. i.e.:

code:
template< template<typename ...> class T >
struct foo
{};

foo<std::map> something_cool;

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