Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 09 Dec 2005
Offline Last Active Today, 03:52 PM

#5256449 Android C++ new[] delete[] problems -> invalid HEAP etc

Posted by Bregma on 09 October 2015 - 05:50 PM

BTW i wasnt aware that delete [] some_pointer_to_class_array; calls the destructor of these classes, and its subclasses too (without calling destruction of subclasses in class destructor)

I would strongly urge you to master some of the basic basics of C++ before going any further.


Please note:  constructing an object always calls a constructor, and destroying an object always calls its destructor.  Always.  Remember, copy constructors are constructors.  Learn the rule of 3 (or 5 for the more advanced student).


Contained objects get constructed and destroyed when the object that holds them is constructed and destroyed.  Always.  If you explicitly invoke the destructor of a contained object in the destructor of the object that holds it, that object will be destroyed twice destroyed twice and you will probably corrupt your heap.  It's called a double delete.


Unless you're an advanced C++ user writing very specialized code, you never invoke a destructor directly.

#5256098 Drawing graphics in C++ w/out APIs?

Posted by Bregma on 07 October 2015 - 03:57 PM

Can you communicate directly with drivers and graphics card using C++?



There's two layers:  the OS layer and the userland layer.


You can examine the source code for both of these layers if you look at the open drivers for the Linux operating system.  No magic.  It's all just C code (likely compilable wit ha C++ compiler, because that's what C++ is all about).


At the lower (kernel) level, the GPU has a bunch of memory-mapped registers (ie. you read or write data at a specified memory location and it sets or gets values from hardware registers).  A lot of GPUs have dual-ported memory (ie. they can read and write data from the CPU's main memory).  There's often interrups as well:  these are events triggerd by the hardware the need to be handled by the CPU, but are really just subroutines in the kernel driver.  All drivers these days at least for the Linux kernel tend to be written in C and C only, for portability.


If you choose to write your own driver using C code, it's not going to be portable.  That's the whole raison d'etre of an API like OpenGL:  it hides all the ugly hairy and warty bits behind a thin layer of stretchy fabric so you can sleep at night.

#5255983 What's the best system on which to learn ASM?

Posted by Bregma on 07 October 2015 - 04:45 AM

Oh, and one more thing:  I first learned all about computers using assembly language for the CARDIAC computer -- it was the early 1970s, there were no PCs.  Seriously, you could do worse.  It was easy enough for a 10-year-old to understand and serious enough to propel him on a successful career in the industry when the industry didn't yet exist.

#5255829 What's the best system on which to learn ASM?

Posted by Bregma on 06 October 2015 - 10:15 AM

Get yourself a Raspberry Pi2B or a BeagleBone Black.  These are ARM boards -- the ARM processor is way simpler than the x86 even though they're of the same relative vintage (ARM is RISC, x86 is Swiss Army knife).  In addition to learning ASM, you can futz with blinkenlights so you can really feel close to the metal.  Learn to love the smell of rosin-core solder, it's the smell of victory.

#5255328 I'm sleepy and confused: __try, __except, /EHa, C2712 (unwinding)

Posted by Bregma on 03 October 2015 - 06:45 AM

C++ exceptions are built on SEH. It will quite happily execute any C++ handlers along the way when unwinding.

Really, this is your misunderstanding.


/EHa causes the compiler to emit code such that SEH exceptions are handled like C++ exception, meaning they'll be caught by catch(...).  Plain and simple.  An SEH will not otherwise cause destructors to get invoked during unwinding, because the SEH mechanism knows nothing aout the C++ runtime model.


SEH are not C++ exceptions, unless you use /EHa.  Only C++ exceptions invoke C++ destructors. You need to use try/catch (and not __try/__except) to use C++ exceptions.


I suspect the compiler is just trying to save you from the consequences of your misunderstanding.

#5254954 How can I make a text parser?

Posted by Bregma on 01 October 2015 - 04:42 AM

I just looked it up on google, it seems it has what I need to make a simple text parser.

No, it has what you need for a text matcher, a part of lexical anaylsis (trust me, I implemented a significant part of the regex library for GCC).  It's kind of like saying a couple of ice cubes in your cocktail is an iceburg and can sink ocean liners (disclaimer: do not captain while drunk).


It will definitely do simple pattern matching using captures, which is probably good enough for extracting data from a basic file with some simple data using a crude grammar.  In fact, I'd recommend it except I know at least one of the guys who implemented a significant part of that library is a jerk and I wouldn't trust anything he wrote.


Do yourself a favour and write a suite of unit tests for each of your initialization file lines.  You will thank me.

#5254863 How can I make a text parser?

Posted by Bregma on 30 September 2015 - 02:04 PM

How can I make a text parser?

I hope you realize this is an entire branch of computer science?

Really, if you're not writing a text parser as an end in itself, choose to use a widespread common format (XML, YAML, JSON, INI) and use a library. There are many, all of which have been tested.


Otherwise, look in to writing lexical analyzers, defining grammars built in the lexemes and using semantic insertion, and creating parsers for those grammars.  There's handy tools for that, like boost::spirit (if it's still around).

#5254363 Singleton and template Error (C++)

Posted by Bregma on 28 September 2015 - 07:07 AM

Error 2 error LNK1120: 1 unresolved externals E:\WORK\InLine2D\InLine2D\Debug\InLine2D.exe 1 1 InLine2D

Anyway, back to the OP's question.


Whenever you're trying to do something in C++, it's always a good idea to look to see how the standard C++ already does it, because there have been a lot of expert input into the design and implementation of the standard C++ library.


