Search Results

Search found 15 results on 1 pages for 'gman'.

Page 1/1 | 1 

  • When does invoking a member function on a null instance result in undefined behavior?

    - by GMan
    This question arose in the comments of a now-deleted answer to this other question. Our question was asked in the comments by STingRaySC as: Where exactly do we invoke UB? Is it calling a member function through an invalid pointer? Or is it calling a member function that accesses member data through an invalid pointer? With the answer deleted I figured we might as well make it it's own question. Consider the following code: #include <iostream> struct foo { void bar(void) { std::cout << "gman was here" << std::endl; } void baz(void) { x = 5; } int x; }; int main(void) { foo* f = 0; f->bar(); // (a) f->baz(); // (b) } We expect (b) to crash, because there is no corresponding member x for the null pointer. In practice, (a) doesn't crash because the this pointer is never used. Because (b) dereferences the this pointer (this->x = 5;), and this is null, the program enters undefined behavior. Does (a) result in undefined behavior? What about if both functions are static?

    Read the article

  • With AMD style modules in JavaScript is there any benefit to namespaces?

    - by gman
    Coming from C++ originally and seeing lots of Java programmers doing the same we brought namespaces to JavaScript. See Google's closure library as an example where they have a main namespace, goog and under that many more namespaces like goog.async, goog.graphics But now, having learned the AMD style of requiring modules it seems like namespaces are kind of pointless in JavaScript. Not only pointless but even arguably an anti-pattern. What is AMD? It's a way of defining and including modules that removes all direct dependencies. Effectively you do this // some/module.js define([ 'name/of/needed/module', 'name/of/someother/needed/module', ], function( RefToNeededModule, RefToSomeOtherNeededModule) { ...code... return object or function }); This format lets the AMD support code know that this module needs name/of/needed/module.js and name/of/someother/needed/module.js loaded. The AMD code can load all the modules and then, assuming no circular dependencies, call the define function on each module in the correct order, record the object/function returned by the module as it calls them, and then call any other modules' define function with references to those modules. This seems to remove any need for namespaces. In your own code you can call the reference to any other module anything you want. For example if you had 2 string libraries, even if they define similar functions, as long as they follow the AMD pattern you can easily use both in the same module. No need for namespaces to solve that. It also means there's no hard coded dependencies. For example in Google's closure any module could directly reference another module with something like var value = goog.math.someMathFunc(otherValue) and if you're unlucky it will magically work where as with AMD style you'd have to explicitly include the math library otherwise the module wouldn't have a reference to it since there are no globals with AMD. On top of that dependency injection for testing becomes easy. None of the code in the AMD module references things by namespace so there is no hardcoded namespace paths, you can easily mock classes at testing time. Is there any other point to namespaces or is that something that C++ / Java programmers are bringing to JavaScript that arguably doesn't really belong?

    Read the article

  • dynamic 2d texture creation in unity from script

    - by gman
    I'm coming from HTML5 and I'm used to having the 2D Canvas API I can use to generate textures. Is there anything similar in Unity3D? For example, let's say at runtime I want to render a circle, put 3 initials in the middle and then take the result and put that in a texture. In HTML5 I'd do this var initials = "GAT"; var textureWidth = 256; var textureHeight = 256; // create a canvas var c = document.createElement("canvas"); c.width = textureWidth; c.height = textureHeight; var ctx = c.getContext("2d"); // Set the origin to the center of the canvas ctx.translate(textureWidth / 2, textureHeight / 2); // Draw a yellow circle ctx.fillStyle = "rgb(255,255,0)"; // yellow ctx.beginPath(); var radius = (Math.min(textureWidth, textureHeight) - 2) / 2; ctx.arc(0, 0, radius, 0, Math.PI * 2, true); ctx.fill(); // Draw some black initials in the middle. ctx.fillStyle = "rgb(0,0,0)"; ctx.font = "60pt Arial"; ctx.textAlign = "center"; ctx.fillText(initials, 0, 30); // now I can make a texture from that var tex = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, tex); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, c); gl.generateMipmap(gl.TEXTURE_2D); I know I can edit individual pixels in a Unity texture but is there any higher level API for drawing to texture in unity?

    Read the article

  • git workflow for separating commits

    - by gman
    Best practices with git (or any VCS for that matter) is supposed to be to have each commit do the smallest change possible. But, that doesn't match how I work at all. For example I recently I needed to add some code that checked if the version of a plugin to my system matched the versions the system supports. If not print a warning that the plugin probably requires a newer version of the system. While writing that code I decided I wanted the warnings to be colorized. I already had code that colorized error message so I edited that code. That code was in the startup module of one entry to the system. The plugin checking code was in another path that didn't use that entry point so I moved the colorization code into a separate module so both entry points could use it. On top of that, in order to test my plugin checking code works I need to go edit UI/UX code to make sure it tells the user "You need to upgrade". When all is said and done I've edited 10 files, changed dependencies, the 2 entry points are now both dependant on the colorization code, etc etc. Being lazy I'd probably just git add . && git commit -a the whole thing. Spending 10-15 minutes trying to manipulate all those changes into 3 to 6 smaller commits seems frustrating which brings up the question Are there workflows that work for you or that make this process easier? I don't think I can some how magically always modify stuff in the perfect order since I don't know that order until after I start modifying and seeing what comes up. I know I can git add --interactive etc but it seems, at least for me, kind of hard to know what I'm grabbing exactly the correct changes so that each commit is actually going to work. Also, since the changes are sitting in the current directory it doesn't seem like it would be easy to run tests on each commit to make sure it's going to work short of stashing all the changes. And then, if it were to stash and then run the tests, if I missed a few lines or accidentally added a few too many lines I have no idea how I'd easily recover from that. (as in either grab the missing lines from the stash and then put the rest back or take the few extra lines I shouldn't have grabbed and shove them into the stash for the next commit. Thoughts? Suggestions? PS: I hope this is an appropriate question. The help says development methodologies and processes

    Read the article

  • Unable to empty the rubbish bin

    - by gman
    When I attempt to empty the rubbish bin the preparing window opens but nothing happens. In testing I tried to delete 1 file from the rubbish bin and received a time-out error. No files are removed from the rubbish bin after this. I have added a screen shot that includes a display of system monitor that shows high CPU usage for trash and Nautilus; not sure whether it is related. Click for a full-resolution image. Also, on a separated note, Bleachbit was also freezing on me, which may or may not be related?

    Read the article

  • How to make RewriteCond+RewriteRule change domain2/folder1 to domain1/folder1

    - by gman
    There's actually 2 questions. One is, how do I make RewriteCond+RewriteRule change domain2/folder1 to domain1/folder1 Actually what I want is any domain that tries to access folder1 that is not domain1 gets switched to domain1. So for example domain2.com/domain1/foo - domain1.com/domain1/foo as well as domain3.com/domain1/foo - domain1.com/domain1/foo This is what I tried RewriteCond %{HTTP_HOST} !^domain1\.com$ [NC] RewriteCond %{REQUEST_URI} ^/folder1/ RewriteRule ^/folder1/(.*)$ http://domain1.com/folder1/$1 [L,R=permanent] But that doesn't work. Next I tried some a simpler rule to see if I could narrow down the issue. RewriteCond ${HTTP_HOST} domain2\.com [NC] RewriteRule ^(.*)$ http://google.com/ [L] I though that would make ANY request to domain2.com go to google.com so I tried http://domain2.com/foo but I get domain2.com/foo not google.com If I go to http://domain2.com I get google. Why don't I get there if I go to http://domain2.com/foo? What am I not understanding about mod_rewrite?

    Read the article

  • Getting template metaprogramming compile-time constants at runtime

    - by GMan - Save the Unicorns
    Background Consider the following: template <unsigned N> struct Fibonacci { enum { value = Fibonacci<N-1>::value + Fibonacci<N-2>::value }; }; template <> struct Fibonacci<1> { enum { value = 1 }; }; template <> struct Fibonacci<0> { enum { value = 0 }; }; This is a common example and we can get the value of a Fibonacci number as a compile-time constant: int main(void) { std::cout << "Fibonacci(15) = "; std::cout << Fibonacci<15>::value; std::cout << std::endl; } But you obviously cannot get the value at runtime: int main(void) { std::srand(static_cast<unsigned>(std::time(0))); // ensure the table exists up to a certain size // (even though the rest of the code won't work) static const unsigned fibbMax = 20; Fibonacci<fibbMax>::value; // get index into sequence unsigned fibb = std::rand() % fibbMax; std::cout << "Fibonacci(" << fibb << ") = "; std::cout << Fibonacci<fibb>::value; std::cout << std::endl; } Because fibb is not a compile-time constant. Question So my question is: What is the best way to peek into this table at run-time? The most obvious solution (and "solution" should be taken lightly), is to have a large switch statement: unsigned fibonacci(unsigned index) { switch (index) { case 0: return Fibonacci<0>::value; case 1: return Fibonacci<1>::value; case 2: return Fibonacci<2>::value; . . . case 20: return Fibonacci<20>::value; default: return fibonacci(index - 1) + fibonacci(index - 2); } } int main(void) { std::srand(static_cast<unsigned>(std::time(0))); static const unsigned fibbMax = 20; // get index into sequence unsigned fibb = std::rand() % fibbMax; std::cout << "Fibonacci(" << fibb << ") = "; std::cout << fibonacci(fibb); std::cout << std::endl; } But now the size of the table is very hard coded and it wouldn't be easy to expand it to say, 40. The only one I came up with that has a similiar method of query is this: template <int TableSize = 40> class FibonacciTable { public: enum { max = TableSize }; static unsigned get(unsigned index) { if (index == TableSize) { return Fibonacci<TableSize>::value; } else { // too far, pass downwards return FibonacciTable<TableSize - 1>::get(index); } } }; template <> class FibonacciTable<0> { public: enum { max = 0 }; static unsigned get(unsigned) { // doesn't matter, no where else to go. // must be 0, or the original value was // not in table return 0; } }; int main(void) { std::srand(static_cast<unsigned>(std::time(0))); // get index into sequence unsigned fibb = std::rand() % FibonacciTable<>::max; std::cout << "Fibonacci(" << fibb << ") = "; std::cout << FibonacciTable<>::get(fibb); std::cout << std::endl; } Which seems to work great. The only two problems I see are: Potentially large call stack, since calculating Fibonacci<2 requires we go through TableMax all the way to 2, and: If the value is outside of the table, it returns zero as opposed to calculating it. So is there something I am missing? It seems there should be a better way to pick out these values at runtime. A template metaprogramming version of a switch statement perhaps, that generates a switch statement up to a certain number? Thanks in advance.

    Read the article

  • Flash ActionScript 2.0 problem with the object's _visible parameter

    - by GMan
    Hey guys, I've been trying to build something simple in Flash 8, and I stumbled across something weird I cannot explain: I have an object, and at some point of the program, I want it to be visible (it is invisible at first), so I write: _root.myObj._visible = true; _root.gameOver.swapDepths(_root.getNextHighestDepth()); //so it will be on the top and this works fine, the object becomes visible and etc. What I planned to happen next is that the user presses a button on that same object, and the object will go invisible: on(release) { trace(_root.myObj._visible); _root.myObj._visible = false; trace(_root.myObj._visible); _root.gotoAndPlay("three"); } The trace returns at first true and later on false, so the command works, but oddly the object stays visible, that's what I don't understand. Thanks everybody in advance.

    Read the article

  • Range-based `for` statement definition redundancy

    - by GMan - Save the Unicorns
    Looking at n3092, in §6.5.4 we find the equivalency for a range-based for loop. It then goes on to say what __begin and __end are equal to. It differentiates between arrays and other types, and I find this redundant (aka, confusing). It says for arrays types that __begin and __end are what you expect: a pointer to the first and a pointer to one-past the end. Then for other types, __begin and __end are equal to begin(__range) and end(__range), with ADL. Namespace std is associated, in order to find the std::begin and std::end defined in <iterator>, §24.6.5. However, if we look at the definition of std::begin and std::end, they are both defined for arrays as well as container types. And the array versions do exactly the same as above: pointer to the first, pointer to one-past the end. Why is there a need to differentiate arrays from other types, when the definition given for other types would work just as well, finding std::begin and std::end? Some abridged quotes for convenience: §24.6.5 The range-based for statement — if _RangeT is an array type, begin-expr and end-expr are __range and __range + __bound, respectively, where __bound is the array bound. If _RangeT is an array of unknown size or an array of incomplete type, the program is ill-formed. — otherwise, begin-expr and end-expr are begin(_range) and end(_range), respectively, where begin and end are looked up with argument-dependent lookup (3.4.2). For the purposes of this name lookup, namespace std is an associated namespace. and §24.6.5 range access template T* begin(T (&array)[N]); Returns: array. template T* end(T (&array)[N]); Returns: array + N.

    Read the article

  • Are function-local typedefs visible inside C++0x lambdas?

    - by GMan - Save the Unicorns
    I've run into a strange problem. The following simplified code reproduces the problem in MSVC 2010 Beta 2: template <typename T> struct dummy { static T foo(void) { return T(); } }; int main(void) { typedef dummy<bool> dummy_type; auto x = [](void){ bool b = dummy_type::foo(); }; // auto x = [](void){ bool b = dummy<bool>::foo(); }; // works } The typedef I created locally in the function doesn't seem to be visible in the lambda. If I replace the typedef with the actual type, it works as expected. Here are some other test cases: // crashes the compiler, credit to Tarydon int main(void) { struct dummy {}; auto x = [](void){ dummy d; }; } // works as expected int main(void) { typedef int integer; auto x = [](void){ integer i = 0; }; } I don't have g++ 4.5 available to test it, right now. Is this some strange rule in C++0x, or just a bug in the compiler? From the results above, I'm leaning towards bug. Though the crash is definitely a bug. For now, I have filed two bug reports. All code snippets above should compile. The error has to do with using the scope resolution on locally defined scopes. (Spotted by dvide.) And the crash bug has to do with... who knows. :) Update According to the bug reports, they have both been fixed for the next release of Visual Studio 2010.

    Read the article

  • ActionScript 2.0 problem with the object's _visible parameter

    - by GMan
    Hey guys, I've been trying to build something simple in Flash 8, and I stumbled across something weird I cannot explain: I have an object, and at some point of the program, I want it to be visible (it is invisible at first), so I write: _root.myObj._visible = true; _root.gameOver.swapDepths(_root.getNextHighestDepth()); //so it will be on the top and this works fine, the object becomes visible and etc. What I planned to happen next is that the user presses a button on that same object, and the object will go invisible: on(release) { trace(_root.myObj._visible); _root.myObj._visible = false; trace(_root.myObj._visible); _root.gotoAndPlay("three"); } The trace returns at first true and later on false, so the command works, but oddly the object stays visible, that's what I don't understand. Thanks everybody in advance.

    Read the article

  • Fastest container or algorithm for unique reusable ids in C++

    - by gman
    I have a need for unique reusable ids. The user can choose his own ids or he can ask for a free one. The API is basically class IdManager { public: int AllocateId(); // Allocates an id void FreeId(int id); // Frees an id so it can be used again bool MarkAsUsed(int id); // Let's the user register an id. // returns false if the id was already used. }; Assume ids happen to start at 1 and progress, 2, 3, etc. This is not a requirement, just to help illustrate. IdManager mgr; mgr.MarkAsUsed(3); printf ("%d\n", mgr.AllocateId()); printf ("%d\n", mgr.AllocateId()); printf ("%d\n", mgr.AllocateId()); Would print 1 2 4 Because id 3 has already been declared used. What's the best container / algorithm to both remember which ids are used AND find a free id? If you want to know the a specific use case, OpenGL's glGenTextures, glBindTexture and glDeleteTextures are equivalent to AllocateId, MarkAsUsed and FreeId

    Read the article

  • Why is there a sizeof... operator in C++0x?

    - by Motti
    I saw that @GMan implemented a version of sizeof... for variadic templates which (as far as I can tell) is equivalent to the built in sizeof.... Doesn't this go against the design principle of not adding anything to the core language if it can be implemented as a library function[citation needed]?

    Read the article

  • Why is my mysql database timestamp changing by itself?

    - by Scarface
    Hey guys quick question, I have an entry that I put in my database, and as I echo the value, the value in the database stays the same while the data echoed keeps increasing, which is messing up my function. If anyone knows whats going down, would appreciate any suggestions. <?php include("../includes/connection.php"); $query="SELECT * FROM points LEFT JOIN users ON points.user_id=users.id WHERE points.topic_id='82' AND users.username='gman'"; $check=mysql_query($query); while ($row=mysql_fetch_assoc($check)){ $points_id=$row['points_id']; echo $timestamp=$row['timestamp']; } ?>

    Read the article

1