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
UraniumAnchor
May 21, 2006

Not a walrus.

OddObserver posted:

A virtual superclass isn't the same thing as a superclass with virtual member functions.

Edit, to be at least somewhat helpful: When doing inheritance, besides just saying
Foo: public Bar, you can also say Foo: public virtual Bar. You need the 'virtual'
to put things to be a diamond, and not to have 2 copies instead. I'll leave the details to you
or more helpful posters.

Or just read this.

(With the assumption that this is considered a good source around here.)

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

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

UraniumAnchor posted:

Or just read this.

(With the assumption that this is considered a good source around here.)

Well, it's in the OP, so...

UraniumAnchor
May 21, 2006

Not a walrus.

Avenging Dentist posted:

Well, it's in the OP, so...

Which I haven't had cause to read in months, but I suppose I should have checked.

ehnus
Apr 16, 2003

Now you're thinking with portals!
Clang can now compile LLVM!

It's not self-hosting yet but it sounds like it's pretty close to happening.

functional
Feb 12, 2008

rjmccall posted:

No. I mean, you're right, a non-leaking malloc implementation is forced to have *some* way of re-deriving the approximate size of an allocation, but there's no standard way to access it. It's definitely not necessarily stored at a constant offset relative to returned pointer; there are implementations that (e.g.) track arenas where all the allocations are the same size.

Note that the size stored need only be approximate: malloc needs to return a pointer of a certain alignment, so typically it will only allocate chunks in multiples of the required alignment, and it may only store the size at that level of granularity.

If you provide a global object named malloc in an object file or library which appears earlier on the link line than libc (easy, since libc is typically last), then the resulting program will use your malloc instead of the system malloc. Note that this gets more complicated on Darwin.

If you want other libraries to still use the system malloc, typically you #define malloc my_malloc in all your implementation files, and then you have my_malloc allocate out of pools which it acquires either through the system malloc or through mmap.

The above summarizes everything interesting that was said when we went over this topic.

One safe way to derive the size of the block of allocated memory from a malloc-allocated pointer is to use the malloc_usable_size(void* p) function. Most popular malloc libraries support it, and if yours doesn't it's easy to switch to one that does.

MagneticWombats
Aug 19, 2004
JUMP!
What do people think about http://www.stepanovpapers.com/notes.pdf ? It's less mathy than most of his other stuff and it's kind of fun (and on a level, sad) to see the guy who did STL rail against the standards committee. On the other hand, for all his regrets, I wonder why he never made his own language or something.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Skimming through it briefly, it looks like it has a lot of similarity to Elements of Programming. Which is to say, programmers (especially programmers who hate C++) should be required to read it.

raminasi
Jan 25, 2005

a last drink with no ice

Avenging Dentist posted:

Skimming through it briefly, it looks like it has a lot of similarity to Elements of Programming. Which is to say, programmers (especially programmers who hate C++) should be required to read it.

Speaking as a programmer who hates C++ and is currently reading Elements of Programming, do you think we should read it because it will make us hate C++ less or because it will make us use C++ better in spite of hating it?

Avenging Dentist
Oct 1, 2005

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

GrumpyDoctor posted:

Speaking as a programmer who hates C++ and is currently reading Elements of Programming, do you think we should read it because it will make us hate C++ less or because it will make us use C++ better in spite of hating it?

Well, hopefully it will make you understand why a lot of "Serious Programmers" use C++ and why generic programming is so important that it's worth dealing with the cruft in C++. (This is coming from someone who's only actually read a chapter and a half of EoP, though I am traveling soon so that amount will increase.)

That Turkey Story
Mar 30, 2003

Avenging Dentist posted:

Well, hopefully it will make you understand why a lot of "Serious Programmers" use C++ and why generic programming is so important that it's worth dealing with the cruft in C++. (This is coming from someone who's only actually read a chapter and a half of EoP, though I am traveling soon so that amount will increase.)

