Jump to content

  • Log In with Google      Sign In   
  • Create Account

fastcall22

Member Since 04 Feb 2008
Offline Last Active Today, 01:44 AM

#5289431 Optimization issue when using operator=

Posted by fastcall22 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.

EDIT:
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 fastcall22 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 fastcall22 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



#5288358 How do I link my DLLs to Code Blocks?

Posted by fastcall22 on 23 April 2016 - 06:00 PM

I tried copying the dll file directly into the folder my project is in, but the system just comes up with another missing dll to complain about...


You can use Dependency Walker to find all the DLLs you need, though I suspect you would only need to copy all the allegro DLLs.
 

There are mainly two places where the DLLs can be copied, the system32 directory, or the directory the program is in...

Actually, putting the DLLs into the system directory is the "correct thing"...


I disagree. According to Dynamic-Link Library Search Order for Desktop Applications, it also searches %PATH%, which I argue would be much cleaner than whoring out the system directory to third party libraries. Just add the allegro binary directory to %PATH% and you should be good to go.

I prefer copying (or hard-linking) the DLLs directly into the project directory, that way I can zip-and-deploy at any time. And also avoids the problem (for some libraries):
libwowamazing.dll: x86 or x64? debug or release? Linked against the mutli-threaded runtime or the static runtime?


#5287853 Negative ping? Or how do I sync time with client and server?

Posted by fastcall22 on 20 April 2016 - 03:51 PM

I suspect phone's time is not really correct.


Bingo. Clocks aren’t perfect, analog or digital. Think of a mechanical watch, always slipping either a few minutes ahead or behind over time. Digital clocks suffer the same problem, but on a much smaller scale (see: how can a digital clock drift in accuracy?). These days, your system periodically syncs the time with a nearby time server. This helps, but you still don’t have the exact time, unless you can you guarantee that all time servers have the exact same time.
 

What do you guys suggest?


Just ask the server what time it is, and adjust accordingly:

1. Client requests server time.
2. Client adjusts the value returned by the server, according to the time it took for that request to return.
3. Use this value as a baseline for all timestamps returned by the server, or repeat and average error.


#5287824 How to randomize a string variable

Posted by fastcall22 on 20 April 2016 - 01:41 PM

Important note: The system time in seconds is generally used to seed a new random number generator. Random number generators created within the same second will generate the same sequence of numbers.

Therefore, you shouldn’t make that “somewhere” in “create a new random number generator somewhere in your code” be immediately before its use, but rather, as a member variable initialized at load-time.


#5287729 std::sort corrupts my memory

Posted by fastcall22 on 20 April 2016 - 01:16 AM

SMyStruct::operator< is an invalid comparator. Write a well formed comparator for SMyStruct to fix:
 
bool SMyStruct::operator< ( const SMyStruct& m ) const {
    return std::tie(x, y) < std::tie(m.x, m.y); 
}

// or 

bool SMyStruct::operator< ( const SMyStruct& m ) const {
    if ( x < m.x )  return true;
    if ( x > m.x )  return false;
    if ( y < m.y )  return true;
    return false;
}

(Also note that this is the typical implementation of operator< for tuples and pairs.)


What compiler are you using? I can’t remember the last time anybody’s needed to add whitespace between right angle brackets in a template typename...


#5287692 Efficient click detection design

Posted by fastcall22 on 19 April 2016 - 04:53 PM

Either through elaborate GUI systems or something as simple by transforming the coordinates into an index into a list of items.


#5287647 FizzBuzz: Enterprise Edition!

Posted by fastcall22 on 19 April 2016 - 01:47 PM

For those not familiar with fizzbuzz: http://c2.com/cgi/wiki?FizzBuzzTest

There is an “enterprise implementation” of fizzbuzz available on GitHub: https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition


And even more amusing are its open issues:
https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition/issues?page=4&q=is%3Aissue+is%3Aopen


Some of my favorites:
#141: *URGENT* *NEED HELP ASAP* I can't run this on my LG Optimus L9
#13: FIPS PUB 199 COMPLIANCE
#216: 6 Month Release Cycle
#214: Can we have a REST API?
#224: A Great Job Opportunity For You


Also: This looks like spam, but it’s not!!

