Question on odd use of brace initializers for arrays in C++

I am currently going through and linting a lot of old code at a company that has been around a while. We have recently been having issues with uninitialized C++ members having garbage data, which is one reason for the housekeeping. Unfortunately with all the updates to C++ over the years there are a few… Read More Question on odd use of brace initializers for arrays in C++

Why does this code only work properly if the method is in a while loop condition? (Java)

I know the System.in.read() method doesn’t have much use, but I’m confused as to why it has to be in the while statement for the following code to work. I’m trying to take character input from the method and add it to a string. When the enter key is pressed, it stops taking input and… Read More Why does this code only work properly if the method is in a while loop condition? (Java)

Why the indexing affects a complete array instead of one item ? (c#)

I have faced with a strange behavior in c#. I created a int[][] array like this: int[][] m_dist = Enumerable.Repeat(Enumerable.Repeat(-1, m.Length).ToArray(), m.Length).ToArray(); Array looks like this: -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1, Later on when I tried to modify a single array element I was supraised because not only one item was changed but a… Read More Why the indexing affects a complete array instead of one item ? (c#)

How to declare a variable but not assign it?

I want to declare acceptable_set and use it. But an empty vector is assigned to it. So the compiler warns. How to declare a variable and do not assign it? let mut acceptable_set: Vec<String> = Vec::new(); if opt.acceptable_set.is_none() { acceptable_set = crate::builtin_words::ACCEPTABLE .to_vec() .iter_mut() .map(|x| x.to_string()) .collect(); } else { acceptable_set = get_acceptable_set(opt) } warning:… Read More How to declare a variable but not assign it?

How to properly initialize a struct in Rust, with good enough encapsulations?

How to properly initialize a struct in Rust, with good enough encapsulations? Or more naively: how to leverage object/instance methods in the initialization/constructing process of structs? For example, as the initialization block in Kotlin: private class BinaryIndexedTree(nums: IntArray) { private val nNums = nums.size private val fenwick = IntArray(nNums + 1) { 0 } //… Read More How to properly initialize a struct in Rust, with good enough encapsulations?