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
Avenging Dentist
Oct 1, 2005

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

Zerf posted:

Yay for working with undefined behavior while making real-time systems!

But games aren't real-time systems and I don't think I'd want to use C++ for real-time computing anyway???

Adbot
ADBOT LOVES YOU

Zerf
Dec 17, 2004

I miss you, sandman

Avenging Dentist posted:

But games aren't real-time systems and I don't think I'd want to use C++ for real-time computing anyway???

Entirely depends on how you define real-time systems. Try googling it, you'd be surprised.

If you have a hard target of a framerate of 30 fps, it means you have a timeslice of 33 ms to work with each frame. In my book, that's a real-time constraint.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Ok, let's say you're right that games have a "real-time constraint" (they don't; it's a guideline, no one is going to recall a game because the framerate stutters once an hour for some users). Then game programmers are all complete idiots for using a language ill-suited to real-time computing in the first place.

More to the point: the STL isn't really well-suited to game development anyway (at least not "AAA" games), so the problems with vectors run considerably deeper than "clear() has undefined behavior". Granted, this is somewhat better in C++0x with slightly less stupid allocator support.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge
More on why STL can suck for games here http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html

Summary: EA ended up writing their own STL-like collection of data-structures and algorithms for in-house use.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
I was really pleasantly surprised when I first saw that. It's nice to see game developers taking advantage of the opportunity to present their issues rather than writing their in-house replacement and bitching about the STL but never trying to fix it. (That said, I basically hate the STL.)

MrMoo
Sep 14, 2000

Zakalwe posted:

More on why STL can suck for games here http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html

Summary: EA ended up writing their own STL-like collection of data-structures and algorithms for in-house use.

An Trolltech followed suited for Qt for pretty much the same reasons.

floWenoL
Oct 23, 2002

MrMoo posted:

An Trolltech followed suited for Qt for pretty much the same reasons.

No they didn't? IIRC, Trolltech rolled their own because they for some reason just liked Java iterators more than C++ ones.

MrMoo
Sep 14, 2000

floWenoL posted:

No they didn't? IIRC, Trolltech rolled their own because they for some reason just liked Java iterators more than C++ ones.

They have a list of reasons, but memory was the main one I saw,

Trolltech posted:

Whereas STL's containers are optimized for raw speed, Qt's container classes have been carefully designed to provide convenience, minimal memory usage, and minimal code expansion. For example, Qt's containers are implicitly shared, meaning that they can be passed around as values without forcing a deep copy to occur. Also, sizeof(QXxx) == sizeof(void *) for any Qt container, and no memory allocation takes place for empty containers.

http://doc.trolltech.com/4.0/qt4-tulip.html

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!
Symbian also had some kind of collection of data structure classes that was completely different from STL. It was disgusting, just like everything else about Symbian.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


Backing up a bit,

UraniumAnchor posted:

Const reference if your function isn't going to modify, pointer if it will.

I just think it's more obvious at the call level, the & in front of your argument signifies 'hey this might change'.

And then usually the first thing I do in the function is put the pointer into a reference so I don't have to keep typing -> :v:

If somebody wants to point out why this is dumb go right ahead.

I discussed this with another mate of mine and he said his rule is "use pointers if there's any chance the parameter could be NULL (either coming in or going out) and use references otherwise, and use const as appropriate." Sounds reasonable enough to me.


On another question that I think the answer to is going to make me feel completely retarded: is it C++ standard behavior to do nothing on a delete of a NULL/0 pointer, rather than coredump/throw an exception, or is that compiler dependent? And if it's the former, why the hell have I been doing things like if (ptr) delete ptr; all these years? :saddowns:

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Ledneh posted:

is it C++ standard behavior to do nothing on a delete of a NULL/0 pointer
Yes.

Ledneh posted:

why the hell have I been doing things like if (ptr) delete ptr; all these years? :saddowns:
Because of lovely books and tutorials.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


Son of a bitch :cry:

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

Plorkyeran posted:

Ledneh posted:

is it C++ standard behavior to do nothing on a delete of a NULL/0 pointer
Yes.

Ledneh posted:

why the hell have I been doing things like if (ptr) delete ptr; all these years? :saddowns:
Because of lovely books and tutorials.
To be a little fair, I wouldn't be surprised if the first editions of the lovely books and tutorials (or the source materials that the writers stolelearned from) were written before there was a true standard or when it was very common to find non-compliant compilers that would blow up if you tried to do that. And since it doesn't really hurt anything, why bother updating it?

