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
Bitruder
Nov 29, 2003

Majoring in connect the dots and colouring

GrumpyDoctor posted:

What's more conventional when using classes out of the STL: "using namespace std;" in each .cpp file in which it matters, individually importing the symbol for each class you're using ("using std::vector", "using std::list" or whatever), or explicitly qualifying every occurrence of the symbol ("for (std::vector<int>::iterator p...")? Is there even a convention? I feel weird pulling in entire namespaces to save myself typing.
I think that while pulling an entire namespace is usually overkill, it's not uncommon to do this with std since it's used so frequently in so many different source files.

Adbot
ADBOT LOVES YOU

Plinkey
Aug 4, 2004

by Fluffdaddy

julyJones posted:

I'm not familiar with the type of development you're doing, but when I used VB 6 on a past project, I had to run "regsvr32" to get Windows to register any third-party DLL's I wanted to load.
http://technet.microsoft.com/en-us/library/bb490985.aspx

I figured it out, turns out that the dll I was using relied on an older dll for some functionality. I ran it through dependency walker and it let me know what was wrong....I also have to wrap the functions using DllImport, and define the entry point of the dll.

Orbitize
Apr 28, 2008
I am quite new to C++, so this might be a stupid question for many of you, but I hope I will get some help on this issue anyway.

I am trying to pass data between two applications, the first one is based on QT, and the second one on DarkGDK. The QT application will supply data (coordinates mostly) for displaying 3D data in the second application.

I have tried googling and asking friends, but no one seems to have a good solution for this. So far, the two options I have considered are:

1. QT opens file, writes to file, closes file. Display opens file, reads, closes.

2. They share(?) a socket, and pass data with this.

The first option I would be able to implement myself, but I suspect it will be much too slow for operation in main-loops. The second one, unfortunately, I have no clue how to do.

How hard is it to implement a socket system? Would I be able to pass data-structs/classes with it? Will I need to load external libraries or such?

Could you please give me some hints, and perhaps point me in the right direction?
Thanks guys!

Edit: Oh, win32 system, using Visual C++ 2008.

Orbitize fucked around with this message at 20:52 on Feb 2, 2009

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
Sockets are one of the easier ways to do IPC. Windows' implementation of them, sometimes called Winsock, is generally compatible with regular Berkely sockets (notable incompatibilities: you need to call closeSocket(sockfd) rather than just close(sockfd), and you need to set up a WSAData structure beforehand; also, you only include winsock.h rather than sys/socket.h netinet/in.h etc). Googling "socket IPC" should get you pretty far (in fact the first result for this looked pretty good at a glance). Beej's guide is also a pretty good overview of sockets, but isn't focused on IPC.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
What about memory-mapped files?

ehnus
Apr 16, 2003

Now you're thinking with portals!
What about named pipes?

edit: here's the list of the IPC methods that Windows supports: http://msdn.microsoft.com/en-us/library/aa365574(VS.85).aspx

UberJumper
May 20, 2007
woop
Ive got an object that has a private variable of string.

I was told to do this:

class Foo
{
private:
const std::string playerName;

public:
Foo(const std::string& _playerName)
:playerName(_playerName)
{
}

};

Is there a better way to do this?

Avenging Dentist
Oct 1, 2005

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

Thug Bonnet
Sep 22, 2004

anime tits ftw

Avenging Dentist posted:

Do what?

I assume set playername from the constructor argument, the question being should you use an initialization list, and the answer being yes.

UberJumper
May 20, 2007
woop
Errr sorry i was exhusted when i wrote that.

Basically we have an object that needs to store a string, this string will never change.

So is the way above the best way to do this?

newsomnuke
Feb 25, 2007

UberJumper posted:

Errr sorry i was exhusted when i wrote that.

Basically we have an object that needs to store a string, this string will never change.

So is the way above the best way to do this?
Yes, it's also pretty much the only way of doing it.

raminasi
Jan 25, 2005

a last drink with no ice
I'm trying to break some class method definitions out of a header file into their own .cpp file, but for some reason, including the original .h file in this new .cpp file causes the compiler to generate 70 dumb "missing type specifier" errors (and others indicative of compiler confusion) in a completely different header file, and I can't figure out why. I don't even have to have any code at all in the new .cpp file for the problem to occur; just a single line: #include "ProblematicHeader.h" will do it. This header is included fine from other files in the project (both other .h and .cpp files), and everything relevant seems to be properly include-guarded.

