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!
You don't get the JIT debugger with Visual C++ Express, IIRC

Adbot
ADBOT LOVES YOU

cronio
Feb 15, 2002
Drifter

Avenging Dentist posted:

I don't see why you'd run in debug mode unless you have breakpoints set. Running without debug (Ctrl+F5) will keep the command prompt open, and if you compiled in debug mode, the JIT debugger will load up if something goes haywire anyway.

Because most people who have never programmed before don't know the difference? After all, it's the exact problem the person who asked the question was having.

Anyway, Ctrl+F5 works here as well, but if you're writing an *interactive* console app a cin.get() to pause the app is not exactly verboten.

Cross_
Aug 22, 2008

Avenging Dentist posted:

I don't see why you'd run in debug mode unless you have breakpoints set. Running without debug (Ctrl+F5) will keep the command prompt open, and if you compiled in debug mode, the JIT debugger will load up if something goes haywire anyway.
The debugger is nice enough to take you to the line where exceptions occured - no breakpoints necessary. I was not aware of the system("pause") trick, but for a test program that seems fine. Seriously, why even bring ncurses into the discussion if you are just starting out with C++ ?

Ru
Mar 25, 2005
Hi,

I would like to create a CComboBox using MFC, where the EditBox part of the CComboBox remains a constant size, while the font size of the drop down box (ComboLBox?) is larger than that of the editbox, which would change the overall size of the drop down box.

Now I think the only way to do this is to create an "Owner draw" Combo box, as I have been through a few example of Font List Combo boxes from various sources. However, when I try to implement any "owner draw" box in my own case, I get Debug Assertion Errors, as all the examples I have seen create the combo box in the resource view (using the IDE) and I don't want to do this, I want to create the combobox in the program code.

I have tried many time to create my own class which inherits from CComboBox, then try and perform myOwnComboBox->Create(...) methods, but to no avail. Mainly because I really don't understand what is required of an owner drawn box. But I have read through alot of documentation online, but none of it its much help.

SO can anyone suggest any directions to take?

(using Visual Studio C++ 2008 btw)
Cheers!

Olly the Otter
Jul 22, 2007
I've got some very simple code which generates an inexplicable error if I compile it using GCC in Linux, but Visual Studio 2005 compiles it with no problems.

Here is the code, and the error I'm getting:

code:
$ cat test1.cpp
#include <list>

template <typename _Elem> 
class MyObject
{
public:
  void DoSomething(void)
  {
    std::list<int>::iterator intListIterator;       // ok
    std::list<_Elem> elementList;                   // ok
    std::list<_Elem>::iterator elementListIterator; // error
  }
};

