#Unwanted change to array value

28 messages · Page 1 of 1 (latest)

warm rootBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

upbeat perch
#

int res[arraySize][arraySize] this does not bring me pleasure

#

but

#

what are x and y

#

what is the size of this array

#

is that x is 1 and y is 3

#

where are you printing this from?

#

well I guess youll never learn

#

rip

warm rootBOT
#

@river delta Has your question been resolved? If so, type !solved :)

upbeat perch
#

try debugging and seeing what the values of i and j are

#

well

#

no you printed them

#

honestly idk this looks like classic undefined behaviour

#

which can be really hard to track down sometimes and idk realllly what all the values are

shrewd apex
#

When compiling with -Og -g3 -Wall -Wextra -Wpedantic -fsanitize=address,undefined -Wpedantic I get the following output:

<source>: In function 'bestVerticalCut':
<source>:164:16: warning: 'bestVal' may be used uninitialized [-Wmaybe-uninitialized]
  164 |             if (aux > bestVal)
      |                ^
<source>:161:9: note: 'bestVal' was declared here
  161 |     int bestVal, aux = 0;
      |         ^~~~~~~

Program returned: 0
Program stdout

Array with the pieces:
1 1 5 
1 3 10 
i: 1, j: 2
res[0][0]: 5
i: 1, j: 2
res[0][0]: 5
res[0][0]: 10
10, 10, 10
Result: 20

Program stderr

/app/example.c:78:11: runtime error: index 3 out of bounds for type 'int [*]'
/app/example.c:94:16: runtime error: index 2 out of bounds for type 'int [*]'
/app/example.c:108:17: runtime error: index 0 out of bounds for type 'int [*]'
/app/example.c:109:17: runtime error: index 0 out of bounds for type 'int [*]'
/app/example.c:95:24: runtime error: index 0 out of bounds for type 'int [*]'
/app/example.c:117:41: runtime error: index 0 out of bounds for type 'int [*]'
/app/example.c:118:17: runtime error: index 1 out of bounds for type 'int [*]'
/app/example.c:120:41: runtime error: index 0 out of bounds for type 'int [*]'
/app/example.c:121:17: runtime error: index 0 out of bounds for type 'int [*]'
/app/example.c:122:41: runtime error: index 0 out of bounds for type 'int [*]'
/app/example.c:123:60: runtime error: index 0 out of bounds for type 'int [*]'
/app/example.c:123:49: runtime error: index 1 out of bounds for type 'int [*]'
/app/example.c:123:38: runtime error: index 0 out of bounds for type 'int [*]'
/app/example.c:79:34: runtime error: index 3 out of bounds for type 'int [*]'
#

So it appears there are a few out of bounds accesses and your bestVal variable is used uninitialized

#

Here's the according godbolt with the line numbers, or you can just compile it for yourself with the options I've named above

shrewd apex
#

No, it says it was "declared".

#

Declaration

int x;

Declaration and Initialization:

int x = 4;
```Assignment:
```c
x = 4;
```Declaration and Assignment:
```c
int x;
x = 4;
#

🐒

shrewd apex
#

I'll try to figure it out after a smoke break.
In the meantime could you just give me an explanation on what your code even does so that I can understand and follow it better?

shrewd apex
#

Okay, so the first false index access is in the line

res[X][Y] = bestValue(X, Y, res, pieces, n);

Here X and Y describe the size of the array, so the maximum index one could use is X - 1 and Y - 1 respectively, meaning you're accessing out of bounds.

#

Also arraySize is uninitialized when you first access it in bestValue, as in your main you merely declared and initialized a local variable, rather than changing the global one.

#

Except the compiler doesn't complain about it because global variables are default-initialized, so in that case arraySize would be 0 everywhere except for inside the main function

#

Haven't looked over all yet but I'm sure that could lead to some problems

warm rootBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity