Sandbox Design Mistakes

You’re right, I’ll stop. :rofl:

This has gone wildly off-topic. Please take it back on topic and stop poking each other or we’ll close the thread.

3 Likes

Sandbox Day

image

The neverending sandbox

1 Like

while(geten){snark++;}

3 Likes

while(Bitcoin){Bitcoin.price++;}

I’m a C# developer by profession (Azure Architect, build and maintain RESTful services which call credit enrichment providers as part of aggregator-based insurer quote traffic) so this may not be correct with respect to C++, but pretty sure from my Computer Science degree that the principles are roughly the same within any OO language… Both your code is valid, but only 1 creates an instance of Spaceship.

This sets aside memory for a reference to an object of type Spaceship, but it does not in itself create an instance of Spaceship:

Spaceship spaceship;

For instance if Spaceship had a method Fly() you would not be able to write spaceship.Fly() as you would get an object reference not set exception (very common exception in debugging). Typically at a later step in the process you would see the following statement:

spaceship = new Spaceship();

The following on the other hand both creates a reference in memory as well as assigns an instance of type Spaceship which the reference points to, so you would be able to invoke spaceship.Fly() thereafter:

Spaceship spaceship = new Spaceship();

Essentially both code is fine and it has nothing to do with compile or run-time, you would use both depending on context, there are times when you want to immediately create an instance of something and other scenarios where you wouldn’t want to do that until something had taken place beforehand. The Builder or Factory patterns would be one example where you may not wish to create an instance of Spaceship until everything required to build one had been received.

you are wrong.

here is an example that literally says this call creates a new object, without a call to memory allocation (malloc/new).

so you are able, in cpp, to write

Spaceship spaceship;
spaceship.fly();

And this works.

Yep you’re right, looks like C++ works differently here.

Just to highlight the diff here’s an example from an Eve Swagger auth client i wrote in C#:

There’s a good thread on Stack Overflow which covers this very diff:

Every day is a learning day!

2 Likes

Eve is no longer a singleton

Yes, it’s because cpp is C, and in C you can use structure as parameter directly (so the structure is copied in the function call memory).
This is not specially a good thing, because then you can do something like

Spaceship* getIt(){
 Spaceship ret;
 return &ret;
}

Which will return the address of the internal function variable ret, which will be on the heap, and after the function exits the heap may be reallocated to another function call and thus you will have data corruption.

That’s why in modern languages every object is allocated on the stack ; but it’s good to know that instanciation and allocation are two different things.

Especially in cpp where you can redefine your “new” operator

in that case, even a

Spaceship* sp = new Spaceship(params) ;

may result in no memory allocation. This is useful eg if you want to create your own buffer of objects to return, and get one of them eg with a bit array to check for access. AND possibly create a huge buffer, split in n parts, each containing x chunks of size 2^n to allocate from them. This is much faster than using default new, although it can lead to fragmentation (up to half memory space can become unusable in a part).

All those mechanism are actually very bug-prone and so are removed from younger languages. Yet for example in java you can manipulate the metaclass, you can by pass runtime execution, but all those possibilities are either encapsulated in specific classes that can be removed at runtime(so your program will fail), or directly encapsulated in specific classes that are safe to use.

So people believe that “instanciation => allocation” but this is not true, just hidden. Syntaxic sugar sometimes make people believe it’s simpler than it is.

You so fail at troll.

Ignore Anderson, his post was just to be nit picky as usual.

If an object contains data, for everyday use:

Sure, if someone wants to continue picking at nits, then there are always exceptions in C++, because it is such an amazing and flexible language.

Using Spaceship spaceship; the memory is not dynamically allocated and managed for you, but the object ends when its scope ends or the program terminates. But then you have little control in memory management.

Using Spaceship *spaceship = new Spaceship(); the memory is dynamically allocated, but then you have to manage the memory. But this is trivial if you write your own object factory and recycler, or if they’re included in a library somewhere. Also, if you have a list type object, you can make a loop with the list and churn out effectively unlimited objects in this way.

Using dynamically allocated objects is far more useful. I have had the privilege of being on a project where 100% of the objects are dynamically allocated. Even the engine itself are objects, allowing for engines within engines, very useful in making mini games and moving them around within the project or to other projects.

A takeaway of all this convo was supposed to be the big concepts behind objects, and how amazing and useful they are, that they can be anything and everything.

Anyway, I worked in C# in the past, I actually liked it a lot cause it was so organized, and had a great interface. Hopefully I’ll get in on a project with that again someday.

1 Like

Will do!

4 Likes

It’s generally polite to identify oneself before seeking personal information from a stranger…

–Hi, I’m Gadget

So, you’re a knob?

Negative. I am a meat popsicle.

Now that that’s out of the way.

Who pray tell are you?

–Gadget Dallas

1 Like

hi gadget
im tutucox , lets be fiends ? :smiley:

*Checks back on this thread*
Is Anderson STILL going on about semantic details, on which she’s wrong about, that have no relevance to OP’s post? Talk about someone who wouldn’t get hired by Merriam-Webster nor Oxford University Press…

Sigh. It’s times like these I fantasize I were ISD…

image

Barstorlode already saw this in a different thread and didn’t take it down. He has a sense of humor :grin:

image

3 Likes

Funny thing that you consider “correcting people about things that are blatantly BS” as “semantic details”.

Says the one who makes personal aggression in a thread.

@ISD_Dorrim_Barstorlode

Aye. It is time to summon him - to close the thread. The thread veered wildly off topic literally more than 100 posts ago.