Dooey
Jun 30, 2009
So I've moved on from using Dev-C++ in class for simple one-file projects, and I'm attempting a larger project for my first time, using Visual C++ 2008. (Express Edition, in case it matters)

I've run into a bunch of problems, but so far I've managed to find solutions on Google. Until now.

I'm trying to use the library OpenCV for accessing my webcam, but I get errors like:

error C3862: 'cvRound': cannot compile an unmanaged function with /clr:pure or /clr:safe

and

error C3645: 'cvRound' : __clrcall cannot be used on functions compiled to native code

which Google tells me is caused by trying to compile it as managed code, when it can't be compiled to manged code because it uses inline assembly. At least, I think that is the problem.

Unfortunately, I can't find any way to change it to unmanaged code. The project was started as a Windows Forms Application, which apparently makes it automatically managed, and I've heard that making it a Win32 project instead will fix the problem, but that would also mean redoing all my forms (I think? I'm not to sure about the redoing forms part.)

Can anyone help me please? I can post code if it would help, but I'm note sure what code I should post.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
C++ doesn't support WinForms. You might be able to keep your forms, but I doubt it.

Dooey
Jun 30, 2009
drat.

Well, I guess I can manage without the forms, but its still going to suck redoing everything. I guess I'll know better next time.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
In theory, it's possible to do what you want, but it's not easy and I wouldn't recommend it anyway, since C++/CLI is kinda crap to be honest.

washow
Dec 1, 2007

Here you go, op :toot:
Okay so I have to read from a file, malloc a new structure and write the data into that new structure.

That's the description but I'm having a bit of trouble figuring out how to actually do that. Little hits please?

mr_jim
Oct 30, 2006

OUT OF THE DARK

washow posted:

Okay so I have to read from a file, malloc a new structure and write the data into that new structure.

That's the description but I'm having a bit of trouble figuring out how to actually do that. Little hits please?

Describe the struct and the file format. Does the struct have an array holding the entire file contents? Or are you reading sections of the file and storing them in individual fields in the struct? Is it a binary file with the needed data at fixed offsets, or is it text that needs to be parsed?

mr_jim fucked around with this message at 04:16 on May 17, 2010

Contero
Mar 28, 2004

washow posted:

Okay so I have to read from a file, malloc a new structure and write the data into that new structure.

That's the description but I'm having a bit of trouble figuring out how to actually do that. Little hits please?

Do you know how to do any of those individual steps? If you do it should be extremely easy. If not, write a basic program to learn how to do that step like making a small program that reads something from a file and prints it out immediately.

washow
Dec 1, 2007

Here you go, op :toot:

mr_jim posted:

Describe the struct and the file format. Does the struct have an array holding the entire file contents? Or are you reading sections of the file and storing them in individual fields in the struct? Is it a binary file with the needed data at fixed offsets, or is it text that needs to be parsed?

Just a txt file. I'm prompting 3 different things, simple int int char and storing them in an array of structure. After sorting and other small stuff, I transfer all that stuff to a txt file. Then I dynamically allocate an array of structures based on the effective size and populate that said array with the records from the txt file.

Last part is where I'm stuck at. Oh and something happens during the sorting part and whatever happens to be on top becomes gibberish. The rest is fine however

I'm looking at fread and fwrite and stuff on cplusplus.com but still going :psyduck: at this moment

DeciusMagnus
Mar 16, 2004

Seven times five
They were livin' creatures
Watch 'em come to life
Right before your eyes
If it's just a simple text file you can use fscanf. Example:

code:
1 2 c
3 4 d
code:
FILE * fp = fopen(...);
struct record
{
  int first;
  int second;
  char third;
} line;
if (fscanf(fp, "%i %i %c", &line.first, &line.second, &line.third) != 3))
{
  /* error condition. failed to read in three fields */
}

UraniumAnchor
May 21, 2006

Not a walrus.
If it's text based you don't want fread/fwrite anyway, that's for binary data.

You probably want fscanf for now. If it's int/int/char then you can probably use:

code:
fscanf(file, "%d %d %c", &i1, &i2, &c);
And then do whatever else with it.

Note: This is not the best way to deal with it, but it'll probably work well enough for now.

efb you bastard

Colonel Taint
Mar 14, 2004


I'm just refreshing my C++ knowledge after a few months in Java/scripting languages, and I'm running into a bit of trouble. I think it has something to do with a destructor, but I'm not sure what exactly. My program runs fine in debug mode until I try to delete one of my tableEntry objects, at which my program receives a SIGTRAP signal.