I'm completely at a loss for how to even start figuring out what's wrong.

Avenging Dentist
Oct 1, 2005

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

GrumpyDoctor posted:

I'm completely at a loss for how to even start figuring out what's wrong.

So are we because you didn't post any code. :colbert:

I don't know, maybe make sure that the header isn't using stuff that's declared in another header that you aren't including?

raminasi
Jan 25, 2005

a last drink with no ice

Avenging Dentist posted:

So are we because you didn't post any code. :colbert:

I don't know, maybe make sure that the header isn't using stuff that's declared in another header that you aren't including?

okay here is the problematic code
code:
#include "GSTTypes.h"
And yes, that was the problem. For some reason GSTTypes.h doesn't include everything it needs. I am still learning my way around some of these compiler errors I guess!

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.
Which is worse, an Avenging Dentist or a Grumpy Doctor?

POKEMAN SAM
Jul 8, 2004

Mustach posted:

Which is worse, an Avenging Dentist or a Grumpy Doctor?

Which is worse at programming? :D

Thug Bonnet
Sep 22, 2004

anime tits ftw

GrumpyDoctor posted:

And yes, that was the problem. For some reason GSTTypes.h doesn't include everything it needs. I am still learning my way around some of these compiler errors I guess!

Are you sure you just don't have your includes set up in a different directory structure than GSTTypes.h is expecting?

raminasi
Jan 25, 2005

a last drink with no ice

Thug Bonnet posted:

Are you sure you just don't have your includes set up in a different directory structure than GSTTypes.h is expecting?

Yeah, unless I'm misunderstanding you. Everywhere else GSTTypes.h was included, there were two other includes just before it, and if I included GSTTypes.h without these includes as well, GSTTypes.h wouldn't compile, but the compiler wouldn't figure this out until it was trying to put some other file together. Moving those includes into GSTTypes.h itself fixed the problem.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
Can someone tell me why this isn't seeing the EOF once it reaches the end?

http://pastebin.com/m2db6e2ec

When I first ran it, it entered an infinite loop of 0's, so I put a getchar() in there and saw that it's not seeing the end of file, so it gets stuck in the inner while loop, since there is obviously no sentinel after the EOF.

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

Anunnaki posted:

Can someone tell me why this isn't seeing the EOF once it reaches the end?

http://pastebin.com/m2db6e2ec

When I first ran it, it entered an infinite loop of 0's, so I put a getchar() in there and saw that it's not seeing the end of file, so it gets stuck in the inner while loop, since there is obviously no sentinel after the EOF.

The EOF flag is only updated after you try to read from a file past the EOF. It looks like that might be your problem.

floWenoL
Oct 23, 2002

Anunnaki posted:

Can someone tell me why this isn't seeing the EOF once it reaches the end?

http://pastebin.com/m2db6e2ec

When I first ran it, it entered an infinite loop of 0's, so I put a getchar() in there and saw that it's not seeing the end of file, so it gets stuck in the inner while loop, since there is obviously no sentinel after the EOF.

it is entirely possible that you're hitting eof between the first check and your inner while loop. your inner while loop should be testing for eof, too.

Also, it is generally a bad idea to mix C (getchar) and C++ I/O (iostreams stuff) calls.

Edit:
Wait, you do know that getchar() reads only from stdin, right? :confused:

floWenoL fucked around with this message at 02:36 on Feb 5, 2009

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

floWenoL posted:

it is entirely possible that you're hitting eof between the first check and your inner while loop. your inner while loop should be testing for eof, too.

Also, it is generally a bad idea to mix C (getchar) and C++ I/O (iostreams stuff) calls.

Edit:
Wait, you do know that getchar() reads only from stdin, right? :confused:

Oh, okay, I just added an if statement within the loop to check for the EOF, and it works now. Thanks. :)

Also, I'm using getchar() because it doesn't need another library (like _getch()). Is there another one I can use?

Edit: VVVVVV
Awesome, thanks.

Cosmopolitan fucked around with this message at 03:05 on Feb 5, 2009

Avenging Dentist
Oct 1, 2005

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