Also:
Posting because these are hitting a little too close to home right now... :(


#5287403 HINSTANCE & HWND Corrupted when passed around functions

Posted by fastcall22 on 17 April 2016 - 10:53 PM

It seems to be a slicing of WinEventArgs due to a permissive interface. At the time of passing in args to func, an assignment from const WinEventArgs& to IEventArgs is made. Your callback then receives this copy and erroneously downcasts to WinEventArgs, and accesses invalid memory.

So the first step is to lock down your interface:
// How do I C++?
struct IEventArgs {
protected:
	IEventArgs() = default;
	~IEventArgs() = default;

public:
	IEventArgs(const IEventArgs&) = delete;
	IEventArgs& operator = (const IEventArgs&) = delete;
};
The next step is to stop using const references on small data types. Not only will you need to be extremely mindful of the lifetimes of the objects they refer to, but you also incur double-indirection.
 
struct WinEventArgs : public IEventArgs
{
	WinEventArgs(const int a, const int b) : a(a), b(b) { }

	const int a;
	const int b;
};
The general rule of thumb is to pass-by-reference objects that are expensive to copy.
POD-types (int, float, WPARAM, LPARAM) and handles (such as HINSTANCE, HWND) are all integral-types. Pass-by-value*.
A vector<int> with 25000 elements? Pass-by-reference.


EDIT:
With the updated code above, Visual Studio reports:
std::function<void(IEventArgs)> func = std::bind(&Component::test, cmp, std::placeholders::_1);
func(args);  // C2280: 'IEventArgs::IEventArgs(const IEventArgs &)': attempting to reference a deleted function



#5287263 HINSTANCE & HWND Corrupted when passed around functions

Posted by fastcall22 on 16 April 2016 - 11:16 PM

There isn’t enough code here to determine the actual cause (though I would first suspect the down-cast from IEventArgs& to WinEventArgs&). Setup a data breakpoint to see exactly when the value is corrupted.


#5287262 Multiple bullets?

Posted by fastcall22 on 16 April 2016 - 11:09 PM

Anyway, look into "std::vector".


Yes, you would be correct if the OP was using C++. But the tags on his topic states “Java,” so he should use java.util.ArrayList instead.

The same principles apply, though:
ArrayList<Bullet> bullets = new ArrayList<Bullet>();

bullets.add(new Bullet(x, y));

for ( Bullet b : bullets )
    b.update();
}    
At some point, you will need the ability to remove bullets. Use an iterator instead:
ListIterator<Bullet> itr = bullets.listIterator();
while ( itr.hasNext() )
    Bullet b = itr.next();

    if ( !b.update() )
        itr.remove();
}



#5287204 I can't build the Box2D physics library in Visual Studio 2015

Posted by fastcall22 on 16 April 2016 - 12:02 PM

You may have warning levels set on your project to treat warnings as errors (/WX). Usually these warnings indicate a possible bug with your code, but since this isn’t your code, you may be safe in ignoring these warnings.


Relevant code in b2CollideCircle:
	// Find the min separating edge.
	int32 normalIndex = 0;
	float32 separation = -b2_maxFloat;                            // (line 64)
	float32 radius = polygonA->m_radius + circleB->m_radius;
	int32 vertexCount = polygonA->m_count;
	const b2Vec2* vertices = polygonA->m_vertices;
	const b2Vec2* normals = polygonA->m_normals;

⋮
⋮

		b2Vec2 faceCenter = 0.5f * (v1 + v2);
		float32 separation = b2Dot(cLocal - faceCenter, normals[vertIndex1]);  // (line 141)
		if (separation > radius)
		{
			return;
		}



#5287130 Computing an optimized mesh from a number of spheres?

Posted by fastcall22 on 15 April 2016 - 08:57 PM

Take a look at constructive solid geometry, it may be what you’re looking for.


#5287093 My octagon is a circle and my pentagon is wonky :'(

Posted by fastcall22 on 15 April 2016 - 02:22 PM

To expand on what SotL said, here’s a brief history on Windows terminals.

Prior to Windows 10, terminals used the System Typeface, which was a bitmap font carried over from the IBM days, and consequently stuck on Code Page 437. Unicode support was nonexistent, but at least you had pixel-perfect characters to make your rouge-like game.

Starting with Windows 10, the terminal was completely reworked and now supports Unicode and uses TrueType fonts with modern font rendering. It’s smooth as silk, but you no longer have square characters nor pixel perfect box drawing.

Additionally, since the terminal supports Unicode, you need to use the Unicode code points rather than Code Page 437 code points:
Before: cout << (char)251; // √
After: wcout << L'√' /* or: */ << (wchar_t)0x221A;


So, here are some of your options:

1. Configure your terminal to use "Terminal" with an 8x8 font size. This is the easiest solution. Inserting wide characters should map to the original Code Page 437 code points: 0x221A -> 251.

2. Account for font proportions in your code. This involves scaling the Y-axis such that 1 unit along the Y-axis appears to be the same length as 1 unit along the X-axis, even if it doesn’t quite match up 1:1 to rows and columns.

3. Use U+2584, U+2580, and U+2588 (▄, ▀, and █ respectively) to split a character into two. This is trickier to do, as you would need to render to an "off-page" image before you can determine what characters to use. Don’t forget to use wcout to insert a “wide-character” into cout.




PARTNERS