1

I am re-studying C right now and I have always been told that in C we have to define which maximum size would have an array so it can have enough space reserved in the memory. I mean this:

#include <stdlib.h> //Para función malloc

int main() {

   int *enteros = malloc( sizeof(int) * 10 ); //An array of 10 integers
   int i;
   for(i = 0; i < 100; i++) {
      enteros[i] = i*10;
      printf( "-> enteros[%d] = %d\n", i, i*10 );
   }
}

However, the following code works well (here I am not telling the max size, I just allocate enough memory for the pointer to int), and I just can't find an answer to what it's happening here.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *enteros = malloc( sizeof(int) );
    int i;
    for(i = 0; i < 2000; i++) {
       *(enteros++) = i*10;
       printf( "-> enteros[%d] = %d\n", i, i*10 );
       enteros++;
   }
}

It seems that I just can manage dynamic arrays perfectly well, without having to reallocate memory for the array and being forced to introduce a starting size, however I feel that I'm missing something.

2
  • 1
    there is no guarantee that the second code even compiles to what you expect let alone work as you expect Commented Nov 23, 2019 at 16:45
  • 3
    The second program is overflowing the allocation and is corrupting memory. This is the sort of thing that results in emergency security patches. Commented Nov 23, 2019 at 16:51

2 Answers 2

1

Yes, you can compile and run the code, but you may write data to addresses reserved by other variables.

But this is just a source of the problem. The actual problem is that you are getting unexpected program behavior and run time errors extremely hard to find if you have large volume of code and with allocation issue inside. And it can be dramatic if happens on production. This cannot happen in most of other languages, but in C a developer is responsible.

Try this code as an example:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *enteros = malloc( sizeof(int) );
    int *a = malloc( sizeof(int));
    int i;
    *a = 4; // value setup
    for(i = 0; i < 2000; i++) {
       *(enteros++) = i*10;
       printf( "-> enteros[%d] = %d\n", i, i*10 );
       enteros++;
   }
   printf( "a = %d\n", *a); // value changed!!!
}

Then comment out to double check:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *enteros = malloc( sizeof(int) );
    int *a = malloc( sizeof(int));
    int i;
    *a = 4;
    for(i = 0; i < 2000; i++) {
       // *(enteros++) = i*10;
       // printf( "-> enteros[%d] = %d\n", i, i*10 );
       enteros++;
   }
   printf( "a = %d\n", *a); // value did not change
}

https://repl.it/repls/SoggyOverdueFactors

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the example, now I understand that if I don't allocate the size of the array then it just writes in memory addresses that may have been filled in by other variables all along the program. Can you add this clarification to the answer please?
Okay I edited as requested. Well, slightly different, because I think the main point is not data replacement for some variable, but a danger of that and "hard to find" error state.
0

One have to specify size of array while dynamic allocation using malloc/new.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.