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
FlyingDodo
Jan 22, 2005
Not Extinct
I am creating a bounding volume hierachy which I would like the flexibility to re-use as much code as possible while supporting any type of bounding volume (eg abbox, sphere, capsule) and be able to store anything in it. I have an idea of how I might implement it but I am unsure if I am going the right way about it.

I have not made extensive use of a number of C++ features, in this case templates and function objects. My idea is have a templated volume hierachy with template parameters for the following;

Contents - what is being stored, eg a polygon, a mesh, a player.
Bounds - the bounding object to use, eg aabbox, sphere, capsule.

Then a lot of function object template parameters; one for creating a Bounds object from a Contents object, one for making a new bigger Bounds object that encloses two others, one for classifying Bounds against another such as inside or outside or intersecting, one for tracing a Bounds object by a line.

Is this a reasonable approach? std::set uses function objects in a similar manner, but only one. I don't know of having so many is such a good idea.

Adbot
ADBOT LOVES YOU

baquerd
Jul 2, 2007

by FactsAreUseless
code:
int
UpdateStuff(id, x, y)
long id;
int x, y;
{
  //dostuff
}
Working on some code and I ran into this unusual function definition. Can anyone point me towards documentation that supports this apparently valid syntax?

Vanadium
Jan 8, 2005

K&R style functions :ohdear:

baquerd
Jul 2, 2007

by FactsAreUseless

Vanadium posted:

K&R style functions :ohdear:

Oh, neat. Guess that makes sense since this code is from 1986. Just updated it for Sybase 12.5!

pr0metheus
Dec 5, 2010
I was having issues with boost's dynamic_bitset. In my program I have a tree data structure where each node is an instance of a class which has one dynamic bitset as a member variable. I allocate my tree on heap, and clean it up when my program is done. The issue I am running into is apparent memory leaking from dynamic_bitset's = operator. Valgrind is reporting that new operator that is called as result of calling = operator on dynamic_bitset at some point in course of my program never has an associated delete. I am quite positive that I am cleaning up my entire tree properly, and valgrind doesn't bitch about any node allocation which makes me think I did my job right.

Does anybody else run into this issue? Is it possible I am doing something wrong and forget to delete some nodes? If I did forget to delete nodes then why doesn't valgrind show callstack for allocating that node ( I use --leak-check=full )?

Here are callstacks from valgrind:

valgrind posted:


==11076== 8 bytes in 1 blocks are definitely lost in loss record 2 of 5
==11076== at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
==11076== by 0x805BADF: __gnu_cxx::new_allocator<unsigned long long>::allocate(unsigned int, void const*) (new_allocator.h:89)
==11076== by 0x805A0B3: std::_Vector_base<unsigned long long, std::allocator<unsigned long long> >::_M_allocate(unsigned int) (stl_vector.h:140)
==11076== by 0x8058AC7: unsigned long long* std::vector<unsigned long long, std::allocator<unsigned long long> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<unsigned long long const*, std::vector<unsigned long long, std::allocator<unsigned long long> > > >(unsigned int, __gnu_cxx::__normal_iterator<unsigned long long const*, std::vector<unsigned long long, std::allocator<unsigned long long> > >, __gnu_cxx::__normal_iterator<unsigned long long const*, std::vector<unsigned long long, std::allocator<unsigned long long> > >) (stl_vector.h:963)
==11076== by 0x805644B: std::vector<unsigned long long, std::allocator<unsigned long long> >::operator=(std::vector<unsigned long long, std::allocator<unsigned long long> > const&) (vector.tcc:165)
==11076== by 0x8054565: boost::dynamic_bitset<unsigned long long, std::allocator<unsigned long long> >::operator=(boost::dynamic_bitset<unsigned long long, std::allocator<unsigned long long> > const&) (dynamic_bitset.hpp:631)
==11076== by 0x805386C: DFA_state::DFA_state(boost::dynamic_bitset<unsigned long long, std::allocator<unsigned long long> > const&) (FSG.h:388)
==11076== by 0x804B5BE: FSG::reduceToDFA(FSG&) (RegEx2FSG.cpp:283)
==11076== by 0x804A704: RegEx2FSG::parseString(std::string const&, FSG_OPTIONS const&) (RegEx2FSG.cpp:21)
==11076== by 0x804CF29: RegEx2FSG::buildFSG(std::string const&, std::string const&, FSG_OPTIONS const&) (RegEx2FSG.cpp:475)
==11076== by 0x804F5C2: main (main_app.cpp:30)
==11076==
==11076== 40 bytes in 5 blocks are definitely lost in loss record 3 of 5
==11076== at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
==11076== by 0x805BADF: __gnu_cxx::new_allocator<unsigned long long>::allocate(unsigned int, void const*) (new_allocator.h:89)
==11076== by 0x805A0B3: std::_Vector_base<unsigned long long, std::allocator<unsigned long long> >::_M_allocate(unsigned int) (stl_vector.h:140)
==11076== by 0x8058AC7: unsigned long long* std::vector<unsigned long long, std::allocator<unsigned long long> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<unsigned long long const*, std::vector<unsigned long long, std::allocator<unsigned long long> > > >(unsigned int, __gnu_cxx::__normal_iterator<unsigned long long const*, std::vector<unsigned long long, std::allocator<unsigned long long> > >, __gnu_cxx::__normal_iterator<unsigned long long const*, std::vector<unsigned long long, std::allocator<unsigned long long> > >) (stl_vector.h:963)
==11076== by 0x805644B: std::vector<unsigned long long, std::allocator<unsigned long long> >::operator=(std::vector<unsigned long long, std::allocator<unsigned long long> > const&) (vector.tcc:165)
==11076== by 0x8054565: boost::dynamic_bitset<unsigned long long, std::allocator<unsigned long long> >::operator=(boost::dynamic_bitset<unsigned long long, std::allocator<unsigned long long> > const&) (dynamic_bitset.hpp:631)
==11076== by 0x805386C: DFA_state::DFA_state(boost::dynamic_bitset<unsigned long long, std::allocator<unsigned long long> > const&) (FSG.h:388)
==11076== by 0x804BBC5: FSG::reduceToDFA(FSG&) (RegEx2FSG.cpp:327)
==11076== by 0x804A704: RegEx2FSG::parseString(std::string const&, FSG_OPTIONS const&) (RegEx2FSG.cpp:21)
==11076== by 0x804CF29: RegEx2FSG::buildFSG(std::string const&, std::string const&, FSG_OPTIONS const&) (RegEx2FSG.cpp:475)
==11076== by 0x804F5C2: main (main_app.cpp:30)

