Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411196 Posts in 69314 Topics- by 58380 Members - Latest Member: feakk

March 19, 2024, 02:34:33 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Proficiency in C++
Pages: 1 2 [3] 4
Print
Author Topic: Proficiency in C++  (Read 8513 times)
paste
Level 6
*


BARF!


View Profile WWW
« Reply #40 on: March 10, 2012, 05:47:49 PM »

@Average Software: Would you (or anyone else for that matter) be willing to share a segment of code or a situation where someone less experienced would use dynamic allocation and a redone version of it using value semantics? I've only just heard of it, and I'm one of the people you are probably talking about that overuses "new". As such, I find it much easier to learn from example and then abstract idea, so an example would be of great help.
Logged

InfiniteStateMachine
Level 10
*****



View Profile
« Reply #41 on: March 10, 2012, 05:49:50 PM »

yeah I would also like to see that. Also thank you ntdb for posting that book link. Does that particular book espouse the virtues of avoiding dynamic allocation as well.

I can certainly see the benefits of avoiding dynamic allocation. I guess I have a new goal for my next C++ project.
Logged

Will Vale
Level 4
****



View Profile WWW
« Reply #42 on: March 10, 2012, 06:46:23 PM »

You might not want to avoid it altogether, but it's definitely a good mindset because it makes you face up to the problem that computers have limited resources.

One interesting question which usually comes up is how to deal with having N of something, like bullets, and not really knowing how many N is. This is different to the case where each bullet is on the heap, or in a std::vector, and you just add one when you need it.

