Interesting C++ Related Drivel

Posted by Astryl on Nov. 8, 2011, 3:17 p.m.

The title quite neatly cancels itself out.

Anyway, I just realized this morning that my C++ projects were all at a standstill, so I hurriedly opened Code::Blocks and stared at my Recent Projects list.

This frightened me, so I went back in time a bit to my old lolcode interpreter (Which you have not seen, yet). It worked, but it had some nasty memory leaks.

Therefore, I replaced most of this:

lines = (char*)malloc(sizeof(char)*lsize));
...
if(lines)free(lines);

(I apologize profusely to blackhole for the trouble I caused over this exact style of coding)

With this:

lines = new string[num_lines];
...
if(lines)delete[] lines;

This, I assure you, is the 'proper' (and sane) way to do things. Also, std::string is far more useful than a char array. :P

After doing that (And making the program work a bit better), I moved on to Hydra.

A day with Hydra

So, I opened my Hydra Library project, and the first thing that struck me was that it was damned messy.

You see, I haven't touched Hydra much in about a month, and during that time I was using (And enjoying) Java.

So that got me thinking about something the organization of the project, and the overall structure. And once again I wrote a lengthy design document detailing a new way to structure the source code, in a similar way to Java.

This basically means that everything in Hydra <insert arbitrary version number here> will be an Object. I have yet to figure out what this implies.

And with that

Forget everything you have just read, and comment.

Or comment and then read.

Comments

JuurianChi 12 years, 10 months ago

Quote:
Forget everything you have just read, and comment.
Forget what?

OH!

you're good.

BP Scraps 12 years, 10 months ago

Quote:
The title quite neatly cancels itself out.
This is going to bother me all week.

colseed 12 years, 10 months ago

)

DISASTER AVERTED.

Quote:
Also, std::string is far more useful than a char array. :P
Yes…yes it is

I remember the first C++ book I read started with strings by talking only about char arrays with null terminators…I thought everything string-related would have to be done with char arrays, but then I read another tutorial that talked about using String as a type, and life was happy again :D

blackhole 12 years, 10 months ago

If num_lines is constant you should have declared it on the stack. Otherwise, if you want to be coding in modern C++, you should probably use std::unique_ptr for automated cleanup (yes you can use unique_ptr on arrays). The exception is usually when its being thrown around in a tricky situation, or is inside of a class and thus can only be destroyed when the class is destroyed anyway.

The first method is still valid for certain performance critical situations. Note that the first method will generate a large block of memory, whereas the second creates fragmented chunks all over the place. This can be a very important distinction in certain cases (but not yours so its still terrible there).

Astryl 12 years, 10 months ago

Quote:
If num_lines is constant you should have declared it on the stack.

It isn't in most cases. I usually use arrays like this when I'm reading in data from a file, especially for text processing/byte reading. I could also use a std::list or std::vector, but I find plain ol' arrays to be faster for the reading operation (Especially when loading 3D models in the OBJ format).

Also, to be perfectly honest, I don't have the slightest clue what std::unique_ptr is. I shall investigate.