Rectangle 27 21

C++ gives you three ways to pass an object: by pointer, by reference, and by value. Java limits you with the latter one (the only exception is primitive types like int, boolean etc). If you want to use C++ not just like a weird toy, then you'd better get to know the difference between these three ways.

Java pretends that there is no such problem as 'who and when should destroy this?'. The answer is: The Garbage Collector, Great and Awful. Nevertheless, it can't provide 100% protection against memory leaks (yes, java can leak memory). Actually, GC gives you a false sense of safety. The bigger your SUV, the longer your way to the evacuator.

C++ leaves you face-to-face with object's lifecycle management. Well, there are means to deal with that (smart pointers family, QObject in Qt and so on), but none of them can be used in 'fire and forget' manner like GC: you should always keep in mind memory handling. Not only should you care about destroying an object, you also have to avoid destroying the same object more than once.

Not scared yet? Ok: cyclic references - handle them yourself, human. And remember: kill each object precisely once, we C++ runtimes don't like those who mess with corpses, leave dead ones alone.

When you pass your object around by value, not by pointer or by reference, you copy the object (the whole object, whether it's a couple of bytes or a huge database dump - you're smart enough to care to avoid latter, aren't you?) every time you do '='. And to access the object's members, you use '.' (dot).

When you pass your object by pointer, you copy just a few bytes (4 on 32-bit systems, 8 on 64-bit ones), namely - the address of this object. And to show this to everyone, you use this fancy '->' operator when you access the members. Or you can use the combination of '*' and '.'.

When you use references, then you get the pointer that pretends to be a value. It's a pointer, but you access the members through '.'.

struct MyStruct
{
    int* someIntPointer, someInt; //here comes the surprise
    MyStruct *somePointer;
    MyStruct &someReference;
};

MyStruct s1; //we allocated an object on stack, not in heap

s1.someInt = 1; //someInt is of type 'int', not 'int*' - value/pointer modifier is individual
s1.someIntPointer = &s1.someInt;
*s1.someIntPointer = 2; //now s1.someInt has value '2'
s1.somePointer = &s1;
s1.someReference = s1; //note there is no '&' operator: reference tries to look like value
s1.somePointer->someInt = 3; //now s1.someInt has value '3'
*(s1.somePointer).someInt = 3; //same as above line
*s1.somePointer->someIntPointer = 4; //now s1.someInt has value '4'

s1.someReference.someInt = 5; //now s1.someInt has value '5'
                              //although someReference is not value, it's members are accessed through '.'

MyStruct s2 = s1; //'NO WAY' the compiler will say. Go define your '=' operator and come back.

//OK, assume we have '=' defined in MyStruct

s2.someInt = 0; //s2.someInt == 0, but s1.someInt is still 5 - it's two completely different objects, not the references to the same one
std::auto_ptr

Pretty sure you can't have a reference as a member without also providing a constructor with an initialization list that includes the reference variable. (A reference has to be initialized immediately. Even the constructor body is too late to set it, IIRC.)

c++ - Why should I use a pointer rather than the object itself? - Stac...

c++ pointers c++11
Rectangle 27 21

C++ gives you three ways to pass an object: by pointer, by reference, and by value. Java limits you with the latter one (the only exception is primitive types like int, boolean etc). If you want to use C++ not just like a weird toy, then you'd better get to know the difference between these three ways.

Java pretends that there is no such problem as 'who and when should destroy this?'. The answer is: The Garbage Collector, Great and Awful. Nevertheless, it can't provide 100% protection against memory leaks (yes, java can leak memory). Actually, GC gives you a false sense of safety. The bigger your SUV, the longer your way to the evacuator.

C++ leaves you face-to-face with object's lifecycle management. Well, there are means to deal with that (smart pointers family, QObject in Qt and so on), but none of them can be used in 'fire and forget' manner like GC: you should always keep in mind memory handling. Not only should you care about destroying an object, you also have to avoid destroying the same object more than once.

Not scared yet? Ok: cyclic references - handle them yourself, human. And remember: kill each object precisely once, we C++ runtimes don't like those who mess with corpses, leave dead ones alone.

When you pass your object around by value, not by pointer or by reference, you copy the object (the whole object, whether it's a couple of bytes or a huge database dump - you're smart enough to care to avoid latter, aren't you?) every time you do '='. And to access the object's members, you use '.' (dot).

When you pass your object by pointer, you copy just a few bytes (4 on 32-bit systems, 8 on 64-bit ones), namely - the address of this object. And to show this to everyone, you use this fancy '->' operator when you access the members. Or you can use the combination of '*' and '.'.

When you use references, then you get the pointer that pretends to be a value. It's a pointer, but you access the members through '.'.

struct MyStruct
{
    int* someIntPointer, someInt; //here comes the surprise
    MyStruct *somePointer;
    MyStruct &someReference;
};

MyStruct s1; //we allocated an object on stack, not in heap

s1.someInt = 1; //someInt is of type 'int', not 'int*' - value/pointer modifier is individual
s1.someIntPointer = &s1.someInt;
*s1.someIntPointer = 2; //now s1.someInt has value '2'
s1.somePointer = &s1;
s1.someReference = s1; //note there is no '&' operator: reference tries to look like value
s1.somePointer->someInt = 3; //now s1.someInt has value '3'
*(s1.somePointer).someInt = 3; //same as above line
*s1.somePointer->someIntPointer = 4; //now s1.someInt has value '4'

s1.someReference.someInt = 5; //now s1.someInt has value '5'
                              //although someReference is not value, it's members are accessed through '.'

MyStruct s2 = s1; //'NO WAY' the compiler will say. Go define your '=' operator and come back.

//OK, assume we have '=' defined in MyStruct

s2.someInt = 0; //s2.someInt == 0, but s1.someInt is still 5 - it's two completely different objects, not the references to the same one
std::auto_ptr

Pretty sure you can't have a reference as a member without also providing a constructor with an initialization list that includes the reference variable. (A reference has to be initialized immediately. Even the constructor body is too late to set it, IIRC.)

c++ - Why should I use a pointer rather than the object itself? - Stac...

c++ pointers c++11
Rectangle 27 21

C++ gives you three ways to pass an object: by pointer, by reference, and by value. Java limits you with the latter one (the only exception is primitive types like int, boolean etc). If you want to use C++ not just like a weird toy, then you'd better get to know the difference between these three ways.

Java pretends that there is no such problem as 'who and when should destroy this?'. The answer is: The Garbage Collector, Great and Awful. Nevertheless, it can't provide 100% protection against memory leaks (yes, java can leak memory). Actually, GC gives you a false sense of safety. The bigger your SUV, the longer your way to the evacuator.

C++ leaves you face-to-face with object's lifecycle management. Well, there are means to deal with that (smart pointers family, QObject in Qt and so on), but none of them can be used in 'fire and forget' manner like GC: you should always keep in mind memory handling. Not only should you care about destroying an object, you also have to avoid destroying the same object more than once.

Not scared yet? Ok: cyclic references - handle them yourself, human. And remember: kill each object precisely once, we C++ runtimes don't like those who mess with corpses, leave dead ones alone.

When you pass your object around by value, not by pointer or by reference, you copy the object (the whole object, whether it's a couple of bytes or a huge database dump - you're smart enough to care to avoid latter, aren't you?) every time you do '='. And to access the object's members, you use '.' (dot).

When you pass your object by pointer, you copy just a few bytes (4 on 32-bit systems, 8 on 64-bit ones), namely - the address of this object. And to show this to everyone, you use this fancy '->' operator when you access the members. Or you can use the combination of '*' and '.'.

When you use references, then you get the pointer that pretends to be a value. It's a pointer, but you access the members through '.'.

struct MyStruct
{
    int* someIntPointer, someInt; //here comes the surprise
    MyStruct *somePointer;
    MyStruct &someReference;
};

MyStruct s1; //we allocated an object on stack, not in heap

s1.someInt = 1; //someInt is of type 'int', not 'int*' - value/pointer modifier is individual
s1.someIntPointer = &s1.someInt;
*s1.someIntPointer = 2; //now s1.someInt has value '2'
s1.somePointer = &s1;
s1.someReference = s1; //note there is no '&' operator: reference tries to look like value
s1.somePointer->someInt = 3; //now s1.someInt has value '3'
*(s1.somePointer).someInt = 3; //same as above line
*s1.somePointer->someIntPointer = 4; //now s1.someInt has value '4'

s1.someReference.someInt = 5; //now s1.someInt has value '5'
                              //although someReference is not value, it's members are accessed through '.'

MyStruct s2 = s1; //'NO WAY' the compiler will say. Go define your '=' operator and come back.

//OK, assume we have '=' defined in MyStruct

s2.someInt = 0; //s2.someInt == 0, but s1.someInt is still 5 - it's two completely different objects, not the references to the same one
std::auto_ptr

Pretty sure you can't have a reference as a member without also providing a constructor with an initialization list that includes the reference variable. (A reference has to be initialized immediately. Even the constructor body is too late to set it, IIRC.)

c++ - Why should I use a pointer rather than the object itself? - Stac...

c++ pointers c++11
Rectangle 27 20

C++ gives you three ways to pass an object: by pointer, by reference, and by value. Java limits you with the latter one (the only exception is primitive types like int, boolean etc). If you want to use C++ not just like a weird toy, then you'd better get to know the difference between these three ways.

Java pretends that there is no such problem as 'who and when should destroy this?'. The answer is: The Garbage Collector, Great and Awful. Nevertheless, it can't provide 100% protection against memory leaks (yes, java can leak memory). Actually, GC gives you a false sense of safety. The bigger your SUV, the longer your way to the evacuator.

C++ leaves you face-to-face with object's lifecycle management. Well, there are means to deal with that (smart pointers family, QObject in Qt and so on), but none of them can be used in 'fire and forget' manner like GC: you should always keep in mind memory handling. Not only should you care about destroying an object, you also have to avoid destroying the same object more than once.

Not scared yet? Ok: cyclic references - handle them yourself, human. And remember: kill each object precisely once, we C++ runtimes don't like those who mess with corpses, leave dead ones alone.

When you pass your object around by value, not by pointer or by reference, you copy the object (the whole object, whether it's a couple of bytes or a huge database dump - you're smart enough to care to avoid latter, aren't you?) every time you do '='. And to access the object's members, you use '.' (dot).

When you pass your object by pointer, you copy just a few bytes (4 on 32-bit systems, 8 on 64-bit ones), namely - the address of this object. And to show this to everyone, you use this fancy '->' operator when you access the members. Or you can use the combination of '*' and '.'.

When you use references, then you get the pointer that pretends to be a value. It's a pointer, but you access the members through '.'.

struct MyStruct
{
    int* someIntPointer, someInt; //here comes the surprise
    MyStruct *somePointer;
    MyStruct &someReference;
};

MyStruct s1; //we allocated an object on stack, not in heap

s1.someInt = 1; //someInt is of type 'int', not 'int*' - value/pointer modifier is individual
s1.someIntPointer = &s1.someInt;
*s1.someIntPointer = 2; //now s1.someInt has value '2'
s1.somePointer = &s1;
s1.someReference = s1; //note there is no '&' operator: reference tries to look like value
s1.somePointer->someInt = 3; //now s1.someInt has value '3'
*(s1.somePointer).someInt = 3; //same as above line
*s1.somePointer->someIntPointer = 4; //now s1.someInt has value '4'

s1.someReference.someInt = 5; //now s1.someInt has value '5'
                              //although someReference is not value, it's members are accessed through '.'

MyStruct s2 = s1; //'NO WAY' the compiler will say. Go define your '=' operator and come back.

//OK, assume we have '=' defined in MyStruct

s2.someInt = 0; //s2.someInt == 0, but s1.someInt is still 5 - it's two completely different objects, not the references to the same one
std::auto_ptr

Pretty sure you can't have a reference as a member without also providing a constructor with an initialization list that includes the reference variable. (A reference has to be initialized immediately. Even the constructor body is too late to set it, IIRC.)

c++ - Why should I use a pointer rather than the object itself? - Stac...

c++ pointers c++11
Rectangle 27 6

Use of sleep in DisplayMessage is out of Celluloid scope.

Using your gists, I verified this issue can be reproduced in MRI 2.2.1 as well as jRuby 1.7.21 and Rubinius 2.5.8 ... The difference between server1.rb and server2.rb is the use of the DisplayMessage and message class method in the latter.

When sleep is used in server1.rb it is using Celluloid.sleep in actuality, but when used in server2.rb it is using Kernel.sleep ... which locks up the mailbox for Server until 60 seconds have passed. This prevents future method calls on that actor to be processed until the mailbox is processing messages ( method calls on the actor ) again.

def handle_message(message)
  defer {
    DisplayMessage.message(message)
  }
end
class DisplayMessage
    def self.message(message)
      #de ...
      Celluloid.sleep 60
    end
end

To reiterate, the deeper issue is not the scope of sleep ... that's why defer and future are my best recommendation. But to post something here that came out in my comments:

Using defer or future pushes a task that would cause an actor to become tied up into another thread. If you use future, you can get the return value once the task is done, if you use defer you can fire & forget.

But better yet, create another actor for tasks that tend to get tied up, and even pool that other actor... if defer or future don't work for you.

I'd be more than happy to answer follow-up questions brought up by this question; we have a very active mailing list, and IRC channel. Your generous bounties are commendable, but plenty of us would help purely to help you.

Celluloid.sleep()
DisplayMessage.message()
actor sleeping

Celluloid can correctly determine the "actor" context even if methods of other objects are invoked by a Celluloid "actor"

@TheCodeArtist you mean change the wording in the parenthesis? Because invoking Celluloid.sleep does work, like in my code sample. The sleep method body I linked to does detect if it's being run from inside an actor, but it does need to be explicitly called ( if the call is made outside Celluloid scope )

"which will end up calling Kernel.sleep" does not appear to be true. I added logs in my local copy of celluloid.rb and saw that invoking Celluloid.sleep() within DisplayMessage.message() does call actor.sleep (not kernel.sleep())

... we're saying the same thing ... and if we're not, I'm very confused about what you mean, because your entire answer reiterates my bullet point several times. If explicitly called, Celluloid.sleep will invoke the appropriate method, whereas sleep by itself, if called without DisplayMessage scope, will call Kernel.sleep ... I will try to tweak that line to make it clearer?

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

ruby - Understanding Celluloid Concurrency - Stack Overflow

ruby multithreading zeromq celluloid
Rectangle 27 8

I have even seen examples where Docker is used inside Vagrant and whatnot but I don't get what are the benefits to do this in a practical sense; meaning that I understand the difference between VMs and containers and the logical separation the latter provide, but when should I use the one instead of the other and when Docker inside Vagrant?

Docker are frequently used inside Vagrant because it doesn't currenlty run on Mac OSX (see Kernel Requirements), which is very commonly used by developers.

Then to have your dev-team working on the same containers, builds and tests products on a laptop and later on "running at scale, in production, on VMs, bare metal, OpenStack clusters, public clouds and more", you need Vagrant on their Mac OSX.

That said, here you can see another awesome case study http://bit.ly/19h8gUk

There are now additional case studies available on the docker website, including videos.

vagrant - Can you explain Docker with a practical example/case? - Stac...

docker vagrant virtualization docker-container
Rectangle 27 8

I have even seen examples where Docker is used inside Vagrant and whatnot but I don't get what are the benefits to do this in a practical sense; meaning that I understand the difference between VMs and containers and the logical separation the latter provide, but when should I use the one instead of the other and when Docker inside Vagrant?

Docker are frequently used inside Vagrant because it doesn't currenlty run on Mac OSX (see Kernel Requirements), which is very commonly used by developers.

Then to have your dev-team working on the same containers, builds and tests products on a laptop and later on "running at scale, in production, on VMs, bare metal, OpenStack clusters, public clouds and more", you need Vagrant on their Mac OSX.

That said, here you can see another awesome case study http://bit.ly/19h8gUk

There are now additional case studies available on the docker website, including videos.

vagrant - Can you explain Docker with a practical example/case? - Stac...

docker vagrant virtualization docker-container
Rectangle 27 4

There are no standard conformant ways of forcing objects in libraries to be initialised - you have to use tricks depending on your particular platform(s). The difference between a DLL and and a static library (on Windows, at least) is that the former has start-up and shut-down code that is executed by the OS, whereas the latter is just a concatenation of object files.

Also, the linker is not optimising away your start up code - it is simply not linking it, because it apparently is never used. Linkers are pretty stupid beasts - if you want to find out how they do what they do, take a look at the book Linkers & Loaders.

What's the difference between a linker optimizing code away and not linking it? I thought this was just two names for the same thing.

C++ was designed to work with existing linkers (see Stroustrup's D&E book) which had/have no concept of constructors.

Well, you can do the same in C: int sRegisterFoo() {...}; static int sFooRegistry = sRegisterFoo();

There is no guarantee that will get linked either.

c++ - How to prevent the linker from optimizing away startup code? - S...

c++ linker static-initialization
Rectangle 27 0

Also note the difference between @(Html.Raw()) and @Html.Raw() - you s...

Its usually a bad idea to try and combine server code and client strings inside the quotes of a property (ie onclick="something@(something())" )

Its better to just return the entire lot in a server side function

<a href="#" style="color:#0564c1;"                               
onclick="@Html.Raw(
  String.Format(
    "PopUpStart('POST','','',200,100,'{0}','{1}','ReloadPage',true);return false;"
    , Url.Action("index",address,new{id = baseObject.Id, type = dataTypeInt})
    , Url.Action("cancel",address,new{id = baseObject.Id, type = dataTypeInt})
  )
)"/>
Edit
</a>

Thanks, i Changed my RouteConfig.cs and with Your Answer Problem Solved

c# - How Can DeActivate Razor Html Encoding ? - Stack Overflow

c# asp.net-mvc asp.net-mvc-4 razor html-entities
Rectangle 27 0

Also note the difference between @(Html.Raw()) and @Html.Raw() - you s...

Its usually a bad idea to try and combine server code and client strings inside the quotes of a property (ie onclick="something@(something())" )

Its better to just return the entire lot in a server side function

<a href="#" style="color:#0564c1;"                               
onclick="@Html.Raw(
  String.Format(
    "PopUpStart('POST','','',200,100,'{0}','{1}','ReloadPage',true);return false;"
    , Url.Action("index",address,new{id = baseObject.Id, type = dataTypeInt})
    , Url.Action("cancel",address,new{id = baseObject.Id, type = dataTypeInt})
  )
)"/>
Edit
</a>

Thanks, i Changed my RouteConfig.cs and with Your Answer Problem Solved

c# - How Can DeActivate Razor Html Encoding ? - Stack Overflow

c# asp.net-mvc asp.net-mvc-4 razor html-entities