Hi Gabriel
Thanks for clarifying, but I'm not really convinced.

On Monday 06 July 2009 14:04:20 Gabriel M. Beddingfield wrote:
> Hi Jakob,
>
> Jakob Lund wrote:
> > Every note that gets played still causes a `new Note` being issued in
> > hydrogen.cpp -- am I right that this is potentially a problem? If so, we
>
> Yes, but it's been low on the priority list because it's not known to
> create a problem.  Also, std::vector<> (and maybe std::dequeue<>) has a
> block of memory pre-allocated (and larger than the vector).  So, it's
> usually only a problem if you exceed that storage range.

The Note objects are created with 'new', and the POINTERS to them get stored 
on a std::priority_queue. Even if the queue behaves nicely, the object 
allocation may (and, as it seems happens most often, may not) cause 
problems -- but can we be _sure_ it will not, as long as we use 'new' ?

>
> > Also, there's a very old patch of mine (attached) that I think ought to
> > be considered for this branch (there is a priority queue of Note*'s using
> > comparison of Note&'s, and I _suspect_ this causes the Note( Note* )
> > constructor to take effect "behind the scenes" -- this is C++, right? )
>
> No, passing a Note& does not call any constructors.  Passing a reference is
> nearly identical to passing a pointer, except that:
>
>     * You must be passing a valid instance of Note
>
>     * You access the values using '.' instead of
>       pointer dereferencing '->'.
>
>     * You can not construct, delete, or recreate
>       the Note inside the function.
>
> However, in reality, it's actually the pointer that is being passed on the
> stack to the function.  The following two are nearly identical at the
> machine level:
>
> class Dog;
>
> void walk_dog_p( const Dog* d) {
>      d->walk();
> }
>
> void walk_dog_r( const Dog& d) {
>      d.walk();
> }
>
> Dog& requires that there actually be an instance of Dog.  Passing a Dog*
> will allow you to pass a null or invalid pointer.  However, using Dog& will
> _not_ call the Dog::Dog() constructor.
>
> You can also use references instead of pointers when doing forward
> declarations. You probably know that you can do this in a header:
>
> class Dog;
>
> class Kennel {
> public:
>      void add_dog(const Dog* d);
>      Dog* find_dog(const char* name);
> };
>
> This will compile even though you haven't declared the 'Dog' class.  In
> fact, Dog can be an opaque type that isn't even known until run-time.  The
> following does the same thing with references, and will also compile
> without knowing what 'Dog' is:
>
> class Dog;
>
> class Kennel {
> public:
>      void add_dog(const Dog& d);
>      Dog& find_dog(const char* name);
> }
>
> As said before, the difference is that 'd' and the return of find_dog()
> must be a real instance of the object.  (I.e. no null pointers.)
>
> I will often use references instead of pointers because (a) I like to use
> '.' notation rather than '->' pointer dereferencing and (b) you don't have
> to constantly test to see if someone passed you a null or invalid pointer.

All very well, but that doesn't account for the situation in hydrogen.cpp; 
please read, compile and run the attached dog example to see what I mean!

Cheers'n'all
 -- Jakob.
#include <stdio.h>
#include <deque>
#include <queue>


class Dog {
public:
	Dog() {
		printf( "constructor w. no params\n" );
	}
	Dog( Dog* d ) {
		printf( "construction by pointer\n" );
	}
};

bool operator> (const Dog& d, const Dog& e) {
	return true; // dog operator.
}                                                               /// dog FIFO
std::priority_queue<Dog*, std::deque<Dog*>, std::greater<Dog> > dog_queue;

struct compare_dogs {
	bool operator()( Dog* d, Dog* e ) { return true; }
};
std::priority_queue<Dog*, std::deque<Dog*>, compare_dogs > dog_pointer_queue;

int main( char** args, int argc ) {
	Dog * d;
	Dog * e;
	printf( "Create two dogs..\n" );
	d = new Dog();
	e = new Dog();


	printf( "Now what happens when the dogs enter the first queue?\n" );
	dog_queue.push( d );
	dog_queue.push( e );

	printf( "Now let's put those dogs in the other queue!\n" );
	dog_pointer_queue.push( d );
	dog_pointer_queue.push( e );
}
------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time, 
vendors submitting new applications to BlackBerry App World(TM) will have 
the opportunity to enter the BlackBerry Developer Challenge. See full prize 
details at: http://p.sf.net/sfu/blackberry
_______________________________________________
Hydrogen-devel mailing list
Hydrogen-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hydrogen-devel

Reply via email to