UKBouldering.com

Coding question (C) (Read 3688 times)

tomtom

Offline
  • *****
  • forum hero
  • Posts: 20288
  • Karma: +642/-11
Coding question (C)
June 21, 2016, 03:51:17 pm
Hi There - this has me stumped... I'd post it on a coding forum, but they'd probably all laugh at me (HAHAHAHAHAHA) and I'm not a member of any... So, I've a student migrating someone elses C code over to C# - and theres these lines, that work in C, but not in C# and for the life of me I cant figure out why they should work in C anyway!

What I don't get is that according to this, its looking for the position -1 in the 1d array X... which you can't do right? Unless this is some sneaky C method to do something different... baffled...

   z = 0;
   X[z-1] = X[z];  Y[z-1] = Y[z]-1;

   while ((Y[z] < 2*Ymax -1) && (z < MaxBeachLength-1))
   {
            //do some shit...
         }

Dexter

Offline
  • ***
  • obsessive maniac
  • Posts: 484
  • Karma: +19/-0
#1 Re: Coding question (C)
June 21, 2016, 04:08:08 pm
negative indices are supported in C  (see here http://stackoverflow.com/questions/3473675/are-negative-array-indexes-allowed-in-c)

I think that they are not in C# (see here http://stackoverflow.com/questions/15730346/array-with-negative-indexes)

hope this helps

sidewinder

Offline
  • ***
  • obsessive maniac
  • Posts: 351
  • Karma: +11/-0
#2 Re: Coding question (C)
June 21, 2016, 04:33:48 pm
Been a while but pretty certain you are not allow negative indices, unless as described in the stackoverflow you are using it to reference/move negatively a position/pointer
Code: [Select]
This is only valid if arr is a pointer that points to the second element in an array or a later element. Otherwise, it is not valid, because you would be accessing memory outside the bounds of the array.

tomtom

Offline
  • *****
  • forum hero
  • Posts: 20288
  • Karma: +642/-11
#3 Re: Coding question (C)
June 21, 2016, 04:36:39 pm
Been a while but pretty certain you are not allow negative indices, unless as described in the stackoverflow you are using it to reference/move negatively a position/pointer
Code: [Select]
This is only valid if arr is a pointer that points to the second element in an array or a later element. Otherwise, it is not valid, because you would be accessing memory outside the bounds of the array.

That's what I thought... But afaik the code is written (thankfully :) ) in pointer less C! Bizarre. It smacks of an old school C trick I don't know of!

filz

Offline
  • **
  • addict
  • Posts: 152
  • Karma: +6/-0
#4 Re: Coding question (C)
June 21, 2016, 04:49:05 pm
It's been a lot since I programmed in C, but from what I remeber doing X[-1] = X[0] you are overwriting a part of the memory not allocated to X and this should result in undefined behavior.

Are X[-1] and Y[-1] ever accessed later in the code? How is z increased / decreased?
If not the C program probably works for this reason and you can safely remove the line.

But then being 20 or so years I don't write in C it may be "an old school C trick" as you say  :)

sidewinder

Offline
  • ***
  • obsessive maniac
  • Posts: 351
  • Karma: +11/-0
#5 Re: Coding question (C)
June 21, 2016, 04:57:16 pm
That's what I thought... But afaik the code is written (thankfully :) ) in pointer less C! Bizarre. It smacks of an old school C trick I don't know of!
I would think it is more likely a typo error, that for some reason is being got away with in c, perhaps due to the compiler doing something, that value never getting used, or as filz says because it is never then used.
From my experience c is pretty good at allowing you to try and run code which contains terrible errors you have (unintentionally) introduced.
You can get it to step through it in a debug mode and or output the address of the x or y -1 elements to see where it is pointing.

tomtom

Offline
  • *****
  • forum hero
  • Posts: 20288
  • Karma: +642/-11
#6 Re: Coding question (C)
June 21, 2016, 05:02:19 pm
It doesn't appear to be a typo. If you remove the line or modify it so z starts as 1 then it compiles and runs - but doesn't run correctly. So it's important for the functioning of the code.

tomtom

Offline
  • *****
  • forum hero
  • Posts: 20288
  • Karma: +642/-11
#7 Re: Coding question (C)
June 21, 2016, 05:03:07 pm
When z is 1 or more it makes sense as its transferring one array value to the next etc.. But not when it's zero.

jwi

Offline
  • *****
  • forum hero
  • Posts: 4241
  • Karma: +331/-1
    • On Steep Ground
#8 Re: Coding question (C)
June 21, 2016, 05:42:48 pm
I haven't written anything in C during this millennium, so more useless help coming up... where are X and Y declared? How? are they part of a structure?

tomtom

Offline
  • *****
  • forum hero
  • Posts: 20288
  • Karma: +642/-11
#9 Re: Coding question (C)
June 21, 2016, 07:42:26 pm
I haven't written anything in C during this millennium, so more useless help coming up... where are X and Y declared? How? are they part of a structure?

Hmm, I'll check, but I'm pretty sure they're big standard 1d integer arrays...

highrepute

Offline
  • *****
  • forum hero
  • Posts: 1292
  • Karma: +109/-0
  • Blah
#10 Re: Coding question (C)
June 22, 2016, 09:12:33 am
The explanation that comes to me first, although without seeing other parts of the code it's hard to know for sure, is that z is probably defined as an unsigned int.

If it were 8 bit (uint_8) then is can take a value from 0 to 255. If z = 0 then the result of z - 1 will be 255. By this method you can rotate the elements of the array, each time you call that line of code the array will shift the values around (sort of).

In c# (I've done very little) variables aren't defined as such (i think) and the compiler just goes for what seems right. In this case it'll be a signed integer so can take negative values.

You can't negatively address arrays in C or C#. Should be easily solvable. Present more details if you need more help.

tomtom

Offline
  • *****
  • forum hero
  • Posts: 20288
  • Karma: +642/-11
#11 Re: Coding question (C)
June 22, 2016, 10:08:52 am
Good call highrepute. I'll chase that up. As i mentioned earlier I thought it might be some 'go to the far end of the array' trick.. Which would make sense in terms of what the code is supposed to do...

 

SimplePortal 2.3.7 © 2008-2024, SimplePortal