Doubt about array size

3

I have a question regarding array, for example, if I have 2 arrays of size 9 and to print the values of both is normal the input 9 get the value 0 of the next array?, this is another test, I know the maximum is 8?

Code sample:

struct stType {
  type[9];
}select;

strcut vrType {
  type[9];
}variable;

void st_type(int type, int value) {
    int i;
    uint32 type_select = select_type(type);

    ST_TYPE(i, type_select) {
        select->type[i] += value;
    }
}

void vr_type(int type, int value) {
    int i;
    uint32 type_select = variable_type(type);

    VR_TYPE(i, type_select) {
        variable->type[i] += value;
    }
}

int main() {
    st_type(0, 50); // define valor 50 para array select->type[0]
    vr_type(0, 60); // define valor 60 para array variable->type[0]

    for (i = 0; i < 10; i++) 
        printf("[ST Debug %d] Value: %d\n", i, select->type[i]);

        printf("\n");

    for (i = 0; i < 10; i++)
        printf("[VR Debug %d] Value: %d\n", i, variable->type[i]);
}

Output:

[ST Debug 0] Value: 50
[ST Debug 1] Value: 0
[ST Debug 2] Value: 0
[ST Debug 3] Value: 0
[ST Debug 4] Value: 0
[ST Debug 5] Value: 0
[ST Debug 6] Value: 0
[ST Debug 7] Value: 0
[ST Debug 8] Value: 0
[ST Debug 9] Value: 60 // posição 9 puxa o 0 de vr

[VR Debug 0] Value: 60
[VR Debug 1] Value: 0
[VR Debug 2] Value: 0
[VR Debug 3] Value: 0
[VR Debug 4] Value: 0
[VR Debug 5] Value: 0
[VR Debug 6] Value: 0
[VR Debug 7] Value: 0
[VR Debug 8] Value: 0
[VR Debug 9] Value: 0
    
asked by anonymous 15.01.2018 / 17:35

2 answers

0

Yes it is perfectly normal. This happens because of the way the variables are allocated in c, but it also depends on the compiler. Note the following situations:

int a = 5, b = 10;

int main() {
  int *p = &a;
  printf("%d\n", *p);
  p++;
  printf("%d\n", *p);
}

If you compile this code using or even the , the program will produce the following output:

5 - 10

In this case we have two global variables and the memory was allocated in the order in which the variables were declared, one after the other. The same happens in your example, first the memory was allocated the first vector, and soon after the second, making the last element of the first vector next to the first element of the second vector.

But things do not always happen this way. Notice the second example:

int func(int a, int b){
   int *p = &a;
   printf("%d - ", *p);
   p++;
   printf("%d", *p);
}

int main() {
   func(5, 10);
}

This code has an output when compiled with the and another with .

Output VC ++: 5 - 10 GCC output: 5 - 0

As you can see in this example, in c there is no rule that determines the order in which variables are allocated. In your example select->type[9] is equal to 60, but maybe in another compiler or another platform things would not be like this.

    
15.01.2018 / 19:42
2

Actually having a zero is a coincidence, it could have any value there. You are getting a value that is in a memory location outside the area reserved for this array . You know it goes from 0 to 8, so 9 is something that comes next in memory.

C allows access to all application memory without restrictions, it is the problem of the programmer to ensure that he is not accessing something unduly.

    
15.01.2018 / 18:50