$ g++ -c test1.cpp
test1.cpp: In member function &#8216;void MyObject<_Elem>::DoSomething()&#8217;:
test1.cpp:11: error: expected `;' before &#8216;elementListIterator&#8217;
I've tried to compile this with GCC version 4.1.2 under CentOS 5.2, as well as with GCC version 3.4.6 under RedHat Enterprise 4, and get the same error on both machines.

Is there some standard that I'm breaking here, which VS just happens to allow while GCC is more strict? If so, how can I work around this? If it were only the most recent version of GCC then I'd suspect a compiler bug, but with two different major versions giving the same results, that seems unlikely.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
You need the "typename" keyword before before the third declaration because list<_Elem>::iterator is dependent upon your template parameter. Without saying "typename" there's actually no guarantee that list<_Elem>::iterator is a type at all. Some jerk could have made a specialized version of list where iterator is a member variable, or doesn't even exist.

Olly the Otter
Jul 22, 2007
That worked, thanks!

slovach
Oct 6, 2005
Lennie Fuckin' Briscoe
Ok, I feel retarded.

Shouldn't something like:
code:
(rand()%(120 - 80) + 80);
Generate a range from... 80 to 120?

TheSleeper
Feb 20, 2003
Mod is a terrible way to get rands in a specific range. You'll pretty much always wind up with the range being biased to certain values.

Instead, get your rand into the range 0 to 1(i.e. (double)rand()/RAND_MAX), then multiply that to get a number in your range.

ZorbaTHut
May 5, 2005

wake me when the world is saved

TheSleeper posted:

Mod is a terrible way to get rands in a specific range. You'll pretty much always wind up with the range being biased to certain values.

Instead, get your rand into the range 0 to 1(i.e. (double)rand()/RAND_MAX), then multiply that to get a number in your range.

Technically speaking, this doesn't remove the bias, just evenly distributes it among the elements. This might be great if you're doing damage rolls in a roleplaying game, but if you're trying to simulate selecting one out of N items, this isn't any better than the modulus approach. If you want a completely unbiased random number generator it's a bit trickier to pull off :)

My feeling is that there are very few situations where "mod" is too inaccurate but "double and divide" is accurate enough.

That said: yes, the original code will generate a range from 80 inclusive to 120 exclusive, assuming your rand() isn't ridiculously broken. (Did you remember to seed it?)

TheSleeper
Feb 20, 2003

ZorbaTHut posted:

Technically speaking, this doesn't remove the bias, just evenly distributes it among the elements. This might be great if you're doing damage rolls in a roleplaying game, but if you're trying to simulate selecting one out of N items, this isn't any better than the modulus approach. If you want a completely unbiased random number generator it's a bit trickier to pull off :)

My feeling is that there are very few situations where "mod" is too inaccurate but "double and divide" is accurate enough.

That said: yes, the original code will generate a range from 80 inclusive to 120 exclusive, assuming your rand() isn't ridiculously broken. (Did you remember to seed it?)

Uh, exactly how can a bias be evenly distributed? Isn't that the opposite of bias?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
rand is usually typed to return an int, and C doesn't require a particular semantics for the % operator when applied to negative dividends. Many platforms use modulus semantics, i.e. (x/y,x%y)=(a,b) where 0<=b<y and ay+b=x, but it's also permitted to use remainder semantics, i.e. |ay|<=|x| and -y<b<y. If you're seeing a range of 40-120, this may be why.

EDIT: to be precise, C requires / and % to be consistent (i.e. (x/y)*y+x%y=x), and it requires the absolute value of x % y to be less than the absolute value of y whenever y is non-zero, and it furthermore requires x % y to be non-negative whenever x is, but that's it.

rjmccall fucked around with this message at 12:24 on Oct 17, 2008

Vanadium
Jan 8, 2005

rand is also guaranteed to return a non-negative int. What are you getting at?

TSDK
Nov 24, 2003

I got a wooden uploading this one

TheSleeper posted:

Uh, exactly how can a bias be evenly distributed? Isn't that the opposite of bias?
I think Zorba's just referring to the fact that not all implementations of rand() have a uniform distribution. In fact, some of the most common implementations are shockingly bad.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Vanadium posted:

rand is also guaranteed to return a non-negative int. What are you getting at?

So it is, my mistake.

UraniumAnchor
May 21, 2006

Not a walrus.
What's MSVC's equivalent to gcc's "attribute (constructor)"? (Makes a function run at program startup, before main()).

Avenging Dentist
Oct 1, 2005

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

UraniumAnchor posted:

What's MSVC's equivalent to gcc's "attribute (constructor)"? (Makes a function run at program startup, before main()).

If you're using DLLs, there's _DLL_InitTerm

Vanadium
Jan 8, 2005

Just put it in the constructor of a global object :colbert:

Atom
Apr 6, 2005

by Y Kant Ozma Post

TheSleeper posted:

Uh, exactly how can a bias be evenly distributed? Isn't that the opposite of bias?

let's pretend rand_max is 13 and we need a number from 0-9

(double)rand() / RAND_MAX * 10 will be any of the following values

0/13 = 0.0 = 0
10/13 = 0.77 = 0
20/13 = 1.53 = 1
30/13 = 2.31 = 2
40/13 = 3.07 = 3
50/13 = 3.84 = 3
60/13 = 4.62 = 4
70/13 = 5.38 = 5
80/13 = 6.15 = 6
90/13 = 6.92 = 6
100/13 = 7.69 = 7
110/13 = 8.46 = 8
120/13 = 9.23 = 9
130/13 = 10.0 = 10

See the bias? It biases towards 0, 3 and 6. They are twice as likely as any other number.

edit: I feel the need to point out that this bias decreases as RAND_MAX gets larger. But it's still there!

Atom fucked around with this message at 00:57 on Oct 18, 2008

citsejam
Dec 24, 2005

<3
Accidentally posted this in the general questions, so here we go again:

In C:

quote:

char c = 148;
printf("%i", c);

Rather than outputting 148, it gives me -108. On closer inspection, I seem to be getting incorrect values when an ASCII character's integer value is => 100. Is something wrong with my libraries, or am I retarded/missing something?

Cross_
Aug 22, 2008

quote:

Rather than outputting 148, it gives me -108. On closer inspection, I seem to be getting incorrect values when an ASCII character's integer value is => 100.
%i, as the letter implies, is expecting an int. You are feeding it a char, that leaves printf with taking "random" values from nearby memory to fill in the missing 3 bytes.

dis astranagant
Dec 14, 2006

citsejam posted:

Accidentally posted this in the general questions, so here we go again:

In C:


Rather than outputting 148, it gives me -108. On closer inspection, I seem to be getting incorrect values when an ASCII character's integer value is => 100. Is something wrong with my libraries, or am I retarded/missing something?

char is probably stored as a signed 8 bit integer. -108 is the 2's complement of 148 in 8 bits.

dis astranagant fucked around with this message at 03:56 on Oct 18, 2008

Yeroc2
Aug 13, 2003

"The glow is the combination of all your past lives, focusing their energy through your body."
Grimey Drawer
I've got an assignment to write a C/C++ program that produces a COM file that will output "Hello World" when ran. However I have no idea how to make a program that produces a COM file. The only advice I got way that a COM file is just a memory image, and that I can use all the DOS APIs I want.

I tried googling but I have a feeling I am googling the wrong thing.

floWenoL
Oct 23, 2002

Cross_ posted:

%i, as the letter implies, is expecting an int. You are feeding it a char, that leaves printf with taking "random" values from nearby memory to fill in the missing 3 bytes.

This is completely wrong.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

citsejam posted:

Rather than outputting 148, it gives me -108. On closer inspection, I seem to be getting incorrect values when an ASCII character's integer value is => 100. Is something wrong with my libraries, or am I retarded/missing something?

Your system uses signed characters, meaning "char" values may hold integers from -128 to 127. 148 falls outside this range, so it's interpreted as negative according to 2's compliment.

You want something like this:

code:
unsigned char c = 148;
printf("%i", c);

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
http://forums.somethingawful.com/showthread.php?threadid=2779598&pagenumber=29#post350837583

Ok, guys, can we please stop talking about this.

Also, for the curious, varargs like in printf promote integral values to ints and floating point values to doubles. (Sometimes this is a lie; I'm pretty sure IA64 promotes integral values to longs).

Thug Bonnet
Sep 22, 2004

anime tits ftw

Is it possible to forward declare derived child classes? I would think no, but it seems to make taking full advantage of covariant return types pretty difficult if your base classes are dependent on one another.

foobase.h
code:
#ifndef _foobase_h_
#define _foobase_h_

class BarBase;

class FooBase{
	virtual BarBase * returnBar(BarBase * _bar);
};

#endif
barbase.h
code:
#ifndef _barbase_h_
#define _barbase_h_


class FooBase;

class BarBase{
	virtual FooBase * returnFoo(FooBase * _foo);
};

#endif
If I then want to overload those methods using covariant types, obviously there's going to be a problem since I can't forward declare the lineage of the derived types, and I can't use their includes, since it would be circular. The #ifndef will keep it from wigging out the compiler, but I'll still end up with things undefined.

fooderived.h
code:
#ifndef fooderived_h_
#define fooderived_h_

#include "foobase.h"

class FooDerived: public FooBase{
	BarDerived * returnBar(BarBase * _bar); // Can't define BarDerived
};

#endif
barderived.h
code:
#ifndef barderived_h_
#define barderived_h_

#include "barbase.h"

class BarDerived: public BarBase{
	FooDerived * returnFoo(FooBase * _foo); // Can't define FooDerived
};

#endif

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Why would you think that class FooDerived; is bad? It's 100% legal.

Thug Bonnet
Sep 22, 2004

anime tits ftw

Avenging Dentist posted:

Why would you think that class FooDerived; is bad? It's 100% legal.

Forward declaring FooDerived is just fine, but the problem is that as far as I can tell any notions of lineage require definition, not just declaration, so it can't be used to redefine FooBase * BarBase::returnFoo(FooBase * _foo) as FooDerived * BarDerived::returnFoo(FooBase * _foo)

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Ah, I see what you mean by "covariant return types". Those are strictly illegal in C++ (as well they should be, since they open up a can of worms with respect to overloading). That's just not how C++ works; apparently it's legal in Java, but I honestly don't see the point anyway.

Thug Bonnet
Sep 22, 2004

anime tits ftw

Avenging Dentist posted:

Ah, I see what you mean by "covariant return types". Those are strictly illegal in C++ (as well they should be, since they open up a can of worms with respect to overloading). That's just not how C++ works; apparently it's legal in Java, but I honestly don't see the point anyway.

They're legal in C++ according to the spec though apparently it was a long time before MSVC/GCC supported them. I can see the can of worms they open, but on the other hand they can also enforce some safety too. They were intended to reduce the need for dynamic_cast-ing, but they don't really.

I figured I would be fancy and use them, but that's why I was surprised since they are apparently way less useful than they were intended (and Bjorn says as much in "Design and Evolution of C++", but I hadn't had occasion to try them out until now.

I figured I must be doing it wrong, so that's seriously a bummer imo :(

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
I got it working, if you care. Here's the trick:

code:
class nil{};

template<typename T,typename Base = nil>
class wrapper : public wrapper<Base>
{
public:
	wrapper() {}
	wrapper(T *p) : wrapper<Base>(p) {}
	T * get() { return (T*)wrapper<Base>::p; }
};

template<typename T>
class wrapper<T,nil>
{
public:
	wrapper() {}
	wrapper(T *p) : p(p) {}
	T * get() { return p; }
protected:
	T *p;
};
Use wrapper<Foo>* and wrapper<FooDerived,Foo>* as return types (obviously this requires more in the way of memory management, but what can you do :()

This might not even be strictly valid C++, I only tested it in MSVC 8. If you want all the source files, I'll send them to you, but the above is the relevant bit.

EDIT: I managed to make it appear transparent to the user but it's all just memory allocation tricks so it's nothing special.

Avenging Dentist fucked around with this message at 08:06 on Oct 20, 2008

Thug Bonnet
Sep 22, 2004

anime tits ftw

Avenging Dentist posted:

I got it working, if you care. Here's the trick:

code:
class nil{};

template<typename T,typename Base = nil>
class wrapper : public wrapper<Base>
{
public:
	wrapper() {}
	wrapper(T *p) : wrapper<Base>(p) {}
	T * get() { return (T*)wrapper<Base>::p; }
};

template<typename T>
class wrapper<T,nil>
{
public:
	wrapper() {}
	wrapper(T *p) : p(p) {}
	T * get() { return p; }
protected:
	T *p;
};
Use wrapper<Foo>* and wrapper<FooDerived,Foo>* as return types (obviously this requires more in the way of memory management, but what can you do :()

This might not even be strictly valid C++, I only tested it in MSVC 8. If you want all the source files, I'll send them to you, but the above is the relevant bit.

Yeah, I'm pretty sure that's not valid, but it's quite a hack, seriously that's clever :)

Avenging Dentist
Oct 1, 2005

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

Thug Bonnet posted:

Yeah, I'm pretty sure that's not valid, but it's quite a hack, seriously that's clever :)

Well, it compiles without warning in MSVC and GCC with -Wall -pedantic. :)

The trick really is just that wrapper<T,Base> asserts that T is a subclass of Base (except in the case where Base = nil, obviously), and then wraps up the necessary cast into the class.

Jarl
Nov 8, 2007

So what if I'm not for the ever offended?
Why is it that when having a diamond form (which of course one should avoid to begin with) a cast demands an in between cast to tell which of the class branches you take? It seems unnecessary since I end up with the same no matter if I use (class4*)(class3*) or (class4*)(class2*).

code:
class class1
{
public:
	void hej();
};

void class1::hej()
{
	cout << "class1" << endl;
}

class class2 : public class1
{
public:
	void hej();
};

void class2::hej()
{
	cout << "class2" << endl;
}

class class3 : public class1
{
public:
	void hej();
};

void class3::hej()
{
	cout << "class3" << endl;
}

class class4: public class2, public class3
{
public:
	void hej();
};

void class4::hej()
{
	cout << "class4" << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	class1 *obj = (class2*) new class4();

	class4 *obj2 = (class4*)(class3*) obj;

        return 0;
}

Vanadium
Jan 8, 2005

Jarl posted:

I end up with the same no matter if I use (class4*)(class3*) or (class4*)(class2*).

You do not.

code:
#include <iostream>

struct A {};
struct B : A {};
struct C : A {};
struct D : B, C {};

int main() {
  D d;
  A& a_1 = (B&) d;
  A& a_2 = (C&) d;

  std::cout << &a_1 << ", " << &a_2 << std::endl;
}
You do not actually have diamond inheritance because both the class2 and class3 subobjects of the class4 object have their own class1 subobject. For actual diamond inheritance you need to make class1 a virtual base class of class2 and class3, I think, which makes the whole thing even more annoying because you have to explicitly call the virtual base class subobject's constructor in the most derived constructor.

Edit: http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8

Vanadium fucked around with this message at 14:13 on Oct 22, 2008

Jarl
Nov 8, 2007

So what if I'm not for the ever offended?
Thank you, that was very help full.

fankey
Aug 31, 2001

Is there a good C++ based open-source unzip library? I just need to extract a bunch of files from a zip to a given location. zlib doesn't appear to handle zip files directly and minizip is lacking documentation and looks more complicated than I need. Ideally I just want to call Unzip( sourceZip, destinationPath ). Oh, it needs be Win32 compatible as well.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Minizip takes like 10 lines of code to unzip something, just use that. :colbert:

Adbot
ADBOT LOVES YOU

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'm having a hard time understanding how malloc'd memory is freed: I understand you can free it explicitly with free(), but I've seen contradicting information as to whether a malloc'd piece of memory that's being pointed to is freed when the program terminates. So, do I have to call free() for everything, or just in case I want to change the pointer that points to the malloc'd memory?

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