There's this kind of proud ignorance that a lot of programmers have with respect to advanced C++ and they think that all of the template nuances and metaprogramming should never be used simply because it looks complicated. Elements of Programming shows you how important the ideas of generic programming are and that C++ is one of the few languages out there that really lets you express these essential components that make up generic code. Everyone knows C++ is a mess, but it's also much more capable than other "high level" main-stream languages at expressing truly high level concepts. "Serious Programmers" use C++ because it is necessary for creating powerful and efficient generic code.

I like how Alexandrescu put it in his talk. It's almost impossible to have conversations about the STL, GIL, or the Boost Graph Library, with Java programmers who have never worked with a language capable of generic programming. That's not meant as a put-down to Java programmers or to any other non-C++ programmers, they just don't get it because the languages they've worked with are incapable of expressing all that is necessary to make libraries such as those as powerful as they are. Talk about something as basic as a generic sort algorithm not directly attached to a specific container/collection and you often hit a road block. These are important ideas that transcend languages and yet many programmers have never been exposed to them because they are not taught and because the languages they use are unable to fully represent them.

Subway Ninja
Aug 24, 2005

Quick question regarding classes.

code:
class Vehicle{
//stuff
};

Vehicle Minivan; // create a Vehicle object called 'Minivan'.
I understand this part, as well as constructors, etc. What I can't figure out is how to allow the user to name the class object. For example:

code:
cin >> str; //User input to string str
Vehicle str; //Create a Vehicle object named whatever the value of 'str' is.
Instead, it seems it would create a Vehicle object named, literally, str.
How can I allow a user to define the name of the class object? To clarify, this is simply a question of curiosity. I don't really have a need for this to work, I just want to know if it is possible, and how?

MagneticWombats
Aug 19, 2004
JUMP!
You have the name of the object be a member of Vehicle and in your constructor or whatever you assign the string to that name member??

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

Subway Ninja posted:

Quick question regarding classes.

code:
class Vehicle{
//stuff
};

Vehicle Minivan; // create a Vehicle object called 'Minivan'.
I understand this part, as well as constructors, etc. What I can't figure out is how to allow the user to name the class object. For example:

code:
cin >> str; //User input to string str
Vehicle str; //Create a Vehicle object named whatever the value of 'str' is.
Instead, it seems it would create a Vehicle object named, literally, str.
How can I allow a user to define the name of the class object? To clarify, this is simply a question of curiosity. I don't really have a need for this to work, I just want to know if it is possible, and how?



You don't want to do what you're trying to do for a whole host of reasons, even in languages where it's possible without considerable fuckery

A better way to solve the same problem is to create an associative container (std::map or std::unordered_map) with the value of the user input as the index for that particular Vehicle

Subway Ninja
Aug 24, 2005

I'm fairly new to this so perhaps I'm not grasping this properly, or maybe I'm not communicating it well enough. For example (user input in bold):

Code
string str;
cout << "Name your vehicle: "
cin >> str;

DOS Window
Name your vehicle: Camero

Code
str.Wheels = 4; //str holds the value of 'Camero'
cout << "Which vehicle do you want to inspect? "
cin >> str;

DOS Window
Which vehicle do you want to inspect? Camero

Code
cout << "A " << str << "has " << str.wheels << "wheels.";

DOS Window
A Camero has 4 wheels.

EDIT:

Otto Skorzeny posted:

You don't want to do what you're trying to do for a whole host of reasons, even in languages where it's possible without considerable fuckery

A better way to solve the same problem is to create an associative container (std::map or std::unordered_map) with the value of the user input as the index for that particular Vehicle

Got this in just before my reply. Thanks for the input, I'll have to look up containers/maps and play around with those. Working my way through a tutorial and this question popped into my head. I tried to solve it on my own but couldn't think of a way it would work, nor did it seem impossible, so I figured I was missing something.

Subway Ninja fucked around with this message at 23:39 on Dec 26, 2009

RussianManiac
Dec 27, 2005

by Ozmaugh
What is a good smart pointer library or source code?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Given that you didn't bother to qualify what kind of semantics you want, I'm going to have to say "the C++ standard library" (and/or TR1).

RussianManiac
Dec 27, 2005

by Ozmaugh
I just want something that will keep track of how many pointers i have to something and delete if none are left.

Paniolo
Oct 9, 2007

