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
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
If someone were writing an emulator in C++, I can see them getting confused as to why some of the elements in their enum of opcodes were illegal or whatever, but that's kind of a stretch :v:

Adbot
ADBOT LOVES YOU

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Suspicious Dish posted:

Yes. But if you have a variable named "not", you deserve to get your code broken.

Not that uncommon if you're building an AST or similar, in my limited experience. (And "compl" is out of bounds too?)

hooah
Feb 6, 2006
WTF?

Subjunctive posted:

Not that uncommon if you're building an AST or similar, in my limited experience. (And "compl" is out of bounds too?)

Yeah, I ran into this on a school project this semester. We were making a composite class hierarchy for logical expressions, and we just named all of our classes things like "Imp", "Neg", and of course, "And" and "Or". Visual Studio didn't bat an eye, but when we finished and transferred it to the school's system and tried to compile, we got a bunch of errors about identifiers or keywords or whatever g++ wanted to say about them.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Ah, yeah, I forgot about that; that's a valid use case. Just use not_ and move on.

OneEightHundred
Feb 28, 2008

Soon, we will be unstoppable!
Digraphs/trigraphs/alternate tokens are the kind of thing that should have been implemented at the text editor level, not the language level.

Suspicious Dish
Sep 24, 2011

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

OneEightHundred posted:

Digraphs/trigraphs/alternate tokens are the kind of thing that should have been implemented at the text editor level, not the language level.

You're thinking about it wrong. What if your source file is stored in a character encoding which flat out doesn't have representations for those tokens?

That's obviously a non-issue these days, but it was a big issue when these features were added. And the correct response of "just use a not dumb character encoding" wasn't good enough for the committee.

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

Suspicious Dish posted:

What if your source file is stored in a character encoding which flat out doesn't have representations for those tokens?
Then you can take your EBCDIC or whatever and go on writing FORTRAN, COBOL and PL/1, in whatever cave you crawled out of. :colbert:

fankey
Aug 31, 2001

