2

I'm learning C and trying to build an dynamic array. I found a great tutorial on this but I don't get it all the way. The code I have now is

typedef struct{
    int size;
    int capacity;
    char *data;
}Brry;

void brry_init(Brry *brry){
    brry->size = 0;  
    brry->capacity = 2; 
    brry->data = (char *)calloc(brry->capacity, sizeof(char));
}

void brry_insert(Brry *brry, char value){
      brry->data[brry->size++] = value; //so do check here if I have enough memory, but checking something out
}

int main(void){
    Brry brry;
    brry_init(&brry);

    for (int i = 0; i < 3; i++) {
        brry_insert(&brry, 'a');
    }

    printf("%c\n", brry.data[2]);
    return 0;
}

In my main function I add 3 element to the array, but it only allocated for 2. But when I print it it works just fine? I expected some strange value to be printed. Why is this or am I doing something wrong?

Haagenti
  • 7,974
  • 5
  • 38
  • 52

3 Answers3

3

You are writing into a buffer you didn't allocate enough memory for. That it works is not guaranteed.

What you're trying now is to read from some junk value in memory, who knows, which sometimes leads to a segmentation fault and other times you are lucky and get some junk value, and it doesn't segfault.

Writing into junk memory will invoke undefined behavior, so better watch it. If you do get errors it will almost always be a segfault, short for segmentation fault. Read up on it here.

The technical for what you're doing by reading past the bounds of the array is called derefencing a pointer. You might also want to read more about that here.

Community
  • 1
  • 1
edwardmp
  • 6,339
  • 5
  • 50
  • 77
  • Wrong way 'round. If you're lucky you'll get a seg fault and know you jacked up instead of an "it works" for now, and have someone down the road have some sort of fatal error that could potentially cause the company millions of dollars of damages due to lost wages, repair/redesign costs, etc. – ciphermagi Dec 07 '13 at 23:55
  • @JonahNelson Yes, sometimes you'll be lucky to know. But just let's hope OP learns something from it. – edwardmp Dec 07 '13 at 23:56
1

Yes, you are indeed writing to the third element of a two element array. This means your program will exhibit undefined behavior and you have no guarantee of what is going to happen. In your case you got lucky and the program "worked", but you might not always be so lucky.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
1

Trying to read/write past the end of the array results in undefined behaviour. Exactly what happens depends on several factors which you cannot predict or control. Sometimes, it will seem to read and/or write successfully without complaining. Other times, it may fail horribly and effectively crash your program.

The critical thing is that you should never try to use or rely on undefined behaviour. It's unfortunately a common rookie mistake to think that it will always work because one test happened to succeed. That's definitely not the case, and is a recipe for disaster sooner or later.

Peter Bloomfield
  • 5,578
  • 26
  • 37