code:
class tableEntry{
	char* word;
	int value;
	tableEntry* next;
public:
	tableEntry(): word(0), value(0), next(0) {}

	//Returns the length of this bucket after insertion,
	//or 0 if duplicate
	int insert(const char* word, const int value)
	{
		tableEntry* insertPoint = this;
		int returnVal = 1;
		while(insertPoint->word != 0)
		{
			returnVal++;
			//String already in table
			if(!strcmp(this->word, word))
			{
				this->value = value;
				return 0;
			}

			//make a new blank tableEntry as the last one
			if(0 == insertPoint->next)
			{
				insertPoint->next = new tableEntry();
			}

			insertPoint = insertPoint->next;
		}
		insertPoint->word = new char[strlen(word)];
		strcpy(insertPoint->word, word);
		insertPoint->value = value;
		return returnVal;
	}

	int valueOf(const char* string)
	{
	//removed for brevity. does not modify anything.
	}

	~tableEntry()
	{
		delete word;
		delete next;	
		word = 0;
		value = 0;
		next = 0;
	}
};
Am I doing anything immediately stupid? tableEntry is basically meant to be a chained bucket in a hash table.

Full Code is available at
http://codepad.org/NVslQUpP

I'm using MinGW tools.
I've been trying to deal with this for a good few hours to no avail.

Colonel Taint fucked around with this message at 06:15 on May 17, 2010

washow
Dec 1, 2007

Here you go, op :toot:
Ahh fscanf. I was looking at a wrong thing then.

Thanks goons!

OddObserver
Apr 3, 2009

SintaxError posted:

I'm just refreshing my C++ knowledge after a few months in Java/scripting languages, and I'm running into a bit of trouble. I think it has something to do with a destructor, but I'm not sure what exactly. My program runs fine in debug mode until I try to delete one of my tableEntry objects, at which my program receives a SIGTRAP signal.
Problems I can see from quick read:

0) Unless you're doing some class homework with restrictions on legit language functionality, you should not be working with strings this way in a C++ program. Use a string class, like std::string (or std::wstring, or whatever)

1) When you're copying strings, you're not including space for zero termination in the allocation.

2) Whatever you allocate with new[] you should delete with delete[], not delete.

User Friendly
Jul 19, 2003

So I want to teach myself C - could anyone suggest what the best book to get would be?
Basically I know a little bit about programming from the Pascal days at school and from messing around in Python so don't really need to go through the Hello World/If-Then Loops style tutorials. On the other hand I need something which will explain topics such as pointers as if I were an absolute beginner since I've never been exposed to them.

Brecht
Nov 7, 2009

User Friendly posted:

So I want to teach myself C - could anyone suggest what the best book to get would be?
Basically I know a little bit about programming from the Pascal days at school and from messing around in Python so don't really need to go through the Hello World/If-Then Loops style tutorials. On the other hand I need something which will explain topics such as pointers as if I were an absolute beginner since I've never been exposed to them.

Colonel Taint
Mar 14, 2004


OddObserver posted:

Problems I can see from quick read:

0) Unless you're doing some class homework with restrictions on legit language functionality, you should not be working with strings this way in a C++ program. Use a string class, like std::string (or std::wstring, or whatever)

1) When you're copying strings, you're not including space for zero termination in the allocation.

2) Whatever you allocate with new[] you should delete with delete[], not delete.

Ah, thank you. That fixed it.

The only reason I'm working with strings like that is because I'm using strtok to get the strings from a buffer. This program is meant to be as fast as possible, so I want to be avoid making copies of strings that I don't really need copies of. As such, I've actually modified it a bit to use a const char* to point directly into the buffer to the word.

