Question about array size

Asked

Viewed 108 times

2

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

Code example:

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]);
}

Exit:

[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

2 answers

2

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

C allows access to the entire application memory without restrictions, it is the programmer’s problem to ensure that it is not accessing something improperly.

  • I see, but it hurts something in the code ?

  • Very, you’re accessing an area that shouldn’t, what you have there is considered garbage, and you’ll consume it. Worse, moving there will alter something that is part of another object damaging it.

0


Yes is perfectly normal. This is due to how variables are allocated in c, but also depends on the compiler. Observe 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 the or even the , the program will produce the following output:

5 - 10

In this case we have two global variables and 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 is allocated to the first vector, and then to the second, making the last element of the first vector to be right next to the first element of the second vector.

But things don’t always happen that way. Note 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 and another with .

Exit 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.

  • I understand, I was thinking it was a problem in my code so I came up with this question, your example was very enlightening, thank you.

  • @Carol is actually a problem in her code and this answer is misleading, no matter how good an intention. It is not how C allocates variables and it does not depend on the compiler. Note that the examples do not even address your problem. She is even correct in the problem she is talking about, but it is not the same as yours. The answer gives to understand that your code is right. That’s what I said, you’re accessing an area of memory that wasn’t assigned to that variable, if you have positions from 0 to 8, trying to access 9 is possible, but semantically wrong producing a certainly wrong result...

  • ...although it works by coincidence https://i.stack.Imgur.com/g0Toi.png

  • @Maniero, at no point have I ever said that the gigo is right, I just wanted to show you why they worked that way. The first example is yes the same case as hers. The second was just to show how unpredictable (and consequently dangerous) c can become when it comes to this subject. And you’re right when you say it’s wrong to try to access memory that wasn’t assigned to a variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.