( moved this from the iOS thread because I think it's more of a posix sockets issue... )

I'm having an issue sending Multicast UDP packets from an iOS device that is connected to a Wifi network without a DNS server configured. When connected to a network without DNS I get an error returned from sendto and errno = 51 : network is unreachable. I can fix the issue by setting the DNS server to any valid IP on the network ( including the IP of the iOS device itself ) even if the IP is not a DNS server but I'd rather not force my users to do that. Here's some example code which shows the issue.
code:
struct sockaddr_in dst;

dst.sin_family = AF_INET;
dst.sin_port = htons(2467);

inet_pton(AF_INET, "224.0.23.175", &dst.sin_addr);

NSString* str = @"this is a message";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

ssize_t ret = sendto( sock, data.bytes, data.length, 0, (struct sockaddr*)&dst, sizeof(dst));
if( ret == - 1 )
{
  NSLog(@"error sending %i : %s", errno, strerror(errno));
}
else
{
  NSLog(@"send ok!");
}
I'm guessing the issue might be that for whatever reason the routing table doesn't have a Multicast Route set up if there is no DNS server ( not sure why that'd matter ) but the problem is that there isn't a way to manually add the Multicast Route in iOS.

nielsm
Jun 1, 2009



fankey posted:

( moved this from the iOS thread because I think it's more of a posix sockets issue... )

I'm having an issue sending Multicast UDP packets from an iOS device that is connected to a Wifi network without a DNS server configured. When connected to a network without DNS I get an error returned from sendto and errno = 51 : network is unreachable. I can fix the issue by setting the DNS server to any valid IP on the network ( including the IP of the iOS device itself ) even if the IP is not a DNS server but I'd rather not force my users to do that. Here's some example code which shows the issue.

Can you send regular UDP broadcast (i.e. destination 255.255.255.255) when the device has no configured DNS server?

I'm suspecting that perhaps iOS makes an assumption that if no DNS server is configured then you simply aren't on any network and then cuts short any attempt at doing network stuff. It could be a sort of power-savings measure.

fankey
Aug 31, 2001

nielsm posted:

Can you send regular UDP broadcast (i.e. destination 255.255.255.255) when the device has no configured DNS server?

I'm suspecting that perhaps iOS makes an assumption that if no DNS server is configured then you simply aren't on any network and then cuts short any attempt at doing network stuff. It could be a sort of power-savings measure.

I can receive multicast UDP, send/receive unicast UDP and make TCP/HTTP connections without any issues. The only thing that doesn't work is sending multicast UDP.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

fankey posted:

I can receive multicast UDP, send/receive unicast UDP and make TCP/HTTP connections without any issues. The only thing that doesn't work is sending multicast UDP.

Is this because Bonjour coopts multicast UDP? I remember having weird interactions between DNS and mcast UDP on OS X, but it was a long time ago. (mcast routes weren't set up until Bonjour initialized, and with broken DNS it failed, or something.)

I think this is probably an Apple-related thing, but I'm not certain.

Valithan
Mar 2, 2003
So this is baby level stuff/is probably ugly code, but I'm stumped. I've been working through a C++ textbook on my own and doing the programming exercises in each chapter and am having problems with this one. The part of the code that isn't working for me is this block:


for (x = 1; x <= floors; x++)
{
if (x != 13)
{
cout << "How many rooms are on floor " << x << "? ";
cin >> rooms;
cout << "How many rooms are occupied on floor " << x << "? ";
cin >> occupied;
total_occupied += occupied;
total_rooms += rooms;
}
}



Basically, this block is supposed to tally how many rooms/occupied rooms there are total across all floors of a hotel, except for the 13th floor ( the number of floors is previously defined by the user). Every time I run the program I get a wild number for total_rooms, but occupied_rooms is appropriate. For example, if I input 14 for 'floors' and enter 1 room per floor and 1 occupied room per floor the end values are:

total_rooms is 52 (should be 13)
total_occupied is 13

I'm sure it's something really obvious that's starting me in the face, but I can't figure out what the problem is.

pseudorandom name
May 6, 2007

You are initializing total_rooms to 0, right?

nielsm
Jun 1, 2009



First a tip, use the code tag to mark up program code, it tends to make it easier to read and prevents smilies from accidentally taking over some sequences. You can also get it to do syntax-highlighting:
pre:
[code=cpp]
// code goes here
// (the "cpp" is for C Plus Plus)
[/code]
Result:
C++ code:
for (x = 1; x <= floors; x++)
{
  if (x != 13)
  {
    cout << "How many rooms are on floor " << x << "? ";
    cin >> rooms;
    cout << "How many rooms are occupied on floor " << x << "? ";
    cin >> occupied;
    total_occupied += occupied;
    total_rooms += rooms;
  }
}
Looks much neater!


Either way, there's a few possible things that could be biting you. First, you should post all of your code, including your variable declarations. The types you use for the variables can be important, even if you don't think so yourself.
What I think is the most likely right now, however, is that you are forgetting to initialize total_occupied and total_rooms, so instead of starting out as 0 they start out with a "random" value.

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!

nielsm posted:

What I think is the most likely right now, however, is that you are forgetting to initialize total_occupied and total_rooms, so instead of starting out as 0 they start out with a "random" value.
For which the real error is that you haven't turned your warnings on high, or you're ignoring one.

Valithan
Mar 2, 2003
Wow, thanks for the quick responses. I was literally just about to copy and text the code in here and realized I had initialized the values like this:

C++ code:
total_rooms, total_occupied = 0;
So I changed it to:

C++ code:
total_rooms = 0;
total_occupied = 0;
And everything is working fine now. Thanks for the help! Also I assume the bit about having warnings on high refers to having the compiler give more warnings about possibly poorly written bits?

raminasi
Jan 25, 2005

a last drink with no ice

Valithan posted:

Also I assume the bit about having warnings on high refers to having the compiler give more warnings about possibly poorly written bits?

Yep. If you post your compiler people can give you recommended warning settings.

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!

Valithan posted:

And everything is working fine now. Thanks for the help! Also I assume the bit about having warnings on high refers to having the compiler give more warnings about possibly poorly written bits?
Yeah, "using an uninitialized variable" is a fairly standard thing to get a warning about, and would have saved you asking a question and feeling silly. :)

I don't understand why compiler defaults aren't "crapload of warnings". I could see why for backwards compatibility they wouldn't want craploads of warnings and warnings treated as errors, but the worst thing warnings would do is sound a bit scary when you're compiling someone else's old code.

Dren
Jan 5, 2001

Pillbug
I'm going to be starting a project in C++ that has to be windows/linux cross platform. I think I'm going to go with gcc on linux. Can anyone give me pros and cons for different compiler choices on windows? I guess I'm looking at gcc w/ mingw and visual studio. Also, does anyone have any good solutions for 3rd party dependencies like boost? Am I just gonna have to build everything I need because windows doesn't have package management?

edit: I don't do very much windows dev so this might be a ridiculous concern but I don't want to lock myself out of using MS APIs, if that's a concern at all with the gnu toolchain.

Dren fucked around with this message at 16:06 on Apr 24, 2014

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Generally, my best experiences have been:

- use VS on Windows, because you get a debugger that work
- build from the command line either with nmake or mingw-make
- package everything you depend on, including the CRT redist and DirectX installer

The mozilla-build package has a pile of stuff all bundled up that can help a lot; it's what Mozilla uses for the Win32 development side of Firefox and friends, and I always start with it now because I get bash and python and the paths all set up etc etc.

https://wiki.mozilla.org/MozillaBuild

jbetten
Dec 30, 2009

Dren posted:

I'm going to be starting a project in C++ that has to be windows/linux cross platform. I think I'm going to go with gcc on linux. Can anyone give me pros and cons for different compiler choices on windows? I guess I'm looking at gcc w/ mingw and visual studio. Also, does anyone have any good solutions for 3rd party dependencies like boost? Am I just gonna have to build everything I need because windows doesn't have package management?

edit: I don't do very much windows dev so this might be a ridiculous concern but I don't want to lock myself out of using MS APIs, if that's a concern at all with the gnu toolchain.

GCC for Linux and MinGW for Windows would work fine if you want to go the free route.

With MinGW you use the native Windows APIs, it isn't Cygwin which turns Windows into a Posix like system.

Dren
Jan 5, 2001

Pillbug

Subjunctive posted:

Generally, my best experiences have been:

- use VS on Windows, because you get a debugger that work
- build from the command line either with nmake or mingw-make
- package everything you depend on, including the CRT redist and DirectX installer

The mozilla-build package has a pile of stuff all bundled up that can help a lot; it's what Mozilla uses for the Win32 development side of Firefox and friends, and I always start with it now because I get bash and python and the paths all set up etc etc.

https://wiki.mozilla.org/MozillaBuild

Thanks, I'm thinking about using Vagrant along with Chef to configure the build environment so maybe I'll have it drop MozillaBuild on the Windows environment.

Any preference for a build system? I'm thinking about looking at CMake since it will create a visual studio solution and I guess I can nmake that.

Edison was a dick
Apr 3, 2010

direct current :roboluv: only

Dren posted:

I'm going to be starting a project in C++ that has to be windows/linux cross platform. I think I'm going to go with gcc on linux. Can anyone give me pros and cons for different compiler choices on windows? I guess I'm looking at gcc w/ mingw and visual studio. Also, does anyone have any good solutions for 3rd party dependencies like boost? Am I just gonna have to build everything I need because windows doesn't have package management?

edit: I don't do very much windows dev so this might be a ridiculous concern but I don't want to lock myself out of using MS APIs, if that's a concern at all with the gnu toolchain.

I don't program for windows, but I understand you can cross-compile from Linux to Windows with MinGW.
If I were to do windows programming, this is what I would do, but that's because I prefer programming in a Linux environment.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Edison was a dick posted:

I don't program for windows, but I understand you can cross-compile from Linux to Windows with MinGW.
If I were to do windows programming, this is what I would do, but that's because I prefer programming in a Linux environment.

In my experience this is the falsest of economies. Even if you do all your development on Linux, compile on Windows to test and distribute. Please. For the children.

Sauer
Sep 13, 2005

Socialize Everything!
You can get Visual C++ Express which will let you have a really good debugger and IDE at no cost. Microsoft's C++ compiler, CL, is also available for no cost on its own if you want. Unfortunately it doesn't use the same compile flags as GCC/Clang but the documentation for it is extensive. A multi-platform build system like CMake or Premake can really help; you setup your project using their system and they spit out makefiles for pretty much any platform you like (GNU makefiles, Visual Studio project files, XCode project files, etc...). There's also nothing stopping you from using Visual C++ Express to code and debug then compile with GCC under MinGW if you want to go that route.

As for CL specifically... its very fast and produces pretty good optimized code. Its not fully compliant with C++11 if you're using that but supports most of the major features aside from constexpr and variadic templates. Microsoft has their own cludgy way of doing variadic templates using huge macros that are supposed to "just work"; never tried. It supports the .Net facilities of Managed C++ if you ever want to use that and is the only way to produce "Modern Windows" applications (Metro) using C++. I don't know if the libraries provided with MinGW support some of the more modern Window's APIs like DirectX 10/11 and so forth so you'll need to see if that's an issue for you.

Sauer fucked around with this message at 22:50 on Apr 24, 2014

MutantBlue
Jun 8, 2001

DSauer posted:

As for CL specifically... its very fast and produces pretty good optimized code. Its not fully compliant with C++11 if you're using that but supports most of the major features aside from constexpr and variadic templates. Microsoft has their own cludgy way of doing variadic templates using huge macros that are supposed to "just work"; never tried.

Visual C++ 2013 does support real variadic templates. C++11 support in the compiler and standard library are much improved over VS2010/2012

Support For C++11 Features (Modern C++)

Paul MaudDib
May 3, 2006

TEAM NVIDIA:
FORUM POLICE
Is there a way to declare a union at a specific, preexisting memory address?

I know how to declare one out of the blue, but what's the syntax for taking an arbitrary pointer and putting a union there?

Paul MaudDib fucked around with this message at 00:41 on Apr 25, 2014

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

Paul MaudDib posted:

Is there a way to declare a union at a specific, preexisting memory address?

I know how to declare one out of the blue, but what's the syntax for taking an arbitrary pointer and putting a union there?

code:
union foo *bar = 0xwhatever;
I assume you're doing something with memory-mapped I/O?

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

Paul MaudDib posted:

Is there a way to declare a union at a specific, preexisting memory address?

I know how to declare one out of the blue, but what's the syntax for taking an arbitrary pointer and putting a union there?

Cast to a pointer of the union type, use memcpy, use placement new (in C++)... What you're asking is unclear, and, by most reasonable interpretations, it's going to be undefined behavior (which in this case will typically trigger a segfault and terminate your program). What are you trying to accomplish with this? Could you show us what you mean with a sample of code?

Paul MaudDib
May 3, 2006

TEAM NVIDIA:
FORUM POLICE

Otto Skorzeny posted:

code:
union foo *bar = 0xwhatever;
I assume you're doing something with memory-mapped I/O?

Working with __shared__ memory in CUDA. You have to allocate what you need in one big block and then slice it up yourself, and I need part of it to be a union.

taqueso
Mar 8, 2004


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

:pirate::hf::tinfoil:

edit: posted too slow, not really applicable

taqueso fucked around with this message at 01:40 on Apr 25, 2014

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

Paul MaudDib posted:

Working with __shared__ memory in CUDA. You have to allocate what you need in one big block and then slice it up yourself, and I need part of it to be a union.

Sounds like you want an allocator, a data structure that'll keep track of that memory for you. An arena is probably simplest. Here's a dumb example of one in C.

C code:
struct arena {
	size_t used;
	size_t capacity;
};

struct arena* new_arena(void *start, size_t capacity) {
	if (start == NULL) return NULL;
	if (capacity < sizeof(struct arena)) return NULL;

	struct arena *a = (struct arena*)start;
	a->used = sizeof(struct arena);
	a->capacity = capacity;
	return a;
}

void* arena_malloc(struct arena *a, size_t size) {
	if (a == NULL) return NULL;
	if (a->used + size > a->capacity) return NULL;

	void* tmp = a->used + (void*)a;
	a->used += size;
	return tmp;
}

void arena_free(struct arena* a, void* p) {
	// do nothing
}
It actually stores itself in the block of memory. How stingy! :v:

When you ask for memory from it, it returns a pointer within the block of memory you create it with, and forever treats that memory as used. It never actually frees anything, but more complicated allocators would actually try and reuse freed memory.

Here's an example of it in practice. Substitute block and sizeof(block) with whatever your memory block's address and size is.

C code:
union foo {
	int a;
	float b;
	char c;
};

int main() {
	// say I have a block of memory all my own
	char block[0x10000];

	// I can make an allocator that will manage it for me
	struct arena *a = new_arena(block, sizeof(block));
	if (a == NULL) return EXIT_FAILURE;

	// let's get a union foo sized slice from it
	union foo *u = arena_malloc(a, sizeof(union foo));
	if (u == NULL) return EXIT_FAILURE;
	u->a = 3;
	u->c = '3';
	u->b = 3.1415;
	arena_free(a, u);

	// cleanup "block" if needed
}

The Laplace Demon fucked around with this message at 12:37 on Apr 26, 2014

Kristler
Apr 19, 2014

The Laplace Demon posted:

C code:

struct arena* new_arena(void *start, size_t capacity) {
	if (start == NULL); // oops
	if (capacity < sizeof(struct arena)); // also oops
	//...
}

Are return's needed for these two clauses?

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

Kristler posted:

Are return's needed for these two clauses?

Yeah, NULLs or something would probably be good if you want to actually handle errors. Went ahead and threw the returns in.

The Laplace Demon fucked around with this message at 13:05 on Apr 26, 2014

Namen
Mar 23, 2004
BEES! BEES IN THE CAR! BEES EVERYWHERE!
So, to those of you accustomed to digging through crash dumps looking for leaks, I've got a question.

Preface:

Our allocator is build on top of Intel tbb, which I believe sits on top of VirtualAlloc. This means that when I look at our dumps in WinDbg, our allocations live in <unknown> memory regions, not in heaps. I wrote a script to walk all of the <unknown> memory regions looking for vftables, and I'm getting some weird results.

Question:

So, I'm getting a very high amount of vftable hits from classes that are pure virtual. I'm not sure how this is possible when pure virtual classes can't be instantiated.

I have a suspicion. There are a few classes that derive from the pure virtual base that seem to not have virtual dtors. I'm wondering if we are deleting instances of these derived classes via base class pointers and leaking them. Aside from that, I can't think of what would cause this many hits for pure virtual vftable points to exist in a dump.

Thoughts?

Tetraptous
Nov 11, 2004

Dynamic instability during transition.
I think CMake is a great tool, but I thought it was pretty difficult to learn. There are a bunch of crappy tutorials you can find via Google that describe how to throw together a trivial project, but anything more and you're basically on your own. The official CMake documentation is basically just a reference manual that fine if you just need a refresher on the syntax for doing something you already know CMake can do, but there are no examples or even general explanations for how to do common things. I guess my point is CMake has a steep learning curve if you want to do anything useful with it, mostly because of a lack of good resources not because the system itself is all that complicated. So don't get discouraged! If you're doing anything cross-platform, it's pretty great and definitely worth learning.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Namen posted:

So, I'm getting a very high amount of vftable hits from classes that are pure virtual. I'm not sure how this is possible when pure virtual classes can't be instantiated.

I have a suspicion. There are a few classes that derive from the pure virtual base that seem to not have virtual dtors. I'm wondering if we are deleting instances of these derived classes via base class pointers and leaking them. Aside from that, I can't think of what would cause this many hits for pure virtual vftable points to exist in a dump.

Dynamic types are weird during construction and destruction. Essentially, when you're in the constructor or destructor for a base class B, all the polymorphic features behave as if B were the most-derived type. That's true even if B is abstract; calling a method that's pure virtual in B during B's construction/destruction just causes a runtime failure.

The easiest way to implement that is to basically overwrite all of B's v-table pointers with the correct tables for B at the start of its constructor (after any of its base classes have been constructed) and destructor (as the very first thing).

This gets really complicated when you have virtual base classes, and MSVC's solution is just hilariously space-inefficient and yet still broken.

Anyway, the net result is that v-table pointers in deallocated objects pretty much always point to the v-tables for the root class. It actually works out pretty well as a way to catch use-after-free errors.

Namen
Mar 23, 2004
BEES! BEES IN THE CAR! BEES EVERYWHERE!

rjmccall posted:

Anyway, the net result is that v-table pointers in deallocated objects pretty much always point to the v-tables for the root class. It actually works out pretty well as a way to catch use-after-free errors.

But should these deallocated objects still show up in the dump? I suppose it's possible that our allocator just marks the memory as free and reusable and isn't explicitly memsetting zeros or some other pattern over old data, which could in theory lead to false positives here. That sort of goes along with your statement about catching use-after-free errors, as calling methods on these pointers would throw pure virtual exceptions.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Most allocators don't overwrite freed memory by default, no; it's a lot of extra cycles and bus traffic for no real purpose except to catch bugs earlier.

Adbot
ADBOT LOVES YOU

pseudorandom name
May 6, 2007

It'd be useful and cheap to NULL out the vtable pointer, though.

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