code:
class tableEntry{
  const char* word;
...
tableEntry::insert(const char* word, int value)
...
      //insertPoint->word = new char[strlen(word)+1];
      insertPoint->word = word;
...
tableEntry::~tableEntry()
//no longer delete word
The app is small enough that it's manageable, and I know for a fact that the buffer will be in memory for the duration of the program and will not be used for any other purposes. So this works well enough for my needs.

Brecht
Nov 7, 2009

SintaxError posted:

This program is meant to be as fast as possible, so I want to be avoid making copies of strings that I don't really need copies of. As such, I've actually modified it a bit to use a const char* to point directly into the buffer to the word.
This is called premature optimization and you need to stop doing it as soon as humanly possible. I can count the number of times I've found std::strings to be too slow on zero hands.

Colonel Taint
Mar 14, 2004


Uh really?

I was just doing this as an exercise as part of this programming challenge http://cplus.about.com/od/programmingchallenges/a/challenge9.htm

Given, it's been over for years, but I see no reason not to write it in the most optimized way I can think of as an exercise.

Really I normally would write code in the most readable/maintainable way, but the point of the contest was to make the program as fast as possible, and I see no reason why I couldn't/shouldn't do it with no copies given the circumstances.

Furthermore

Me posted:

The app is small enough that it's manageable, and I know for a fact that the buffer will be in memory for the duration of the program and will not be used for any other purposes. So this works well enough for my needs.

I really see no reason not to do it like this in such a small app. Making a copy of each of 50000 strings just seems like a huge waste of time and memory.

Colonel Taint fucked around with this message at 18:36 on May 17, 2010

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
1) That's a retarded exercise, and a retarded website. 2) You optimize after you have a correct version of your code. I can make a project arbitrarily fast if I'm not constrained by correctness.

Colonel Taint
Mar 14, 2004


All 'retardedness' aside, I guess I just don't see it as that much of a major optimization or really a major issue to do it that way. The code was working fine with strcpy after the fixes that OddObserver pointed out, so I don't see where you get off on telling me to optimize after I have a correct version.

csammis
Aug 26, 2003

Mental Institution

SintaxError posted:

All 'retardedness' aside, I guess I just don't see it as that much of a major optimization or really a major issue to do it that way. The code was working fine with strcpy after the fixes that OddObserver pointed out, so I don't see where you get off on telling me to optimize after I have a correct version.

Because that is the correct way to do good optimization, but hey, we're not worried about correctness :v:

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Think about it this way: if you knew how to optimize properly, you wouldn't have had to waste other people's time telling you how to write your code because you'd already have a working version. More to the point: if you have to ask someone else how to properly handle manual memory management, you shouldn't be manually managing memory. (Try saying that five times fast.)

Colonel Taint
Mar 14, 2004


OK, I understand that. However, the code was correct before I 'optimized' it. Again, I don't really even see storing words as pointers into a tokenized buffer as some sort of major complicated optimization - it's not like I'm manually unrolling loops. I only see it as an efficient use of time/memory given the requirements of the program.

At any rate, the program runs well now.

Edit: furthermore, had I done it with pointers into the buffer from the start, I would have never even run into the original problems which were caused by problems creating/deleting the tableEntry's word.

Colonel Taint fucked around with this message at 19:23 on May 17, 2010

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Just to throw my two cents in: my rule is that the word "optimization" doesn't enter my vocabulary until I've thrown my program at a profiler.

OddObserver
Apr 3, 2009
The main problem with doing things a certain way because 'it's a small app' when learning is that anything real you make won't be small, so it'll be harder to establish whether the shortcuts you take are actually safe.

And in most real things, you likely won't know what parts are performance-critical until you profile, anyway, or whether the performance even matters (if you're taking 2ms vs. 1.8ms, it's probably not worth the effort).

C-style strings are actually a problematic approach when you're trying to be fast, too, since they do not express ownership, so in general you may end up making far more deep copies than, say, with a reference-counted COW string class, or more allocations than with a small-value optimization string class.

I think an important thing to keep in mind with C++ is that abstractions can be /good/ for performance work, since they put a lot mechanics of how stuff works in one place, letting you improve it w/o having to rework everything --- and C++ gives you a lot of tools like inlining and templates to eliminate the abstraction overhead itself.

Colonel Taint
Mar 14, 2004


Let me just say I agree with all of that.

Just to defend myself, I have to point out that I'm not just learning C++, and I was really just doing this as an exercise to start re-familiarizing myself with the C/C++ environment after spending the last year or so deep in higher level language code. I was well aware of the program requirements beforehand, and I think I made the right choices for exactly what needed to be done.

Adbot
ADBOT LOVES YOU

Brecht
Nov 7, 2009

SintaxError posted:

I think I made the right choices for exactly what needed to be done.
Did you profile the code using std::strings vs. your "optimized" C strings, and document the statistically significant difference? It's kind of a rhetorical question because I'm sure you didn't. Would you be willing to paste your complete code somewhere? If so, I'm willing to rewrite it with std::strings and prove it to be as-fast or faster as your version, if-and-only-if you'd be willing to repent from your sinful lifestyle of fragile, error-prone preoptimization. Will you agree to that? Praise Meyers, Hallelujah!

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