The simplest solution is to decide the max number of bullets you will allow, and then figure out what to do if you run out. You lose flexibility, but you gain predictable memory usage, and better memory locality (all your bullets are close together so they'll usually be faster to access). You also explicitly handle running out of bullets at a high level, so your game won't run out of memory or start hitting the swap file when it runs out of resources at the lower level.

(NB: This works when allocating on the heap too.)

@Average Software:
Going for full-on value semantics sounds interesting, do you find your program spends time copying complex values around, or do you do read-only access by reference?

Will
Logged
BlueSweatshirt
Level 10
*****

the void


View Profile WWW
« Reply #43 on: March 10, 2012, 08:33:40 PM »

Or linked lists. Hand Shake Left WTF Hand Shake Right
Logged

Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #44 on: March 10, 2012, 08:54:49 PM »

Bruce Eckel's Thinking in C++ comes highly recommended and is free to download: http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

I actually did not find this book very good at all, I owned a physical copy at one point, along with Eckel's Java book, and I didn't like either of them.  Unfortunately, I can't recall exactly what I didn't like about them.

My favorite C++ books are Stroustrup's and this one.  It covers modern C++ template programming in exhaustive detail, and one of the authors is on the compiler team that produced the only successful implementation of the export keyword, so this dude knows what he's talking about.  I'm not sure if I would recommend it now, since C++2011 has expanded templates by quite a bit, so the book may now be outdated.

Quote from: Will Vale
Going for full-on value semantics sounds interesting, do you find your program spends time copying complex values around, or do you do read-only access by reference?

I have not found any measurable performance issues, since most of the copying occurs either once, or in code that is not particularly performance sensitive.  I've moved from C++ to Ada for most of my large projects, and this approach is much easier to use in that language due to support for polymorphic copying and value-based polymorphism, but there's no reason you couldn't do it in C++.

The key to it all is to manage your data by value, and process it by reference.  I tend to go for centralized data stores rather than having several things competing for 'ownership' of resources.  This is another reason why I tend to shy away from smart pointers, my memory management structure simply doesn't need them, since memory is clearly owned by particular modules.
Logged



What would John Carmack do?
Will Vale
Level 4
****



View Profile WWW
« Reply #45 on: March 10, 2012, 10:15:04 PM »

and process it by reference.
Thanks, that's what I was curious about. I agree that owning data by value is desirable, although it does imply (in C++) a degree of dependency (#include rather than forward-declare) which you could otherwise avoid. But maybe that's a reasonable price to pay for the degree of transparency and simplicity you get.

Will
Logged
BlueSweatshirt
Level 10
*****

the void


View Profile WWW
« Reply #46 on: March 10, 2012, 10:25:05 PM »

What do you guys exactly mean by "owning data by value" as opposed to by reference?
Logged

Sakar
Guest
« Reply #47 on: March 10, 2012, 10:45:37 PM »

What do you guys exactly mean by "owning data by value" as opposed to by reference?
This is probably a bad explanation, but here it goes:

Owning data by value means that something owns the actual value of the data, like the value a pointer points to. By reference means something holds a reference to it, like a pointer points to memory, but doesn't actual have it.

Basically its like the difference between pointers and normal variables

Shrug
Logged
Will Vale
Level 4
****



View Profile WWW
« Reply #48 on: March 10, 2012, 11:33:26 PM »

That's about right. Maybe a bit of code will help too:

Code:
struct Bullet
{
  float x, y;
};

// Bullets by reference - we point to (refer to) bullets in memory somewhere.
std::vector<Bullet*> bullet_references;

// Bullets by value - we have the complete bullet states (values) right here.
std::vector<Bullet> bullet_values;

Logged
BlueSweatshirt
Level 10
*****

the void


View Profile WWW
« Reply #49 on: March 10, 2012, 11:45:30 PM »

Oh. Droop

I was under the impression you guys were talking about something way different.
Logged

Will Vale
Level 4
****



View Profile WWW
« Reply #50 on: March 11, 2012, 12:10:02 AM »

Unfortunately C++ is mired in jargon. For instance, the appallingly-named RAII (Resource Acquisition Is Initialisation) means more-or-less "acquire things you need in constructors, release them again in destructors". I can't think of a more opaque name for it than RAII Sad

Code:
class File
{
public:
   File( const char *name ) : fp(fopen(name, "rb")) {}
   ~File() { fclose(fp); }

private:
   FILE *fp;
};
Logged
paste
Level 6
*


BARF!


View Profile WWW
« Reply #51 on: March 11, 2012, 07:19:31 AM »

Owning data by value means that something owns the actual value of the data, like the value a pointer points to. By reference means something holds a reference to it, like a pointer points to memory, but doesn't actual have it.

Basically its like the difference between pointers and normal variables

What's the benefit of it?  Less memory leaks and easier readability I'm guessing? Why use pointers at all then?
Logged

PompiPompi
Level 10
*****



View Profile WWW
« Reply #52 on: March 11, 2012, 08:41:02 AM »

You "have" to use pointers. If you want an array that change it's size at run time, you "have" to use pointers. If you want a linked list, you "have" to use pointers.
The thing is, you don't have to use '*' in many cases. But you can use STL(Standard Template Library) which implement many of the things requiring pointers, for you.
So when you want a linked list, you just do std::list<T>, instead of some struct Node { Node * Next;}
Some mentioned boost, I actually don't like to use boost if I don't have to. I think smart pointers should be used mostly when seperate your program into modules.
So basically, you can avoid using '*' in many cases, but behind the scenes, when you use std::vector and std::list, there will be a pointer hidden in their implementation somewhere.
Logged

Master of all trades.
oahda
Level 10
*****



View Profile
« Reply #53 on: March 11, 2012, 09:00:46 AM »

sometimes obscure C++ knowledge. (E.g., "Explain what the 'mutable' keyword does.")
Mutable isn't obscure. Volatile is.

@Average Software: I've been reading that Stroustrup book on and off lately. What book would you recommend for C? I'm guessing Kernighan and Ritchie's? What do you mean by "a proud language lawyer"? Why aren't I asking you this stuff on IRC?
Because if you ask it in this thread, the eventual reply will remain here for anyone to see and benefit from, which is a mighty fine thing.
Logged

Klaim
Level 10
*****



View Profile WWW
« Reply #54 on: March 11, 2012, 09:05:27 AM »

Unfortunately C++ is mired in jargon. For instance, the appallingly-named RAII (Resource Acquisition Is Initialisation) means more-or-less "acquire things you need in constructors, release them again in destructors". I can't think of a more opaque name for it than RAII Sad


RAII name comes from Stroustup that himself says that he's really not good at "marketing" and should have asked someone else for another name. The problem is that the term is already widely used in C++-specific circles...

It would be nice to have a better name, but oh well...
Logged

rivon
Level 10
*****



View Profile
« Reply #55 on: March 11, 2012, 01:09:52 PM »

You "have" to use pointers. If you want an array that change it's size at run time, you "have" to use pointers. If you want a linked list, you "have" to use pointers.
The thing is, you don't have to use '*' in many cases. But you can use STL(Standard Template Library) which implement many of the things requiring pointers, for you.
So when you want a linked list, you just do std::list<T>, instead of some struct Node { Node * Next;}
Some mentioned boost, I actually don't like to use boost if I don't have to. I think smart pointers should be used mostly when seperate your program into modules.
So basically, you can avoid using '*' in many cases, but behind the scenes, when you use std::vector and std::list, there will be a pointer hidden in their implementation somewhere.
You also need pointers when you plan to use a lot of data as stack memory is limited and therefore you need to allocate memory for the data on heap.

sometimes obscure C++ knowledge. (E.g., "Explain what the 'mutable' keyword does.")
Mutable isn't obscure. Volatile is.
I don't know either and I never ever needed it Smiley
Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #56 on: March 11, 2012, 02:09:51 PM »

sometimes obscure C++ knowledge. (E.g., "Explain what the 'mutable' keyword does.")
Mutable isn't obscure. Volatile is.
I don't know either and I never ever needed it Smiley

I use mutable quite often, and there are lots of neat tricks you can do with volatile overloading.  I once wrote a queue class that was mutex locked for concurrent access if you declared it to be volatile.

Outside of embedded programming and some concurrency situations though, volatile probably doesn't see a lot of use.

Truly obscure, at least for me, were bitfields.

Code:
struct Bitfield
{
    unsigned bit_1 : 1;
    unsigned bit_2 : 1;
    unsigned two_bits: 2;
};

I'm not going to explain that code, look it up if you're interested.
Logged



What would John Carmack do?
Moczan
Guest
« Reply #57 on: March 12, 2012, 01:58:42 AM »

Wow, I'm not really a C++ expert, but I always thought that bits being fundamental to programming, using bitfields and bitwise operators was common.
Logged
peous
Level 2
**


Indie opportunist


View Profile
« Reply #58 on: March 12, 2012, 04:41:46 AM »

No, usually it's not useful to know bits when using C++
About volatile, the problem is that implementation can be compiler dependent, and optimization-level dependant, so I usually don't use it
Logged

rivon
Level 10
*****



View Profile
« Reply #59 on: March 12, 2012, 05:20:09 AM »

You don't generally work directly with bits unless you're doing encryption or compression or something like that.
Logged
Pages: 1 2 [3] 4
Print
Jump to:  

Theme orange-lt created by panic