pr0metheus fucked around with this message at 20:58 on Jan 4, 2011

UberJumper
May 20, 2007
woop
I need to do a bit of C++ on Mac OS X. For the last few years i have completely stayed away from coding from scratch on any *nix systems. XCode refuses to work properly on my system, and i am somewhat low on time. Is there any other suggestions?

evensevenone
May 12, 2001
Glass is a solid.
If you're short on time, gcc etc works from the command line and should be installed when you install XCode. And there's no shortage of text editors.

If you want to waste a few hours installing stuff and configuring it, Eclipse with the CDT plugin is a fairly well behaved and full-featured IDE although the debugging kinda sucks.

I can't imagine why XCode doesn't work though.

nop
Mar 31, 2010
I hope someone here can help...

I've got a function that intermittently (but consistently) generates an exception. The error is '0xC0000005: Access violation reading location 0xcdcdcdd5.' From what I can tell this is due to an uninitialized pointer. The mind boggling part though is that it happens after the return statement of the function. So I have no idea what's causing the exception. I ran a tracepoint within the function and all variables remain initialized. (I'm using Visual Studio 2008).

I would be immensely grateful for any possible causes.

code:
NetworkData unpackNetData(char * message){
	int type, count, intSize = sizeof(int);
	
	memcpy(&type, message, intSize); 
	memcpy(&count, message + intSize,intSize); 
	size_t dataSize = structSize(type) * count;
	char * data = new char[dataSize]; 
	memcpy(data,message + intSize * 2, dataSize); 
	
	//package the data
	NetworkData package;
	package.data = data;
	package.count = count;
	package.type = type;
	return package;
}//error occurs here...

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy
Why are you returning a struct and not a pointer to a struct, or asking the caller to provide allocated space? I forget the exact semantics, but I believe a struct can only be a return value if it can fit inside a register.

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!
I'm pretty sure you can return a struct like that, but is NetworkData even a struct? (If you couldn't do that then it would be a compile-time error anyway.)

Whatever it is, if it has a destructor then returning that way would be likely to cause a problem like you're experiencing since the destructor will be called as the function closes (and before you do whatever you're doing with the returned thing).

I'd go with Derpes suggestion and pass in a pointer to an object whose contents will be filled by your return values, and just return a boolean or an error code.

Edit: you wouldn't want to return it as a pointer to a struct since it would be a pointer to your stack NetworkData that is, at least nominally, invalid after the function exits.

roomforthetuna fucked around with this message at 03:50 on Jan 8, 2011

nielsm
Jun 1, 2009



