0

I don't do anything to 'org' char array, but when I see the output. 'org' array is changed from banana to bana. I guess this is caused by memory corruption or something, but I don't know what is wrong exactly. How can I fix the contents of org char array?

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

char org[];
int N;
char clue[1010][110];
int arrest[1010];

void solve(){ // Core function
    char* copy;
    int len_org, len_clue, tmp=0, cnt=0;
    int i, j;

    len_org = strlen(org);
    //printf("len_org = %d\n", len_org);

    for(i=0; i<N; i++){
        //copy = org;
        //memset(copy, 0, strlen(len_org));
        //printf("org = %s\n", org);
        copy = org;
        cnt = 0;
        printf("first copy = %s, org = %s\n", copy, org);
        len_clue = strlen(clue[i]);
        printf("len_clue = %d\n", len_clue);
        tmp = len_org - len_clue;
        for(j=0; j<=tmp; j++){
            printf("clue[i] = %s, copy = %s\n", clue[i], copy);
            if(!strncmp(copy, clue[i], len_clue)){
                cnt++;
                printf("cnt = %d\n", cnt);
            }
            copy++;
            printf("copy = %s, org = %s\n", copy, org);
        }
        printf("final cnt = %d\n\n", cnt);
        arrest[i] = cnt;
    }

    /*for(i=0; i<N; i++)
        printf("%s\n", clue[i]);*/
}

void inputData(){
    int i;
    scanf("%s", org);
    scanf("%d", &N);
    for(i=0; i<N; i++){
        scanf("%s", clue[i]);
    }
}

int main() {
    inputData();
    solve();
    return 0;
}
  • Below is input. banana 2 ana ba And when I run it, org is changed banana to bana. – user3587669 Sep 22 '19 at 09:23
  • 1
    `org` is NULL, but you're using it like it points to something. – Paul Hankin Sep 22 '19 at 09:25
  • banana 2 ana ba – user3587669 Sep 22 '19 at 09:25
  • I put 'banana 2 ana ba ' as input so org is initialized as banana. Should I initialized org before calling it in scanf? – user3587669 Sep 22 '19 at 09:26
  • Thank you for your answer. I changed 'char org[];' to 'char org[] = {0}', and it works well. I know there are many ways to initialize array, but is how I initialize usual way? – user3587669 Sep 22 '19 at 09:35
  • 2
    You are very unlucky that it works well. You still have UB. You now create an array of size 1 and then write "banana 2 ana ba" to that memory location. Only the 'b' fits (since you only allocated memory for one byte), causing all the rest of that string to be written into who knows what and where. – Max Vollmer Sep 22 '19 at 09:38
  • Possible duplicate of [Creating arrays in C](https://stackoverflow.com/questions/21769904/creating-arrays-in-c) – Max Vollmer Sep 22 '19 at 09:40
  • 1
    Thank you for the link. I read it and searched by myself. I think writing like 'char org[] ' should be regarded as const expression. If I want to specific string, I can declare like 'char org[] = "stackoverflow";'. If not I should declare like 'char org[100]' or 'char *org' with malloc. – user3587669 Sep 22 '19 at 10:08
  • @MaxVollmer: `char org[];` at file scope is not equivalent to `char* org;`. If the tentative definition is not completed elsewhere, it is equivalent to `char org[1] = { 0 };`. (And that means it is initialized; so “You don't initialize `org` anywhere” is not true.) – Eric Postpischil Sep 22 '19 at 10:18

1 Answers1

0

Compiling the code with gcc (7.3) identify the problem:

cc a.c
a.c:4:6: warning: array ‘org1’ assumed to have one element
 char org1[];
      ^~~~

In practice (gcc 7.3) The 'org' is placed between 'clue' and 'arrset' (global) variables. As can be seen from 'nm a.out' - the arrest start 4 bytes after org, making the effective size of 'org' to be 4.

Output from: nm a.out | sort

0000000000201040 B N
0000000000201060 B clue
000000000021c25c B org
000000000021c260 B arrest
000000000021d228 B _end
dash-o
  • 13,723
  • 1
  • 10
  • 37