0

From what I heard, I should allocate the memory like this in line 14: array[i]=malloc(sizeof(char)*(strlen(buffer)+1)); I haven't added the 1 and still the code works perfect. I cant make it crash of return anything than 0. So, is the +1 needed or not? And if it is, what are the consequences going to be since my program runs smoothly?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 5
#define SIZE 512

int main(){
    char *array[N];
    char buffer[SIZE];
    int i,j;
    for(i=0;i<N;i++){
        printf("Give word no.%d",i+1);
        gets(buffer);
        array[i]=malloc(sizeof(char)*strlen(buffer));
        printf("%d",strlen(buffer));
        if(!array[i]){
            printf("Program will now exit.");
            exit(0);
        };
        strcpy(array[i],buffer);
    }

Tried it with both +1 and without. Same results although I've seen in tutorials that it is needed.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 8
    Ignoring the other problems in your code... yes, it is needed. The fact that it works without it is just luck, sooner or later it will break. – pmacfarlane Dec 09 '22 at 10:59
  • 4
    Unrelated: You're using ```gets```. It is obsolete and dangerous, so much that it has been removed from C. – Harith Dec 09 '22 at 11:00
  • 4
    `gets(buffer);` --> [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/2410359) – chux - Reinstate Monica Dec 09 '22 at 11:00
  • 2
    If your compiler isn't complaining about that stray semicolon after the closing brace, you should turn compiler warnings up. – Harith Dec 09 '22 at 11:01
  • 2
    kostas791, `printf("%d",strlen(buffer));` --> `printf("%zu",strlen(buffer));` to avoid another problem. Use matching specifiers. – chux - Reinstate Monica Dec 09 '22 at 11:02
  • 1
    The `strcpy` call will add the sting null-terminator even if you haven't allocated space for it. That will be writing out of bounds of your allocated memory, and lead to *undefined behavior*. – Some programmer dude Dec 09 '22 at 11:03
  • As ```sizeof``` is guaranteed to be 1, ```sizeof(char)``` is just noise. – Harith Dec 09 '22 at 11:04
  • 2
    When you do **not** use `+ 1` the memory allocated to `array[i]` will be too small. In other word, `strcpy` will write outside the allocated memory. Writing out of bounds is "undefined behavior". That means "anything may happen". One **especially nasty** thing included in "anything" is that it may produce the expected result... so you may think your code is correct but it isn't :-( – Support Ukraine Dec 09 '22 at 11:05

1 Answers1

3

The +1 is needed to allocate room for the null terminator. If you don't, then you might write to the allocated array out of bounds, which is undefined behavior. What is undefined behavior and how does it work? Meaning it might seem to work just fine, to break mysteriously later on.

sizeof(char) is by definition always 1 though, so that part is not needed.

Recommendation: Use array[i] = malloc(strlen(buffer) + 1);

As a side note, whoever (book/teacher) told you to use gets should be retired and not be used as a source of learning C programming. Why is the gets function so dangerous that it should not be used?

Lundin
  • 195,001
  • 40
  • 254
  • 396