That function is a remote exploit waiting to happen, too. You should at the very least also pass the length of the message to the function, so you don't read past the end of it, in case the dataSize field is wrong, intentionally or not.
(That might even be what is really happening.)

Also this:
code:
struct NetworkData {
    int type;
    int count;
    char data[1];
};
char *message = ...;
NetworkData *nd = static_cast<NetworkData*>(message);
While the data field in the struct is declared as just a single byte, neither C nor C++ will complain if you address it past that index. By declaring the struct in network order, you can simply cast the raw data to it and use it directly.
That's quick and dirty.

The safe way is to cross-check everything and copy every field over to something more convenient to work with.
Are you sure you won't have to deal with machines with different byte order (endianness) or different size of the basic int type?

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.

nop posted:

I hope someone here can help...

I've got a function that intermittently (but consistently) generates an exception. The error is '0xC0000005: Access violation reading location 0xcdcdcdd5.' From what I can tell this is due to an uninitialized pointer. The mind boggling part though is that it happens after the return statement of the function.
Please post the calling code and definition of NetworkData.

Derpes Simplex posted:

I believe a struct can only be a return value if it can fit inside a register.
Not true at all.

nielsm posted:

By declaring the struct in network order, you can simply cast the raw data to it and use it directly.
That's quick and dirty.
Unless he's in a world where sizeof int == sizeof char, that's very dirty, because nothing about that struct definition ensures that those ints will be in network byte order (or unpadded). It would also have to be a reinterpret_cast; that static_cast won't compile.

nielsm
Jun 1, 2009



Mustach posted:

Unless he's in a world where sizeof int == sizeof char, that's very dirty, because nothing about that struct definition ensures that those ints will be in network byte order (or unpadded). It would also have to be a reinterpret_cast; that static_cast won't compile.

I wrote network order (order the data fields are transmitted in), not network byte order. I'm fully aware that my code assumes that the byte order of the data from the network is the same as the machine byte order. nop's original code assumes that as well. My code also assumes that packing is set appropriately. Yes I should have mentioned that.
You're right about the cast.

nop
Mar 31, 2010
Thanks for the replies. I've figured it out. I had failed to initialize a critical section and that was the pointer causing the exception. Why it never popped up when EnterCriticalSection was called is beyond me. I found it by copying unpackNetData into it's calling function and when memcpy then gave me the pointer error I replaced memcpy with a for loop. After that the EnterCriticalSection threw the exception and I figured it out from there.

Oh and to answer your questions, I'm only working with windows platforms so I'm not concerned about endianness and initially I was creating a struct on the heap and returning that but given the error I was getting I altered the code. The calling function checks that the message size is correct before unpacking, so there shouldn't be any overflow issues. In either case this code is strictly for mine own use.

Thanks again for your help!

The struct looks like this if you're wondering (really quick and dirty code...)

code:
struct NetworkData{
	int type;
	int count;
	void * data;
};

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.

nielsm posted:

I wrote network order (order the data fields are transmitted in), not network byte order.
Ah okay. Sorry for the mixup.

Quasar
Oct 24, 2010
I'm feeling pretty dumb right now, sorry for bothering you guys with such a basic question...

I'm in an introductory C class and my professor didn't waste any of his time teaching us how to set up a compiler. I've tried a few different programs out from various sites (eclipse, codeblocks, and maybe something else) but even after following their obtuse 8-step installation guides for modern windows machines (running 64bit Windows 7) I can't get them to function properly. Normally I'd probably be able to struggle through a problem like this but I'm feeling sick and hungover.

Can someone cast out a lifeline to me? Is there a simple way to get me writing/running C code?

e:grammar

Cryolite
Oct 2, 2006
sodium aluminum fluoride
Easiest environment to get started with is probably the free C++ express edition of Visual Studio:

http://www.microsoft.com/express/Downloads/#2010-Visual-CPP

nielsm
Jun 1, 2009



Unless you're required to use GCC, setting Visual C++ Express is easy and won't cost you a penny.

Although, if your class is going to include anything about Makefiles you won't be able to follow that since VC++ doesn't have a (fully?) Unix-compatible Make tool.


Or install some Linux distro on some machine, which could be a virtual machine.

nop
Mar 31, 2010
I found MinGW really useful. I think there's a standard installer now. Although when installed it, it just unpacked to a folder and I had to set a path variable to the bin directory. From there I'd just edit my files in notepad++ and compile from the command line.

There may be easier methods out there, but for simple programs this is probably the fastest.

schnarf
Jun 1, 2002
I WIN.

nop posted:

I found MinGW really useful. I think there's a standard installer now. Although when installed it, it just unpacked to a folder and I had to set a path variable to the bin directory. From there I'd just edit my files in notepad++ and compile from the command line.

There may be easier methods out there, but for simple programs this is probably the fastest.

For anybody on Windows, I really think it makes sense to get VC++ Express. It's a really great IDE, and easy to set up.

that awful man
Feb 18, 2007

YOSPOS, bitch

Comedy option.

Brecht
Nov 7, 2009

Quasar posted:

I'm feeling pretty dumb right now, sorry for bothering you guys with such a basic question...

I'm in an introductory C class and my professor didn't waste any of his time teaching us how to set up a compiler. I've tried a few different programs out from various sites (eclipse, codeblocks, and maybe something else) but even after following their obtuse 8-step installation guides for modern windows machines (running 64bit Windows 7) I can't get them to function properly. Normally I'd probably be able to struggle through a problem like this but I'm feeling sick and hungover.

Can someone cast out a lifeline to me? Is there a simple way to get me writing/running C code?

e:grammar
If you're just starting in an intro C course, learning and manipulating the Microsoft tools will be a task arguably more difficult than learning C itself, and I'd bet anything that you'll waste many, many hours in frustration. IMO, the most hassle-free and effective way to set up a C compiler on Windows is to get VirtualBox (or equivalent) and install Ubuntu, then apt-get install gcc. It's a little silly to run a VM just to do this stuff, but in the end you're going to save yourself one million headaches, and you'll be able to devote your time to actually learning the language.

shrughes
Oct 11, 2008

(call/cc call/cc)

Brecht posted:

If you're just starting in an intro C course, learning and manipulating the Microsoft tools will be a task arguably more difficult than learning C itself, and I'd bet anything that you'll waste many, many hours in frustration. IMO, the most hassle-free and effective way to set up a C compiler on Windows is to get VirtualBox (or equivalent) and install Ubuntu, then apt-get install gcc. It's a little silly to run a VM just to do this stuff, but in the end you're going to save yourself one million headaches, and you'll be able to devote your time to actually learning the language.

This is retarded; running Visual Studio is trivially easy.

baquerd
Jul 2, 2007

by FactsAreUseless
I'm monkeying with some C here, the coding's fine but the compiling is giving me a major headache. This is code from the 80's apparently compiled with some version of Forte with hacked libs from another version (which I don't have explicitly, but I have access to like 20 different versions).

Is there any systemic process of debugging I can take here? I'm getting a lot of errors from pcurses, maybe some other version of that library, but it says it's proprietary so that may not help.

I just want to blow it all up and recode it.

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.

Brecht posted:

IMO, the most hassle-free and effective way to set up a C compiler on Windows is to get VirtualBox (or equivalent) and install Ubuntu, then apt-get install gcc.
Are you aware that this is in fact NOT the Coding Horrors thread?

Brecht
Nov 7, 2009

shrughes posted:

This is retarded; running Visual Studio is trivially easy.
Precompiled headers.

/game set match: Brecht

nielsm
Jun 1, 2009



Brecht posted:

Precompiled headers.

/game set match: Brecht

Disable it? VC doesn't force you to use PCH, and the standard AppWiz even asks if you want to use them.

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy
And if you make an empty project it defaults to being off.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

Does DGJPP work under Windows 7?

http://www.delorie.com/djgpp/

Borland used to have their old C/C++ stuff online for free...

I don't imagine making stuff with Visual C++ Express would be that tough, though.

Another alternative would be to buy a $5 VPS (or shell account with C compiler) and have at it. Hell, some goon might be nice enough to just give you an account on their machine if you asked nice enough. Then just PuTTY into the fucker instead of booting a whole VM.

Zhentar
Sep 28, 2003

Brilliant Master Genius
No, Visual Studio isn't that tough. It just seems tough to Linux people who expect Windows development to work the same way and find that Visual Studio doesn't work that way.

darkhand
Jan 18, 2010

This beard just won't do!
You could also download and boot one of the "hacker" linux LiveCDs. There's one of Gentoo that has gcc, vim, and some form of window-manager built into it. Your productivity would probably go through the roof when all you have on the screen is literally just a couple terminals.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

Zhentar posted:

No, Visual Studio isn't that tough. It just seems tough to Linux people who expect Windows development to work the same way and find that Visual Studio doesn't work that way.

From what I remember it's a piece of cake to get it to make a console app to do "hello world" and reverse a string homework assignments.

Hell, there's always stuff like codepade and ideone if you're that crazy.

evensevenone
May 12, 2001
Glass is a solid.
Visual Studio Express takes like 10 minutes to install and making a console project takes like 3 clicks.