Anunnaki posted:

Also, I'm using getchar() because it doesn't need another library (like _getch()). Is there another one I can use?

iostream::get

FearIt
Mar 11, 2007
Hi CoC,

I'm trying to use CImg to grab a section of my desktop screen on a Windows XP machine. There's got to be a better way than using the Windows API to take a screenshot, save it to a bmp and then load it with CImg.

Any ideas on how I should go by doing this? Hopefully in a very quick way that avoids the hard drive.

floWenoL
Oct 23, 2002

FearIt posted:

Hi CoC,

I'm trying to use CImg to grab a section of my desktop screen on a Windows XP machine. There's got to be a better way than using the Windows API to take a screenshot, save it to a bmp and then load it with CImg.

Any ideas on how I should go by doing this? Hopefully in a very quick way that avoids the hard drive.

Surely you have access to the bitmap in memory before you dump it to disk? CImg has constructors that take in a raw bitstream and reads it given dimensions, stride, and color format. It looks like the Win32 API functions return a 24-bit bitmap, so it should be fairly straightforward to get the necessary params from the bitmap header and pass them to CImg.

Lexical Unit
Sep 16, 2003

I'm trying to figure out how to fix Boost.Optional's InputStreamable implementation. What I mean is, I think any given optional type should behave as much like the type it wraps as possible. For example:
code:
// This:
stringstream sin ("1.2.3.4.5");
optional<double> token;
while (sin >> token) cout << token << endl;

// Should have the same output as this:
stringstream sin ("1.2.3.4.5");
double token;
while (sin >> token) cout << token << endl;
Also this should work (as of boost 1.37.0 it does not):
code:
stringstream sin ("test");
optional<string> token;
sin >> token; // fails to read "test", instead we get an empty optional type
However I think that using an optional type should offer a little more than just that (call me crazy). Since it's semantically valid to have an un-initialized optional type, I think failure to extract something off the stream should return an empty optional type without setting the failbit on the stream. So like:
code:
// outputs 1 nan nan nan nan 5.5 nan nan nan nan nan nan nan -3.2 nan
istringstream sin (" 1 this 5.5 is a test -3.2 a");
optional<double> token;
const double nan = numeric_limits<double>::quiet_NaN ();
while (sin >> token) cout << token.get_value_or (nan) << " ";

// outputs 1
istringstream sin (" 1 this 5.5 is a test -3.2 a");
double token;
while (sin >> token) cout << token << " ";
I think I've figured out an implementation for operator>>() that gives me all this, but I'm unsure if it has hidden bugs or is just pain disgusting implementation wise. I'm still pretty shaky on the nuts-and-bolts of streams in general. Here it is:
code:
template<class T>
std::istream& operator>>(std::istream& in, optional<T>& v)
{
    if (in)
    {
        T x;
        if (in >> x)
        {
            v = x;
        }
        else
        {
            if (in.fail () && !in.eof ())
            {
                in.clear ();
                in.get ();
            }
            v = optional<T> ();
        }
    }
    else
    {
        v = optional<T> ();
    }
    return in;
}
[edit to update with better solution]

So c++ gurus, does this idea seem reasonable? Else on a scale from 1 to 10 (with 10 being Whitesmiths style indentation) how horrible is my code? :downs:

Lexical Unit fucked around with this message at 21:31 on Feb 5, 2009

Thug Bonnet
Sep 22, 2004

anime tits ftw

floWenoL posted:

Surely you have access to the bitmap in memory before you dump it to disk? CImg has constructors that take in a raw bitstream and reads it given dimensions, stride, and color format. It looks like the Win32 API functions return a 24-bit bitmap, so it should be fairly straightforward to get the necessary params from the bitmap header and pass them to CImg.

Yeah you can get the DIB rather than write it to disk iirc.

f37u5.eX
Dec 23, 2004

by Peatpot
int ival = 65;
int iaddr = (int) &ival;


for(int i = 0; i < sizeof(int); i++)
{
printf("%08x: %02x\n", (iaddr + i), (* (char*)(iaddr + i)));
}

I was given this piece of code and I am supposed to explain what its doing, I understand that its printing the address of ival in hex to 8 places. but I don't understand what (* (char*)(iaddr + i)) is doing. What does * (char*) mean? What is it doing?

