New answers tagged

3

There have been languages that, as you suggest, provide only a single datatype and then have operations where the expected encoding of the data is provided as part of the operation, rather than being determined by the type of the variable as it is in most modern languages. The best known of these was probably B, the predecessor of C (in fact, C can be ...


1

Your first snippet of code effectively assigns k1 to i and then checks i against the exit case. This is done every iteration and is why it is slower than the second snippet EDIT A possible workaround in pseudocode to have efficient iteration and still not relying on hard coding the value of i var i = k1; for i to 100000 //iterate next i


8

Let's forget for the moment that processors have specific hardware for manipulating byte sequences of a particular size. Let's forget for the moment that processors have specific hardware for operating on specific interpretations of byte sequences (floating-point registers, SEE registers, etc). What good does this abstraction do from a user perspective? ...


1

You are correct that numeric types are just patterns of bits. But processors are optimized for certain operations on certain patterns of bits. For example x86 processors have registers of sizes 8, 16 and 32 bit, arithmetic operators corresponding to these sizes, and memory is addressed in chunks of 8 bits, known as bytes. There is no support for 19-bit ...


8

So why did all those programming languages decided to provide multiple built-in data types Because the built-in types like int, float, byte and char are used in almost all use cases – and it turns out that standards are a convenience for everyone. If everyone used their own variant, writing code that uses libraries with different types, exchanging data ...


11

Because processors have operations specifically for ints, and specifically for floats. The compiler has to know what operation to target. And I mean, even if you had adding for a series of bits, adding 0110 and 0001 have very different meanings if 0001 is treated as a float rather than an int - not to mention actual operations required to calculate it. And ...


2

Have I not created a variable MyList of size double? With double[] MyList;, you've merely created a variable that can later refer to an array of doubles. You haven't created any doubles. It's essentially a pointer. Why is there an option of declaring an array, instead of creating it at once? Sometimes you won't want to create it at once. For example, ...


1

To answer your actual question: Most higher level (or scripting) languages out there have data structures that can hold different types of data (like numbers, strings and even functions) in the same structure ... So how are these types of data structures found in higher level languages implemented in the lower level language they are written in? You ...


7

The closest thing I managed to write was a class in C++ which allocated a certain amount of memory and, when its size was filled, allocated a longer block of memory, copied the elements in that block and freed the first one, but that doesn't seem efficient at all and anyway only allows to add one specific type of item and only in a stack-like way (where you ...


0

Mixed types are accommodated by storing pointers to objects. The closest thing I managed to write was a class in C++ which allocated a certain amount of memory and, when its size was filled, allocated a longer block of memory, copied the elements in that block and freed the first one, but that doesn't seem efficient at all It's quite efficient in ...


8

I guess the kind of "lists" you had in mind are not the "linked lists" mentioned by Robert Harvey, but the kind of arrays which are called list in Python, List or ArrayList in C#, ArrayList in Java, or std::vector in C++. Another popular term for this kind of data structure is "dynamic array". These are indeed implemented internally exactly the way you ...


4

Arrays, like all data structures, trade one kind of efficiency for another. The efficiency that an array specializes in is that of rapidly looking up an element by its index. It does that, not by searching through the array for the correct element, but by performing a mathematical calculation. If an array has elements of size 8 bytes, and you want to look ...


2

A real world example might be waist sizes. Men's shorts might come in sizes from 28 inches to 50 in 1-inch increments. Instead of everywhere in your program saying, sizes[i + 27] you create an "array" with indicies 28-50 (and thus size=21) and not have to worry about offsets. BoundedIntArray sizes = new BoundedIntArray(28, 50); void addToCount(int size) {...


2

Use a Map Map<Integer, ClientThread> threads = new HashMap<>(); Add thread to map ClientThread thread = new ClientThread(socket); threads.put(thread.serverUniqueId, thread); Remove a thread threads.remove(id); Get a thread by id theads.get(id); Get threads by multiple ids int[] ids = {1,2,3,4}; for (Map.Entry<Integer, ClientThread&...


0

If you want to find something in an unordered array, iterating over it is the fastest thing you can do. You should store the Sockets in a HashMap. You can put in the id and get the corresponding Socket in constant time.



Top 50 recent answers are included