Off Topic > Off Topic
Programming Megathread
<< < (104/241) > >>
McJob:
urrrrrrrrrrrrrrrgh.

Last night, while doing programming exercises in C++, I discovered "Array Decay". Because arrays are just a fancy version of a pointer, when you pass them in as an argument to a function/method, they lose their array properties and are just treated like standard pointers. Normally this works fine; pointer notation allows you to use [] on it anyway. The issue is using Range-Based For loops for iteration over an array.

The Range-Based For loop looks like this:

for (tempType tempVariable : arrayName) {}

It's the equivalent of the foreach loop in C#. Each iteration of the loop, the tempVariable is equal to the current element of the array, and at the end of the iteration tempVariable is saved back to the element in arrayName. When your array and the range-based for loop are in the same scope, this works perfectly, however...

Because an array parameter is treated like a standard pointer, it loses certain necessary properties (such as array::begin) that the Range-Based For loop requires to work. Because there's no workaround for this and arrays don't have "length" property like in C#, you end up having to either manually keep track of the length of the array or hardcode a maximum number in and then break from the loop once it reaches some kind of terminator character.

Thoroughly frustrating.

EDIT: Should also mention; sizeof() correctly returns the length of your whole array in bytes when used in the same scope as the array. sizeof() only returns the length of the first element in an array in bytes when it is used on an array parameter. I was hoping to use simple maths (sizeof(arrayName) / sizeof(int)) to get the size of the array for this programming exercise, but that didn't work for the reasons I've said up above.
Glass Joe:
So I made a program that takes an input file (test.d) stores it in a 2D vector for easy modification, and then puts the result inside an output file (test.dlvl) as a way of making a "tileset"

http://pastebin.com/XnE4bUz3

I wanna know how I can make it a bit more efficient, because as you can tell from the pastebin it isn't very concise.

edit: commented the code, and tweaked a few things.
ZSNO:

--- Quote from: McJob on March 04, 2016, 04:30:09 PM ---urrrrrrrrrrrrrrrgh.

Last night, while doing programming exercises in C++, I discovered "Array Decay". Because arrays are just a fancy version of a pointer, when you pass them in as an argument to a function/method, they lose their array properties and are just treated like standard pointers. Normally this works fine; pointer notation allows you to use [] on it anyway. The issue is using Range-Based For loops for iteration over an array.

The Range-Based For loop looks like this:

for (tempType tempVariable : arrayName) {}

It's the equivalent of the foreach loop in C#. Each iteration of the loop, the tempVariable is equal to the current element of the array, and at the end of the iteration tempVariable is saved back to the element in arrayName. When your array and the range-based for loop are in the same scope, this works perfectly, however...

Because an array parameter is treated like a standard pointer, it loses certain necessary properties (such as array::begin) that the Range-Based For loop requires to work. Because there's no workaround for this and arrays don't have "length" property like in C#, you end up having to either manually keep track of the length of the array or hardcode a maximum number in and then break from the loop once it reaches some kind of terminator character.

Thoroughly frustrating.

EDIT: Should also mention; sizeof() correctly returns the length of your whole array in bytes when used in the same scope as the array. sizeof() only returns the length of the first element in an array in bytes when it is used on an array parameter. I was hoping to use simple maths (sizeof(arrayName) / sizeof(int)) to get the size of the array for this programming exercise, but that didn't work for the reasons I've said up above.

--- End quote ---
If you don't want to keep track of the array properties yourself, try using something like a vector or deque.
Foxscotch:

--- Quote from: Ono-Sendai on March 04, 2016, 02:28:26 PM ---x

--- End quote ---
thanks for all the advice and stuff. I'll probably take another stab at it eventually, but currently I think I'm still way too frustrated to try again right now lol
ZSNO:

--- Quote from: Glass Joe on March 05, 2016, 11:38:13 AM ---So I made a program that takes an input file (test.d) stores it in a 2D vector for easy modification, and then puts the result inside an output file (test.dlvl) as a way of making a "tileset"

http://pastebin.com/XnE4bUz3

I wanna know how I can make it a bit more efficient, because as you can tell from the pastebin it isn't very concise.

edit: commented the code, and tweaked a few things.

--- End quote ---
For one, putting braces after a case statement is useless.

Anyways, http://pastebin.com/6EkTmsiJ
Basically you can have just an array of your tileset symbols, and access those directly instead of having a switch
As long as the inf.get() works correctly, the assignment statement is now one line
You can use the post increment operator (var++) to do the increment after the assignment, saving a line
You could figure out a better way to do the while(true) loop, because while(true)s are ugly
Also removed some #includes you had because they weren't used in this code.
Navigation
Message Index
Next page
Previous page

Go to full version