Array memory allocation of strings

131 views Asked by At

I have written simple string program using array allocation method. I have allocated character array 10 bytes, but when i give input, program is accepting input string of greater than 10 bytes. I am getting segmentation fault only when I give input string of some 21 chars. Why there is no segmentation fault when my input exceed allocated my array limit?

Program:

#include <stdio.h>
#include <string.h>

void main() {

    char str[10];

    printf ("\n Enter the string: ");
    gets (str);
    printf ("\n The value of string=%s",str);
    int str_len;
    str_len = strlen (str);
    printf ("\n Length of String=%d\n",str_len);


}

Output:

Enter the string: n durga prasad

The value of string=n durga prasad
Length of String=14

As you can see, string length is shown as 14, but I have allocated only 10 bytes. How can the length be more that my allocated size?

4

There are 4 answers

0
Natasha Dutta On

As you already know, your input causes buffer overflow, I'm not going to repeat the reason. Instead I would like to answer the particular question ,

  • "Why there is no segmentation fault when my input exceed allocated my array limit?"

The reason that there may or may not be segmentation fault depends on something called undefined behaviour. Once you overrun the allocated memory boundary, you're not supposed to get a segmentation fault for sure. Rather, what you'll be facing is UB (as told earlier). Now, quoting the results of UB,

[...] programs invoking undefined behavior may compile and run, and produce correct results, or undetectably incorrect results, or any other behavior.

So, it is not a must that you'll be getting a segmentation fault immediately on accessing the very next memory. It may run perfectly well unless it reaches some memory which is actually inaccessible for the particular process and then, the SIGSEV signal (11) will be raised.

However, after running into UB, any output from any subsequent statement cannot be validated. So, the output of strlen() is invalid here.

0
Sourav Ghosh On

Please, don't use gets() it suffers from buffer overflow issues which in turn invokes undefined behaviour.

Why there is no segmentation fault when my input exceed allocated my array limit?

Once your input is exceeding the allocated array size (i.e., 9 valid characters + 1 null-terminator), the immediate next access t the array location becomes illegal and invokes UB. The segmentation fault is one of the side effect of UB, it is not a must.

Solution: Use fgets() instead.

0
Gopi On

When you enter more than the number of characters the array can hold then you have undefined behavior. Your array can hold 9 characters followed by a null terminator, so any devaition from this is a UB.

Don't use gets() use fgets() instead

char a[10];
fgets(a,sizeof(a),stdin);

By using fgets() you are avoiding buffer overflow issue and avoiding undefined behavior.

PS: fgets() comes with a newline character

0
Aracthor On

When you declare an array, like char str[10];, your compiler won't always allocate precisely the number of bytes that you required. It often allocate more, usually a multiple of 8 if you are in 64-bits system, for instance it might be 16 in your case.

So even if you asked for 10 bytes, you can manipulate some more. But of course, it's strongly unrecommended because, as you said, it can produce segmentation faults.

And, as said by other answers from Sourav and Gopi, to use fgets instead of gets may also help to produce less undefined behavior.