Jump to content

  • Log In with Google      Sign In   
  • Create Account

SiCrane

Member Since 09 Dec 1999
Offline Last Active Private

#4968545 C++ new/delete

Posted by SiCrane on 11 August 2012 - 05:04 PM

No. The second won't delete the first allocation.


#4967318 TypeCast

Posted by SiCrane on 08 August 2012 - 03:37 AM

LARGE_INTEGER is a union that contains a 64-bit integer, it's not actually a 64-bit integer type itself.


#4966937 New to pointers

Posted by SiCrane on 07 August 2012 - 02:10 AM

With C++03, in order for a type to be usable in a standard library container, it needs to have both a copy constructor and an assignment operator. With C++11, the situation is a little more complex. Only some of the operations require an assignment operator to be available. Some functions only need the type to be move constructable, copy constructable, etc. Since MSVC 2010 is more or less a C++11 compiler, it doesn't freak out if there's a missing assignment operator where other compilers might complain. If you change your function to:
void f() {
    int z;

    std::vector<xxx> a;
    a.push_back(xxx(z));

    std::vector<xxx> b;
    b = a; 
}
it should start complaining about the container assignment since container assignment requires that assignment of the contained type be allowed.


#4966341 Static methods and inheritance

Posted by SiCrane on 05 August 2012 - 05:58 AM

Yes, that using statement would unhide all of the base class' functions with that name.


#4966318 errors with angelscript.h

Posted by SiCrane on 05 August 2012 - 04:00 AM

The add-ons aren't compiled in the library. If you want to use an add-on you need to add it to your project in addition to the AngelScript library.


#4966316 Static methods and inheritance

Posted by SiCrane on 05 August 2012 - 03:45 AM

When you create a derived class any members with the same name as a name in the base class hides the name in the base class. Trying to access the name "Make" in the derived class won't find the version that accepts one argument because the no argument version hides the base class version. In order to make the base class version visible in the derived class you can use a using statement in the derived class. Ex:
class Derived : public Base<Derived> {
  public:
   using Base<Derived>::Make;
   static Derived* Make();
};
Putting virtual in the base class definition wouldn't work because virtual and static are mutually exclusive. Even if it was a non-static method, the two functions have different argument lists so the derived class version would still hide the base class version.


#4962440 Managing headers correctly within Vis C++ express

Posted by SiCrane on 23 July 2012 - 07:56 PM

You probably want to read this article on organizing code files in C and C++. In particular look at problem 4 in the article.


#4961785 different behavior of "sizeof()"

Posted by SiCrane on 21 July 2012 - 04:54 PM

@SiCrane: If you passed an array instead of a pointer, would it return 4 or 30?

In C++, a function argument with a type array of T is automatically transformed into a type of pointer to T. This includes function arguments where the size of the array is specified. So even if you made the argument a char[30] you'd still get a pointer size back.


#4961776 different behavior of "sizeof()"

Posted by SiCrane on 21 July 2012 - 03:57 PM

This is how sizeof is supposed to work. If you use sizeof on an array you get the size of the array. If you use sizeof on a pointer you get the size of the pointer. It doesn't matter what the pointer points to; if you ask for the size of a pointer, you'll get the size of the pointer.


#4959464 One-Step vs Two-Step Initialization (C++)

Posted by SiCrane on 16 July 2012 - 12:00 AM

This isn't a one-size-fits-all situation. There are many platforms with sufficiently bad exception support that even enabling exceptions as a compiler options is a mistake. Even if you do enable exceptions in your project, there are certain operations that have a sufficiently high chance of failure that throwing an exception would be inappropriate. Examples include opening a file or a network connection. Consider the C++ standard library. With a std::fstream you can specify a file name as constructor argument, but if the file isn't opened, it doesn't throw an exception. In comparison, if any of the std::string constructors fail an exception is thrown.


#4959200 Why aren't strings allowed in structures?

Posted by SiCrane on 15 July 2012 - 12:41 AM

C++ is case sensitive. string is not the same as String.


#4958600 Resource Pointer Design

Posted by SiCrane on 12 July 2012 - 06:34 PM

One option is to use an existing, well-known COM smart pointer like CComPtr. The fact that the interface is well-known helps compensate for some of the oddities that you've noticed trying to design your own smart pointer.


#4955269 Can any recursive function be a tail-recursive function?

Posted by SiCrane on 03 July 2012 - 06:44 AM

Anything can be rewritten an infinite number of ways, but in the purest sense, no not every recursive function can be rewritten tail-recursive instead. If it could then via a straightforward transformation we could change those functions to use iteration (which is generally more efficient) and have no use for recursion any more.
Using an explicit stack and tacking on some unrelated meaningless tail-recursion does not count as converting the algorithm to a tail-recursive implementation.

By that logic, there's "no use for recursion" in tail recursive functions at all and hence no point to the idea of tail recursion, because an existing tail recursive function can be trivially transformed into an iterative form as well. Just update the parameter variables and jump to the beginning of the function body, and you have your iterative version. However, not all languages are the same in terms of control structures, and iteration is non-idiomatic in many, if not most, functional languages. In those languages a tail recursive transformation has the same process and the same point as a manual transformation to an iterative version in an imperative language. In other words, a completely pointless waste of programmer time and computing resources in the ordinary case, but useful once in a while. Ultimately, they all express same algorithm, the difference is in the control structures used to implement that algorithm: regular recursion, tail recursion or iteration. Whether or not the change of expression of an algorithm represents a different "function" is a semantic argument.

Fibonacci cannot be written tail recursively as said, Some sorting algorithms that are written recursively I believe can't be tail recursive either.

Incorrect. Fibonacci can be written tail recursively. There's just no point to writing the naive implementation tail recursively because it requires explicitly duplicating the stack structure that the recursive calls automatically generate, and this implicit stack is almost always faster than any explicit stack structure. Any recursive algorithm can be written to use an explicit stack in place of an implicit stack of function calls. This includes Fibonacci, tree-traversal, searches, sorts or whatever recursive algorithm you can imagine. You push data on an explicitly managed stack that would normally be passed as parameters in a recursive call and then pop the stack as computations progress. Again, this is the same process that you would use to transform a recursive function into an iterative version of the same function. The only difference that instead of a loop operating on the explicit stack you use a tail recursive call on the explicit stack.

And that's just for a naive transformation. Actual examination of the algorithm can result in more sophisticated transformations. As an example, Dijkstra's algorithm in a tail-recursive Scheme implementation, which is essentially the same as alvaro's algorithm minus the special case for exponentiation of powers of three:
(define (fib n)
  (define (fib-aux a b p q count)
    (cond ((= count 0) b)
          ((even? count)
           (fib-aux a
                    b
                    (+ (* p p) (* q q))
                    (+ (* q q) (* 2 p q))
                    (/ count 2)))
          (else
           (fib-aux (+ (* b q) (* a q) (* a p))
                    (+ (* b p) (* a q))
                    p
                    q
                    (- count 1)))))
  (fib-aux 1 0 0 1 n))



#4955088 When can i use asGetActiveContext

Posted by SiCrane on 02 July 2012 - 04:44 PM

Well, the latest version of AngelScript implemented support for nested calls on a single context. You could give that a try.


#4954768 visual studio code optimization is breaking my code.

Posted by SiCrane on 01 July 2012 - 10:50 PM

If you want the compiler to know that a variable may be modified outside the code it can see at the time, you need to mark that variable as volatile (or reference it through a volatile reference, etc.).




PARTNERS