Home > front end >  Why null chars added in array?
Why null chars added in array?

Time:11-18

I have an array which keeps chars of a string and want to find its suffixes. If i add 7 chars in the array the total number of chars should be 7*8/2 = 28. So the suffix_array limit should be 28. When i tried to create suffix array i noticed that array has null chars ass well. So my output is wrong. What is the reason of this?

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

static char *suffix;

int main()
{
    char s[7] = {'c','o','n','n','e','c','t'};
    suffix = (char*) malloc(sizeof (char)*28);
    int j;
    static int k=0;
    j=k;
    for(int i=0; i<28; i  ) {
        suffix[i] = s[j];
        printf("%c ", suffix[i]);
        if(j<7) {
            j  ;
        }
        else {
            k  ;
            j=k;
        }
            
    }
    
    return 0;
}
Output:

c o n n e c t  o n n e c t  n n e c t  n e c t  e c

CodePudding user response:

You end up with j==7 (if (j<7) { j ; }), which is beyond the end of s.

Replace

if(j<7) {
    j  ;
}
else {
    k  ;
    j=k;
}

with

j  ;
if(j==7) {
    k  ;
    j=k;
}

Tips:

  • Hardcoding 7 and 28 is a bad idea (including the way Vlad did it). It would be best to use
    char s[] = {...}; size_t n = sizeof(s)/sizeof(s[0]); (if array)
    const char *s = "connect"; size_t n = strlen(s); (if string)

  • You don't really use suffix. But if you wanted to build a string in suffix, make sure to allocate one more char for the trailing NUL and to add the trailing NUL!

CodePudding user response:

The for loop invokes undefined behavior even in its first iteration

for(int i=0; i<28; i  ) {
    suffix[i] = s[j];
    printf("%c ", suffix[i]);
    if(j<7) {
        j  ;
    }
    //...

When j is equal to 6 the it is increased

    if(j<7) {
        j  ;
    }

So j becomes equal to 7 and this value is used in the next iteration of the loop in the assignment statement

    suffix[i] = s[j];

As a result there is an access to the memory past the last element of the array s because the valid range of indices for this array is [0, 7 ).

Pay attention to that there is no great sense to declare the variable suffix in the file scope and to make local variables static.

Your program could be simplified at least the following way

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

int main( void ) 
{
    enum { N = 7, M = ( N * ( N   1 ) ) / 2 };

    const char s[N] = { 'c','o','n','n','e','c','t' };
    char *suffix = malloc( sizeof( char ) * M );

    for ( size_t i = 0, k = 0, j = k; i < M; i   ) 
    {
        suffix[i] = s[j  ];
        printf("%c ", suffix[i]);

        if ( j == N ) j =   k;
    }

    free( suffix );

    return 0;
}

The program output is

c o n n e c t o n n e c t n n e c t n e c t e c t c t t

Pay attention to that you should always free dynamically allocated arrays when they are not required any more.

Actually in this program there is no need to allocate the result array dynamically. You could just write

char suffix[M];
  • Related