In the standard C++ library, there is a global console logging object with a set of overloaded and templatized functions for inserting into it.  In fact, you're trying to create a similar Java-style singleton class to wrap the standard object that already does exactly what you need, except not in the style a Java develop mey be accustomed to, with static member functions returning object instances that are used to invoke instanced functions with overloaded procedures for combining formatting and streaming functions.


In the C++ standard library you will find a set of global logging stream objects that are designed to be instantiated on first use:  std::cout, std::cerr, and std::clog.  They have an accompanying suite of overloaded and templatized functions (operator<<()) that perform manipulation and insertion into those logging streams.  They have already solved your problems, consider using them instead of wrapping them in your own problematic code, or at least examine their design and emulate it yourself.


The C++ standard library logging objects are designed to be extended not only by overloading operator<<() for your own types but by replacing their streambufto give you control over things like final destination or setting categories and severities.  Notice the post linked to there was addressing pretty much exactly the same topic as this one, and is dated 2006.  The aswer is still completely valid over a decade later.

#5253665 C++ Graphics Library

Posted by Bregma on 23 September 2015 - 08:39 AM

Cairo is a drawing library.  You'll still need some kind of context for rendering, like say libSDL2, so you bitmaps will appear on the screen (unless you only want to target a particular platform, say Win32 -- which is still your context, but you're going to have to manually set up and tear down everything instead of have libSDL2/SFML/Allegro do it for you).  Also, you'll need to consider more than graphics because without audio or input, all you've got is a silent movie.

#5252933 How to format the text by adding word spacing and letter spacing?

Posted by Bregma on 18 September 2015 - 02:44 PM

I think the thing you want to look into in something called kerning.

Kerning is the art of spacing individual glyphs such that they appear to have a pleasing and even spacing within a word and usually just involves the pairwise overlap of individual glyphs.  I don't think it's what he's looking for here.


The art of wordflow and textual layout has many heuristics developed over the last few thousand years but for the most part you're going to need to calculate the text box extents from each word (calculated from the font metrics and, yes, kerning pair rules) and then do a standard 2D packing of those text boxes into your target layout.  DX is just used for rendering the pixels generated by the layed out text and fonts.

#5251704 Whats True -1?

Posted by Bregma on 11 September 2015 - 06:39 AM

WTF C++. Y U LET false IMPLICIT TO 0.0f?

Because it's compatible with C.


C didn't have a bool type, it used int instead, with false defined as 0 and true as not 0 (well, really, the Z bit being set in the PDP SR register was defined as false, and clear being defined as true..  also, not 0 is ~0 which represents -1 in 2's complement arithmetic, so true usually ended up as -1, tant pis).  It would be unexpected to not have an implicit conversion between 0 and 0.0, false is just 0, therefore there is an implicit conversion from false in C++ to 0.0f, because that will let old C code compile in C++.


But you knew that.

#5248978 What do you guys use as a key in RPG data structures?

Posted by Bregma on 26 August 2015 - 07:10 AM

There are "attack" objects, which all inherit from a base attack class (and may be composed of some other things in the future)—they do all the calculations given a character's stats and some damage multiplier. This will make it easier to give specific characters specific abilities. When performing an attack, one of these objects is created, passed to an attack-event, and an attack-handler applies all the appropriate damage.

An attack is an action, a verb.  You should not model verbs as nouns, or else you end up with anathemata like attack.do(hero, enemy) instead of a more natural hero.attack(enemy).  Attack handlers, attack events, attack objects:  what do those look like in real life?

#5248139 Memory management

Posted by Bregma on 21 August 2015 - 04:44 PM

So I ended up putting this in there:

virtual ~State() {};
For some reason the RAM still grows but after 5-6 switches between states it stabilizes. ohmy.png
I guess that solved it. Not to sure about why it keeps growing in the beginning though. Thank you!



First, your problem was you did not declare the destructor of State virtual (you have to explicitly do that, or the derived destructors do not get called when you delete by a pointer to the base class).  Traditionally in a pure virtual base class you do that by making it pure abstract virtual and giving it a body.  Like this.

class State {
    virtual ~State() = 0;

And in a .cpp file, add this.

{ }

You add the destructor body in a separate .cpp file because it tells some compilers where to emit code for the various virtual thunks (don't worry about it; trust me, it's a good idea when using certain compilers).  You could, for longer link times, just add it as an out-of-class inline member function definition.  In summary, the default compiler-supplied destructor does not do what you need here, so you have to provide your own.  There is always a default compiler-supplied destructor if you do not supply your own.  Even if you don't think there is one.


Second, it's very likely you're seeing memory growth at the start because standard library containers (including smart pointers) use a pooling allocator by default, which hangs on to its freed memory.  This is normal and under most circumstances a desirable thing.

#5248094 Memory management

Posted by Bregma on 21 August 2015 - 12:22 PM

I'm curious why you would have explicit init/destroy calls that get manually invoked after/before the constructor and destructos of your State class.  WHat would happen if you moved those functions into the constructor/destructor?

#5247846 Matching Parameter Names to Class Fields

Posted by Bregma on 20 August 2015 - 06:14 AM

I am not a fan of "coding standards" or other petty dominance games in general;  I have rarely seen them contribute positively to long-term overall productivity.  I also abhor the type wrats misnamed as Hungarian notation often used to obfiscate code.


That said, I find an appropriate naming convention to indicate scopishness in C++ to be a major win.  I have found over many years that decorating member variables and namespace-level variables while leaving local variables (including function parameters) undecorated to be a highly desirable habit.  It took me and my curmugeonly ways a long to ti arive at this conclusion, but I now embrace it.


Be wary of using an underscore prefix for this wart, because you stand a good chance of running afoul of language rules.  See ISO/IEC 9899:2011 [7.1.3](1) bullet 2, which effectively precludes the use of an underscore prefix for a member variable.