Vanadium
Jan 8, 2005

Not even compile is what it does :colbert:

Edit: Assuming C99, it segfaults.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender
It's clearly supposed to print out each byte of ival by doing some pointer casts, but they screwed up the position of the (char *) cast, so instead it accesses undefined memory.

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 clearly supposed to print out each byte of ival by doing some pointer casts, but they screwed up the position of the (char *) cast, so instead it accesses undefined memory.

Actually that's not true at all. The address of ival is cast to an int, so iaddr+1 refers to one byte past iaddr, not one word past.

The code should actually work on 32-bit systems, but will crash on a 64-bit system because int isn't large enough to hold a pointer.

Vanadium
Jan 8, 2005

Avenging Dentist posted:

Actually that's not true at all. The address of ival is cast to an int, so iaddr+1 refers to one byte past iaddr, not one word past.

Last I checked, iaddr+1 might as well refer to your mom. You do not get to do pointer arithmetic on things that are not pointers.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Vanadium posted:

Last I checked, iaddr+1 might as well refer to your mom. You do not get to do pointer arithmetic on things that are not pointers.

If you mean that strictly speaking it's undefined under the standard, then yes, this is invalid because you are attempting to read an object through a pointer of incompatible type; there is no reason why a C implementation couldn't arbitrarily declare that all accesses through int* will actually be offset by 17 bytes because of spite. If you mean that there are implementations of the standard where this won't work for reasons other than AD's point that they should be using intptr_t, well, I would be fascinated to see a constructive proof of your statement.

Avenging Dentist
Oct 1, 2005

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

ISO/IEC 9899:1999 6.3.2.3 posted:

5. An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.56

6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

56) The mapping functions for converting a pointer to an integer or an integer to a pointer are intended to be consistent with the addressing structure of the execution environment.

So this is, necessarily, a discussion of implementations, not specifications.

shodanjr_gr
Nov 20, 2007
I think that I've got a pointer in my code screwing things up. The thing is, it's not causing a segmentation error, its probably corrupting something inside my app's own memory space and is affecting data in such a way that the app does not break, rather than behave weirdly.

Is there a tool a-la valgrind that makes sure that when ever a pointer is used to access something, that something actually matches the pointer's type?

Any other tips and tricks on diagnosing these kinds of errors in a big-ish app? (a few thousand lines of code).

Super Dude
Jan 23, 2005
Do the Jew
Alright I have a quick question that is really annoying me. Say I have a 16 bit number, and I need to get bits 10-13, how do I do this? I was thinking that I could do x >> 10 & 0xD but that isn't working.

That Turkey Story
Mar 30, 2003

Super Dude posted:

Alright I have a quick question that is really annoying me. Say I have a 16 bit number, and I need to get bits 10-13, how do I do this? I was thinking that I could do x >> 10 & 0xD but that isn't working.

0xD is binary 1101, you meant 0xF which is binary 1111. If you have boost, you don't have to worry about messing stuff like this up since you can just do BOOST_BINARY( 1111 ) to get your mask.

julyJones
Feb 12, 2007

Stopped making sense.

Super Dude posted:

Alright I have a quick question that is really annoying me. Say I have a 16 bit number, and I need to get bits 10-13, how do I do this? I was thinking that I could do x >> 10 & 0xD but that isn't working.

I'm not sure what you mean to do with what you've got so far, but it sounds like what you want to do is make a mask. Draw out 16 bits and set 0's on the bits you don't want and 1's on the bits you do want:
00000000 00111100
That comes out to 0x3c . Then use bitwise and to get just the bits you want:
unsigned short goodBits = mask & val;
You can check if a given bit is set similarly:
bool isSet = (0x8000 >> bitNumToCheck) & val;


efb :P

Also, is there a place where all these awesome Boost features are cataloged?

julyJones fucked around with this message at 03:16 on Feb 8, 2009

Avenging Dentist
Oct 1, 2005

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

julyJones posted:

Also, is there a place where all these awesome Boost features are cataloged?

Yes, the Boost documentation. Where else would it be?

Adbot
ADBOT LOVES YOU

That Turkey Story
Mar 30, 2003

julyJones posted:

Also, is there a place where all these awesome Boost features are cataloged?

Here?

BOOST_BINARY is documented here.

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