Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 04 Feb 2008
Online Last Active Today, 12:23 AM

#5307303 Errors that effect a computer's system.

Posted by on 22 August 2016 - 07:14 PM

Operating systems these days have layers upon layers of abstraction that can catch and recover from serious errors. For example, memory is virtualized, so a program cannot trash another program’s memory-- both program could have memory reserved at 0xC0000123, but is mapped to different locations of physical memory: 0xF0010123 and 0xF0020123. Files are also protected with permissions, you don’t accidentally overwrite important system files.

The worst you can do is crash your video driver, but Windows can generally recover. At worst, you might need to restart your computer.

Now, if you were writing kernel drivers, that’s another story altogether...

#5307108 IFSTREAM: take coordinate from file and convert 3 digit coordinate into integer

Posted by on 21 August 2016 - 09:13 PM

You’re almost there. std::string::c_str returns the actual contents of the string as a const char*; there’s no need to atoi every character in the array:
std::string input = "123";
int expected = 123;
int value = atoi(input.c_str());

assert( value == expected );

After that there's a problem, because I can't have an array that stores '1' '0' '0' because that would be 3 integers, and I don't think integers can be concatenated to get '100'.

Actually, you can using powers of ten: 123 == 1*100 + 2*10 + 3.
(N.B.: This is how we convert from hex to dec: 0x3E8 == 3*16^2 + 15*16^1 + 8*16^0 == 1000)
However, ASCII '0' has a different value from the literal zero, so we’ll need to “slide the ASCII chart” to bring '0' to a literal zero, like so:
char input[] = "123";
int expected = 123;
int value =
     (input[0]-'0') * 100 +
     (input[1]-'0') *  10 +
     (input[2]-'0') *   1;

assert( expected == value );
This works because the char type is an integer type and can be operating like regular integers. We could write the actual ASCII value of '0' (48) in place of '0' in the test above, but with '0' written out, it is more clear.

While there’s nothing wrong with using atoi, there are more C++-y ways to do parsing using streams:
std::string input = "123";
int expected = 123;
int value = 0;

std::istringstream stream(input);
stream >> value;

assert( expected == value );
And extending on the above, you could read your entire file:
int x;
int y;
int type;

// buffer to hold the non-integer tokens, used to validate that 
// the line is in the correct format
char dummy[4] = {};