Heads will roll.
code:
std::tr1::shared_ptr

Plorkyeran
Mar 22, 2007

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

MagneticWombats posted:

What do people think about http://www.stepanovpapers.com/notes.pdf ? It's less mathy than most of his other stuff and it's kind of fun (and on a level, sad) to see the guy who did STL rail against the standards committee. On the other hand, for all his regrets, I wonder why he never made his own language or something.
Are there answers for the quiz questions anywhere? Most of them are pretty obvious but there's a few I'm not confident about.

NickPancakes
Oct 27, 2004

Damnit, somebody get me a tissue.

Ignore me.

Standish
May 21, 2001

Subway Ninja posted:

Code
string str;
cout << "Name your vehicle: "
cin >> str;

DOS Window
Name your vehicle: Camero

Code
str.Wheels = 4; //str holds the value of 'Camero'
"str" is an object of class std::string. std::string has no "Wheels" member. You need a "Vehicle" class:
code:
// this is a class definition:
class Vehicle {
  public:
    int wheels;
    string name;
};
map<string, Vehicle> vehicles; // the Standard Template Library "map" class

// this is an *object* of the Vehicle class:
Vehicle myCar;
cout << "Name your vehicle: "
cin >> myCar.name;

myCar.wheels = 4;
vehicles[myCar.name] = myCar; //store the Vehicle object in the map

cout << "Which vehicle do you want to inspect? "
string tmp;
cin >> tmp;

map<string, Vehicle>::iterator iter = vehicles.find(tmp); // retrieve your Vehicle object from the map
// TODO: handle the case where the vehicle was not found
Vehicle v=iter->second;

cout << "A " << v.name<< " has " << v.wheels << " wheels.";

tractor fanatic
Sep 9, 2005

Pillbug
If I have a union

union thing{
float a;
int b;
};

and I have

thing example;
example.b = 5;
example.a = example.b;

Is this guaranteed to be safe? How does assignment work when its from one union member to another for the same union instance?

Vinterstum
Jul 30, 2003

tractor fanatic posted:

Is this guaranteed to be safe? How does assignment work when its from one union member to another for the same union instance?

For PODs like these, example.b will be read out from memory into a register, and then written to the memory location of example.a. So yeah, it's safe.

tractor fanatic
Sep 9, 2005

Pillbug

Vinterstum posted:

For PODs like these, example.b will be read out from memory into a register, and then written to the memory location of example.a. So yeah, it's safe.

Thank you.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
To be totally formal, you are not allowed to do this with unions. In practice, this restriction is never enforced because there is no other legitimate way to do it.

That Turkey Story
Mar 30, 2003

rjmccall posted:

To be totally formal, you are not allowed to do this with unions. In practice, this restriction is never enforced because there is no other legitimate way to do it.

example.b = 5;
float const temp = example.b;
example.a = temp;

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

That Turkey Story posted:

example.b = 5;
float const temp = example.b;
example.a = temp;

Hah, yes, I did not read carefully enough. This is totally fine.

Vinterstum
Jul 30, 2003

rjmccall posted:

To be totally formal, you are not allowed to do this with unions.

Source? Can't remember reading that anywhere, and a click search through the standard doesn't mention anything about that either.

I can't think of any reason why. Maybe for non-PODs, but even then things with non-trivial copy constructors and assignment operators aren't allowed in unions, so it should be safe then as well (or actually not relevant, since the types would need to be identical).

Vinterstum fucked around with this message at 10:58 on Dec 28, 2009

functional
Feb 12, 2008

For the sake of argument say I'm using stdlib.h. I would rather avoid not including it and having to reimplement its functionality.

If I want to redefine a function that's already defined in stdlib, what are my options? Say I want create my own function named div, even though div is already declared in stdlib. Using the #undef macro doesn't work. The errors generated are "conflicting types" and "previous declaration."

The obvious workaround, "create a function with a different name and use that", is not acceptable in this case.

If I do manage to override the naming convention, what are the effects on the upstream and downstream libraries? Presumably projects which rely on my project as a library will see my new implementation of div. What about other projects that I rely on in mine?

