Examples of code and personal projects

The other day I tried using the GNU Emacs installer and Windows Defender blocked it right away.

2 Likes

Iโ€™ve heard the defender is known to deflect safe programs at times. Are you sure the file of the GNU Emacs was from a safe site?

1 Like

That checks out, because the only way to not have that happen is to pay Microsoft 200$ per year (for a signing certificate). And I canโ€™t imagine the free software foundation would do that.

3 Likes

More reasons to not use the products made by the company of 4 colorsโ€ฆ

I recently started reviewing my old L4D2 script and noticed something strange within a function provided by another modder.

::CommandSeparatedBySpaces <- function(inputString)
{
	local outputString = "";
	local startCopyIndex = 0;
	local index = 0;
	local maxIndex = inputString.len();

	while(index < maxIndex)
	{
		local bShouldIncrement = true;

		if(index + 4 < maxIndex && inputString[index] == 60 && inputString[index + 1] == 115 && inputString[index + 2] == 112 && inputString[index + 3] == 99 && inputString[index + 4] == 62)		// If it hits the "<spc>" sequence, then...
		{
			local tempString = inputString.slice(startCopyIndex, index);		// Assign the first half of the string (everything before '<spc>') to tempString
			tempString += " ";												// Add a space to tempString
			outputString += tempString;										// Add tempString to outputString
			index += 5;														// Skip the "<spc>" sequence
			startCopyIndex = index;											// Assign the beginning index of the second half of the string (everything after '<spc>') to startCopyIndex
			bShouldIncrement = false;
		}
		if(bShouldIncrement)		// If it doesn't hit or has already hit the '<spc>' sequence, then keep incrementing
		{
			index += 1;
		}
	}

	if(startCopyIndex == 0 || startCopyIndex != index)								// If there is no "<spc>" sequence within inputString, then...
	{
		local tempString = inputString.slice(startCopyIndex, inputString.len());		// Reassign the entire string or the remainder to tempString
		outputString += tempString;													// Add tempString to outputString
	}

	return outputString
}

The purpose of this function is to replace the sequence <spc> with a space character within inputString. My dead brain was initially confused as to how both halves of inputString would get inside outputString within the while loop and then I realized something. After the first if statement has been passed, I think the code goes back to the beginning of the while loop and resets bShouldIncrement to true, which continues the incrementation until index reaches maxIndex. Then comes the last if statement. Because the incrementation continued, startCopyIndex is no longer equal to index, the second part of inputString get inserted inside outputString. However, thereโ€™s still something weird. Why did I set bShouldIncrement to false within the first if statement?

1 Like

I think that first if statement takes segment since last detected space, or from beggining and adds " " to it and should-increment boolean is kinda like else statement (if first if statement is correct, then the other wonโ€™t work and vice versa). So from each detected space, to as of now empty output string,
"new " + "string " + "is " + "added " + "in " + "this " + "format, " and after loop is done, the last statement adds last remaining piece of sentence, I think.

1 Like

Um also BJS, by โ€œmy oldL4D2 scriptโ€ you mean you โ€œmoddedโ€ that game in a way with some โ€œaidโ€ from other modders?
(Also I wonder if we will get a โ€œcoding challengesโ€ thread one day with questions to examples like the one you provided here)

@hhyyrylainen In C++, whenever referencing is needed, which is faster? OOP or pointers?

1 Like

I think you need to take a step back to the higher level and ask about what you are trying to actually accomplish. That would probably have a much clearer answer than when to use a reference or a pointer. And actually under the hood references are exactly the same as pointers, thatโ€™s right a reference is stored as exact same bytes as a pointer would be and it generates the same machine code. So they are exactly as fast as the other. Thus the actual difference is when humans are writing code. Hereโ€™s some of the things to consider when determining if a function parameter should be a value, pointer, reference, or a smart pointer:

  • The key difference is that a reference cannot be nullptr meaning that the function can specify in its signature if it expects something to be able to be null or not. If taking in a pointer you need to always verify that it is not null and if it is null what that means for the function. With a reference you always know that the reference is valid so you save on a ton of null checks. Same as the C# nullable reference types feature which is extremely nice.
  • If you cannot answer the question that what should happen if something is null, thatโ€™s a clear sign that it should be a reference so that it is never allowed to be null.
  • Is the value so small that it likely can be passed in CPU registers? For example it being just the size of a pointer or even a few pointers as certain ABIs can place small structs / classes into CPU registers when calling a function. Then you should use a direct value and neither references or pointers.
  • If the value is small it should be passed as a copy unless the method needs to be able to modify the original, in which case it needs to be either a pointer or a reference
  • If the function expects that it will modify the given object a lot, it can be clearer to just take in a value then, as without that the function body itself would just need to make a copy of the parameter, and I think it is better communication to show in the signature that a copy will be made.
  • According to modern C++ style, a pointer should never own the memory it points to. So if the function being called needs to be able to refer to the memory later, you must pass in the data wrapped in a smart pointer, that way the called function can take a copy of the pointer safely for future reference.
  • References can never be uninitialized, so in certain code structures it is not really possible to use a reference without writing a part of the logic as a helper function for no other reason than to be able to initialize a reference immediately. In those cases I would use a pointer. And thereโ€™s even certain code patterns where you just need the flexibility of a pointer being able to be null.

Also advanced features like __restrict apply the same way to references as pointers.

So I hope you can now see that technically they work almost the same, but the human factors of code readability and understandability determine which should be used in a given context.

As a closing thought I think you are focusing on the wrong thing if you are thinking about the performance of OOP versus pointers, as that doesnโ€™t really make sense. At least in my programs the vast majority of my pointers point to object instances thus they are part of OOP (so pointers are part of OOP and not a separate concept). You should gain practical C++ experience before thinking about the performance too much. If you follow a simple rule of taking class instances as const & in function parameters you basically get a right enough solution for 99% of cases. So if you just remember that you should be able to start practising C++ and then over multiple years you can pick up the exceptions to that rule.

2 Likes

I wasnโ€™t too keen on explaining my project because Iโ€™m mentally and hardware-ly unable to work on it at the moment, but I was thinking about making a simulator that can simulate physics and infer the laws of physics to simulate chemistry and finally infer chemistry to simulate molecular biology (mostly simulating proteins in action).

2 Likes

Would one be able to alter the physicsโ€™ principles in such a simulator? Since for some reason this reminds me of a game Iโ€™ve seen before that seems to have been built on the same idea.

That idea sounds general enough that you only really need to pick / research the programming language that would work well for such a project / one you want to learn and seems viable for the project. And also researching what support libraries youโ€™ll want to use. You absolutely do not need to know the difference between references and pointers in C++ to get past that initial stage of the project.

2 Likes