string line;
ifstream file;
while ( getline(file, line) ) {
    istringstream stream(line);

    // line := x 'x' y '[' type ']'
    stream >> x >> dummy[0] >> y >> dummy[1] >> type >> dummy[2];

    if ( !stream ) {
        // not enough tokens, or a problem converting some of the integers

    if ( strcmp(dummy, "x[]") ) {
        // expected "100 x 200 [300]", but got something
        // like     "100 A 200 ?300~" instead

    // x, y, and type have been extracted from the line
    objects.push_back({x, y, type});

#5301603 Why didn't somebody tell me?

Posted by on 20 July 2016 - 01:18 PM

If your folder or file is name "StuffAndThings", with out having to click and use the search bar, just start typing "StuffAndThings" (like hitting the key for the first letter of the file to seek to all "s" files) and explorer will eventually land on and highlight the folder or file for you =). This is useful if their are 50 things that start with "S".

Except if the thing you're looking for is first in the list. If you have two folders that begin with the same letter listed first, say "bin" and "bin86", then pressing "b" will select "bin86" first, before "bin". For example, my root drive typically looks like this:

D:\bin → "Program Files"
D:\bin86 → "Program Files (x86)"
And so, if i want to go to "D:\bin\python\35": winkey+E, D:, enter, b, b, enter, python, ...

#5298952 Getting an error while running this program

Posted by on 03 July 2016 - 09:39 PM

It seems the code was written for an older version of Python. Starting with Python 3.0, print changed from a statement to a function. See: What’s New In Python 3.0.

If you are learning Python, then you may consider downgrading to an older version of python.
If you need different versions of python installed side-by-side, then you may consider using a virtual environment.

#5295428 Do you usually prefix your classes with the letter 'C' or something e...

Posted by on 07 June 2016 - 12:08 AM

lowercase_underscores, because I find the extra horizontal space easier to read than CompactPascalCase or compactCamelCase. And makes acronyms, however rare, easier to read: TWAIN_adapter instead of TWAINAdapter (TWAINA dapter?) or TWAINadapter (TWAI Nadapter?)...

I could do Pascal_Case_Underscores, but that’s a little 2crazy4me...

#5295023 Can't Get Bullets to Shoot

Posted by on 04 June 2016 - 07:02 PM

if (event.key.keysym.sym == SDLK_SPACE)
    gBullet.isAlive = true;
    gBullet.Y_Vel = 0.05f;                  // (2)
    gBullet.Y -= gBullet.Y_Vel * DeltaTime; // (1)
    gBullet.Rect.y = (int)gBullet.Y;

This code only moves the bullet (1) a single step, but only after pressing the space bar. Additionally, if these units are in pixels, then the bullet will take 20 seconds to move a single pixel (2). Obviously, the bullet should move regardless of whether of not the space bar was pressed, so it should be updated independently:
// Update time
ThisTime = SDL_GetTicks();
DeltaTime = (float)(ThisTime - LastTime) / MAX_TICKS;
LastTime = ThisTime;

// Process events
SDL_Event ev;
while ( SDL_GetEvent(&ev) )
    // ...

    if (event.key.keysym.sym == SDLK_SPACE)
        gBullet.isAlive = true;
        gBullet.Y_Vel = 300.0f;  // pixels per second

// Update entities
if ( gBullet.isAlive )
    gBullet.Y -= gBullet.Y_Vel * DeltaTime;  
    gBullet.Rect.y = (int)gBullet.Y;

#5294490 c++: sprintf but return chars. Possible?

Posted by on 01 June 2016 - 10:42 AM

Use StringBuilder:

class string_builder {
	template<typename T>
	string_builder& operator << (const T& val) {
		s << val;
		return *this;

	operator const std::string () const {
		return s.str();

	std::ostringstream s;

int main(int argc, char* argv[]) {
	try {
		// contrived example
		if ( argc <= 1 ) {
			throw std::runtime_error(
				string_builder() << "Not enough arguments, got " << argc
	catch ( const std::exception& ex ) {
		std::cerr << "Exception caught: " << ex.what() << std::endl;
Wait, this isn’t Java?

#5292953 My Texture to HBITMAP function leaks and I don't know why...

Posted by on 22 May 2016 - 05:33 PM

// assuming std::vector<uint8_t> CDirectXWrapper::image_buffer;

image_buffer.resize(textureDesc.Width * textureDesc.Height * 4); // may or may not trigger realloc
uint8_t* destination = image_buffer.data();

#5292949 My Texture to HBITMAP function leaks and I don't know why...

Posted by on 22 May 2016 - 05:16 PM

For starters, you assign new[]’d memory to destination, but you don’t delete[] it. You may also consider using std::vector instead, so you only need to make an allocation for every time the source image grows, instead of allocating every frame.

#5291979 Trying to draw area from image fails.

Posted by on 16 May 2016 - 07:06 PM

Integer division: 1 / 3 is 0.

Consider using float literals wherever you expect to have a non-integer result:

float topborder    = (1.f / n)	  * (t - 1);
float bottomborder = 1 - ((1.f / n) * (n - t));

#5291898 Plane equation, + or - D

Posted by on 16 May 2016 - 11:44 AM

I tend to use +D, since it’s consistent with transformations and the like:

   0 = Ax + By + Cz + D
→  0 = Px*Nx + Py*Ny + Pz*Nz + D
→  0 = (Px,Py,Pz,1) dot (Nx,Ny,Nz,D)
→  0 = (float4(pt.xyz,1)) dot (float4(normal,distance))

#5291455 my C++ console 3D fps Game on Windows from scratch

Posted by on 13 May 2016 - 03:03 PM

Pretty cool! I’ve done something similar:

The color space was generated by combining a combination of foreground colors, background colors, and the three fill chars:

U+0032 ' ' 0%
U+2591 '░' 25%
U+2592 '▒' 50%
U+2593 '▓' 75%

After throwing out duplicates, there were about 360 unique colors. I then used voro++ to do fast nearest-neighbor lookup in 3d space.
Then the process was rendering to an 80x60 image, then transforming (r,g,b) to (foreground,background,char) and sending it to the GPU to render.

But now with Windows 10, the terminals are all different P:

#5289431 Optimization issue when using operator=

Posted by on 30 April 2016 - 10:28 AM

A good compiler should optimize away your entire test. Therefore, it is dangerous to make assumptions based on what you have here.

Redacted-- Frob is right, I forgot which board I was posting in.
Also, upon a second look, I realized that operator* is probably a typo of operator*=

My post is useless.

#5289040 "Could not find or load main class . . ."

Posted by on 27 April 2016 - 11:38 PM

Eh? I thought the proper way to launch jars was:

java -jar /path/to/jar

#5288483 Noob pointer question

Posted by on 24 April 2016 - 01:03 PM

Yes. It fails because there was not an allocation made for the address of a in the heap.

There are two general memory allocation strategies for objects depending on the lifetime of the objects: Stack and heap.

Stack memory is super quick as it only increments a stack pointer register, but binds the lifetime of the object to the scope it was created in; FILO ordering.

Heap memory is slower in that a registry of allocations must be traversed to find space for the object. Contrast to stack memory, objects can be deallocated at any time-- it is not bound to the scope it was created in. A contrived example would be a particle explosion effect, where the particles fizz out at random times.

There are other allocation strategies as well, such as pool allocations.

Whenever you use a reference or pointer, you must be mindful lifetime of the object it refers to. For instance, while your first example is valid, you must be careful not to use a after deleting pA, as the integer it once referred to no longer exists.

// simple demo of scopes and object lifetimes

struct Foobar { int x; };

int main() {
    Foobar f1;
    Foobar* pf1;
    Foobar* pf2;
    Foobar* pf3;

    /* 1: */ {
        Foobar f2;
        Foobar f3;

        /* 2: */ {
            Foobar f4;

            pf1 = &f4;
            pf2 = new Foobar;
            pf3 = pf1;
        } // f4 destroyed, pf1 and pf3 are invalid!

        // pf3->x = 5;  // Undefined behavior-- you’d be lucky to crash here
    } // f3, f2 destroyed

    delete pf2;  // foobar located at pf2 destroyed
} // f1 destroyed