the first code is iterate thu numbers and it works fine. when it come to strings it generate core dump although both of them using same algorithm. the different is just the type.
And i cant detect the core dump.
thx
this is the first code with integer which works fine
Code:
void smg(int outer[], int inner[]){
int i,j;
int match = 0;
int outSize = 14;
int inSize = 8;
int ta = outer[0];
int tb = inner[0];
int gb = inner[0];
i = j = 0;
while( i < outSize && j < inSize){
while(ta < gb){
ta = outer[i++];
}
while(ta > gb){
gb = inner[j++];
}
tb = gb;
while(tb == gb){
tb = gb;
while(tb == ta){
match++;
tb = inner[j++];
}
ta = outer[i++];
}
gb = tb;
}
printf("> %d %d %d",i,j, match);
}
void nj(int outer[], int inner[]){
int i, j;
int tmp;
int match = 0;
for(i = 0 ; i < 14 ; i++){
tmp = outer[i];
for(j = 0 ; j < 8 ; j++){
if(tmp == inner[j])
match++;
}
}
}
int main(){
int outer[] ={
1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int inner[] = {
2,3,5,5,7,8,12,14
};
smg(outer, inner);
}
this is the second code with strings which generate core dump
Code:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
void smg(char *outer[], char*inner[], int innerSize, int outerSize){
int i,j;
char ta[21];
char tb[21];
char gb[21];
char tmp[21];
int match;
strcpy(ta, outer[0]);
strcpy(tb, inner[0]);
strcpy(gb, inner[0]);
i = j = 0;
match = 0;
while(i < outerSize && j < innerSize){
while(strcmp(ta,gb) < 0){
strcpy(tmp, outer[i++]);
strcpy(ta, tmp);
}
while(strcmp(ta, gb) > 0){
strcpy(tmp, inner[j++]);
strcpy(gb, tmp);
}
strcpy(tb,gb);
while(strcmp(tb,gb) == 0){
strcpy(tb,gb);
while(strcmp(tb,ta) == 0){
match++;
strcpy(tmp, inner[j++]);
strcpy(tb,tmp);
}
strcpy(tmp, outer[i++]);
strcpy(ta, tmp);
}
strcpy(gb,tb);
}
printf("> %d %d %d",i,j, match);
}
int main(){
int match = 0;
int innerSize = 8;
int outerSize = 14;
char* innerRel[innerSize];
char* outer[outerSize];
int i;
for(i = 0 ; i < innerSize ; i++){
innerRel[i] = malloc(sizeof(char) * 21);
}
for(i = 0; i < outerSize ; i++){
outer[i] = malloc(sizeof(char) * 21);
}
strcpy(innerRel[0],"2...................\n");
strcpy(innerRel[1],"3...................\n");
strcpy(innerRel[2],"5...................\n");
strcpy(innerRel[3],"5...................\n");
strcpy(innerRel[4],"7...................\n");
strcpy(innerRel[5],"8...................\n");
strcpy(innerRel[6],"12..................\n");
strcpy(innerRel[7],"14...................\n");
strcpy(outer[0],"1...................\n" );
strcpy(outer[1], "2...................\n");
strcpy(outer[2], "3...................\n");
strcpy(outer[3], "4...................\n");
strcpy(outer[4], "5...................\n");
strcpy(outer[5], "6...................\n");
strcpy(outer[6], "7...................\n");
strcpy(outer[7], "8...................\n");
strcpy(outer[8], "9...................\n");
strcpy(outer[9], "10..................\n");
strcpy(outer[10], "11..................\n");
strcpy(outer[11], "12..................\n");
strcpy(outer[12], "13..................\n");
strcpy(outer[13], "14..................\n");
smg(outer, innerRel, innerSize,outerSize);
}