ChiralCondensate
Nov 13, 2007

what is that man doing to his colour palette?
Grimey Drawer

functional posted:

For the sake of argument say I'm using stdlib.h. I would rather avoid not including it and having to reimplement its functionality.

If I want to redefine a function that's already defined in stdlib, what are my options? Say I want create my own function named div, even though div is already declared in stdlib. Using the #undef macro doesn't work. The errors generated are "conflicting types" and "previous declaration."

The obvious workaround, "create a function with a different name and use that", is not acceptable in this case.

If I do manage to override the naming convention, what are the effects on the upstream and downstream libraries? Presumably projects which rely on my project as a library will see my new implementation of div. What about other projects that I rely on in mine?

code:
#define div STDLIB_DIV_HURR
#include "stdlib.h"
#undef div

#include "stdio.h"

void div() {
  printf("hurrrr\n");
}

int main() {
  div();
}
pre:
[box] ~ > gcc hurr.c && ./a.out
hurrrr
[box] ~ >

haveblue
Aug 15, 2005



Toilet Rascal
That's one of those things that you pretty much need to explain why you're trying to do it so you don't get dismissed out of hand as "that's something that just shouldn't be done".

If you're trying to avoid a million search-and-replace operations, you could implement your alternative div with a different name and redirect references to it with a #define (and #undef at the end).

functional
Feb 12, 2008

ChiralCondensate posted:

code:
#define div STDLIB_DIV_HURR
#include "stdlib.h"
#undef div
void div() {
  printf("hurrrr\n");
}

This is a good technique, and it works for div. It doesn't seem to work for all functions. I think it's failing for library functions which are defined using macro abstractions.

Are there any other techniques like this? Whether at the code or compiler level?

OddObserver
Apr 3, 2009
It's probably a better idea to rename your own 'div' function and its uses rather than messing with the system ones. On ELF systems (e.g. Linux, FreeBSD, etc.), your implementation may end up overriding global 'div' symbol even when it's called from within the standard libraries internally, causing stuff to blow up in weird ways, unless you're ultra careful.

ChiralCondensate
Nov 13, 2007

what is that man doing to his colour palette?
Grimey Drawer

functional posted:

This is a good technique, and it works for div. It doesn't seem to work for all functions. I think it's failing for library functions which are defined using macro abstractions.

Are there any other techniques like this? Whether at the code or compiler level?
I didn't say it was good :) Listen to what OddObserver and haveblue said.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Vinterstum posted:

Source? Can't remember reading that anywhere, and a click search through the standard doesn't mention anything about that either.

I misread your post as the similar, pervasive, but technically illegal example.b = 5; foo = example.a;. Your actual example is fine.

There is a special caveat with unions in C++, which is that union members are allowed to have non-copy assignment operators, which can get you in trouble because they're typically not robust against self-assignment. This is a pretty marginal problem prior to C++0x, though, because you'd have to have a class with a non-copy assignment operator but *not* a destructor, constructor, or copy assignment operator.

slovach
Oct 6, 2005
Lennie Fuckin' Briscoe
Is there a better way to deal with passing member functions than wrapping them in another static function? Kind of an ugly solution...

litghost
May 26, 2004
Builder

slovach posted:

Is there a better way to deal with passing member functions than wrapping them in another static function? Kind of an ugly solution...

boost::bind and boost::function or their C++0x versions if you have a compiler that supports them.

slovach
Oct 6, 2005
Lennie Fuckin' Briscoe

litghost posted:

boost::bind and boost::function or their C++0x versions if you have a compiler that supports them.

Does the latest visual studio have support?

:sigh: C++0x seems like it has so many nice things

Avenging Dentist
Oct 1, 2005

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

slovach posted:

Does the latest visual studio have support?

:sigh: C++0x seems like it has so many nice things

Luckily for you, std::bind and std::function are also available in TR1 (in the std::tr1 namespace, of course), which you can download for VS2008 from Microsoft (or use Boost's implementation thereof).

Adbot
ADBOT LOVES YOU

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.
Member functions? Sounds like a job for std::tr1::mem_fn or std::mem_fun.

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