Harokey
Jun 12, 2003

Memory is RAM! Oh dear!
Is there a GUI thread?

Anyway, I'm the author of a C++ library that is used for CS1 students to program robots. The library usually does not have any graphical components, however students can take pictures with the robot, and then display them. When the image is displayed, a graphical window pops up to display the image. Similarly, we have a VideoStream element, which does much of the same: Opens a window, and constantly updates it with the current Image from the robot.

I also am working on putting in a graphics drawing library into this code, which would also need to be non-blocking, so the students can write code that draws things onto the window.

Right now I solve this by creating a separate thread which handles all the GUI event stuff (I'm using fltk1.1). You may (or may not) know that windows requires all its windowing calls from the main thread, so I'm a bit at a loss of how to handle this. The library should be cross-platform and support Linux/OSX/Windows.

Here's a few things I've considered:

1.) Find some sort of cross-platform "timer" that I can use to update/handle events
2.) Whenever any sort of library call is made to my library, call update/handle events
3.) Use some tricky #define stuff to rename the main() to something else, and launch it in a "background" thread, taking over the foreground thread for GUI operations

Is there something better/what do you think is the best approach?

(Should this go in its own thread?)

Ninja Rope
Oct 22, 2005

Wee.

Harokey posted:

You may (or may not) know that windows requires all its windowing calls from the main thread, so I'm a bit at a loss of how to handle this.

Are you sure? I thought windowing functions were not thread-safe, but as long as you synchronize access you can call them from any thread?

nielsm
Jun 1, 2009



On Win32, windows are owned by threads and each thread that owns windows should have a message loop. You can send messages to windows belonging to other threads and processes, and most programmatic interactions with windows happen through messages. (Those that don't are generally thread safe.)

GTK, I believe, requires all GUI access to be done through one thread, generally the main one. It should be possible to use Xlib in multiple threads if it's initialised correctly.

I don't know much about how threads interact with GUI in Mac OS X. I think it's safest to assume it also wants GUI stuff to be done in the main thread.


There's two reasonable designs I can see for your problem. One would be a function the students can call from their code, that takes a callback function pointer, or a pointer to an object type with a virtual entry point method. In the provided function you'd set up the GUI and spawn a new thread for the students' graphics code to run in, this thread would then call the student's callback.
The other would be providing an implemented main function and mandating that the students implement a function by a different name, that will then be their main function.
Both of those ways should ensure that the GUI code will be running in the main thread and the students' code in another. I see the first as more flexible, but also requiring more advanced concepts to use. (Function pointers or virtual inheritance.)

Harokey
Jun 12, 2003

Memory is RAM! Oh dear!

Ninja Rope posted:

Are you sure? I thought windowing functions were not thread-safe, but as long as you synchronize access you can call them from any thread?

I'm not sure, but it seemed to be the take home message from this message thread with the FLTK folks: http://www.mail-archive.com/fltk@easysw.com/msg10421.html


Edit: I'm not opposed to switching windowing libraries if theres another one that would make this process easier.

Harokey fucked around with this message at 19:58 on Jan 15, 2011

Bodhi Tea
Oct 2, 2006

seconds are secular, moments are mine, self is illusion, music's divine.
About setting up a compiler+IDE, CodeBlocks+MingW package is completely trivial to setup. You can start compiling individual files right away without even setting up a project.

Ninja Rope
Oct 22, 2005

Wee.

Harokey posted:

I'm not sure, but it seemed to be the take home message from this message thread with the FLTK folks: http://www.mail-archive.com/fltk@easysw.com/msg10421.html


Edit: I'm not opposed to switching windowing libraries if theres another one that would make this process easier.

I can't speak for the limitations of that (or any other) library, but I did find this:

http://msdn.microsoft.com/en-us/library/ms632597%28VS.85%29.aspx#multithread_apps posted:

A Windows-based application can have multiple threads of execution, and each thread can create windows. The thread that creates a window must contain the code for its window procedure.

Which is basically what is said above. Once you create a window, a message loop is created for the creator thread which can then Get or PeekMessage. Any other thread can post a message to the created window. It looks like some windowing functions can be called for another thread's window (SetWindowPos) and some can't (DestroyWindow). You don't need to create all the windows from the same thread, nor do any of the threads need to be the "main" or first created thread.

Adbot
ADBOT LOVES YOU

fankey
Aug 31, 2001

Harokey posted:

Edit: I'm not opposed to switching windowing libraries if theres another one that would make this process easier.
I believe if you use QT signals and slots it will handle this for you. See http://doc.qt.nokia.com/4.7-snapshot/threads-qobject.html.

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