Please use LINUX to debug this C code. this code runs smoothly with codeblocks b
ID: 3886994 • Letter: P
Question
Please use LINUX to debug this C code. this code runs smoothly with codeblocks but somehow, it crashed when I tried to run with GCC Linux. Thank you in advance
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// A constant for the line size.
const int LINE_SIZE = 128;
//This is the simple data structure.
struct Data
{
char* text;
};
struct Data* CreateDataStruct(char* t)
{
struct Data* r = (struct Data*)malloc(sizeof(struct Data));
r->text = t;
return r;
}
int compare(struct Data* info, struct Data* info2)
{
//checks to see if both pointers are valid.
if(!info || !info2) return 0;
return strcmp(info->text, info2->text);
}
//Destroys the data structure.
void DestroyData(struct Data* info)
{
free(info);
}
//A standard binary search algorithm.
int binarySearch(struct Data** arr, char* txt, int count, int* iter)
{
int low = 0, high = count - 1;
while(low <= high)
{
*iter = *iter + 1;
int cur = (low+high)/2;
int s = strcmp(arr[cur]->text,txt);
if(!s)
{
return cur;
}
else if(s < 0)
{
low = cur + 1;
}
else if(s > 0)
{
high = cur - 1;
}
}
return -1;
}
// This is the "Stable" Selection Sort
void insertionSort(struct Data** arr, int count)
{
struct Data* lowest, *temp;
int i,j;
for(i = 1; i < count; i++)
{
j = i-1;
lowest = arr[i];
while (j >= 0 && compare(arr[j], lowest) > 0)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = lowest;
}
}
// Print out the array.
void printArray(struct Data** arr, char* text, int count)
{
int s;
printf(" i | index[i] | word ");
printf("--------|---------------|-------- ");
for(s = 0; s < count; s++)
{
struct Data* info = arr[s];
if(info)
printf(" %i | %i | %s ", s, (int)(info->text) - (int)text, info->text);
}
}
// Go through the data structure array and free the memory.
void DestroyDataArray(struct Data** arr, int count)
{
int s;
for(s = 0; s < count; s++)
{
DestroyData(arr[s]);
}
free(arr);
}
int main()
{
// Create buffers to store the input and perform data manipulation in.
char *text, *text2, *text3;
text = malloc(LINE_SIZE * 64);
text2 = malloc(LINE_SIZE);
text3 = malloc(LINE_SIZE);
//Read in the first line.
scanf("%[^ ]", text);
//Create Values for Parsing Help
int offset = 0, val = 1;
int count = 1;
//Cleans the input.
while(text[offset] != 0)
{
//Tries to predict how much memory is needed for allocation.
if(text[offset] == ' ' /*|| text[offset] == ' '*/ || text[offset] == ' ') count++;
//Makes all characters lower case.
if(text[offset] >= 'A' && text[offset] <= 'Z')
{
text[offset] += 'a' - 'A';
}
offset++;
}
//sets the offset to zero for further parsing.
offset = 0;
struct Data** arr = (struct Data**)malloc(sizeof(struct Data*)*count);
char *cur = text + offset;
count = 0;
while(text[offset] != 0)
{
if(text[offset] == ' ' /*|| text[offset] == ' ' */|| text[offset] == ' ' || text[offset] == ' ')
{
//sets it to zero to make it easy for my functions to utilize the pointer.
text[offset] = 0;
//this block is used to skip empty strings, and carry on.
if(!cur)
{
offset++;
continue;
}
//makes the new data structure.
arr[count++] = CreateDataStruct(cur);
//trims the string (if necessary)
if(text[offset - 1] == '.' || text[offset -1] == ',' || text[offset-1] == '!' || text[offset-1] == ':') text[offset-1] = 0;
//sets the current pointer to null.
cur = 0;
}
else if(cur == 0)
{
//sets the pointer to the current string.
cur = text + offset;
}
offset++;
}
if(cur)
{
//trims the final character, if necessary.
if(text[offset - 1] == '.' || text[offset -1] == ',' || text[offset-1] == '!') text[offset-1] = 0;
arr[count++] = CreateDataStruct(cur);
}
//Print the collection of words.
printf("Words and indexes (the printed words are 'cleaned'): ");
printArray(arr, text, count);
//Print the sorted collection of words.
printf(" The sorted data (the printed words are 'cleaned'): ");
insertionSort(arr, count);
printArray(arr, text, count);
printf(" Binary search. ---- Enter words to search for. Stop with -1. ");
scanf(" %[^ ]", text2);
val = 1;
int *iter = malloc(sizeof(int));
while(val)
{
offset = 0;
memcpy(text3, text2, 110);
while(text2[offset] != 0)
{
if(text2[offset] >= 'A' && text2[offset] <= 'Z')
{
text2[offset] += 'a' - 'A';
}
if(text2[offset] == ' ')
{
text3[offset] = text2[offset] = 0;
}
offset++;
}
val = strcmp("-1", text3);
if(!val) continue;
if(text2[offset-1] == '.' || text2[offset-1] ==',' || text2[offset-1] == '!' || text2[offset-1] == ' ' || text2[offset-1] == ':')
{
if(text2[offset-1] == ' ')
{
text3[offset-1] = 0;
}
text2[offset-1] = 0;
}
val = binarySearch(arr, text2, count, iter);
printf("(original: %s) cleaned: %s - ",text3, text2);
if(val == -1)
{
printf("not found ");
}
else
{
printf("found ");
}
printf("(%i iterations) ", *iter);
*iter = 0;
//Query for new input.
scanf(" %[^ ]", text2);
}
//free our memory
DestroyDataArray(arr, count);
free(iter);
free(text);
free(text2);
free(text3);
return 0;
}
Explanation / Answer
//Please find the updated code below run it in case of any issue you can post your comments
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// A constant for the line size.
const int LINE_SIZE = 128;
//This is the simple data structure.
struct Data
{
char* text;
};
struct Data* CreateDataStruct(char* t)
{
struct Data* r = (struct Data*)malloc(sizeof(struct Data));
r->text = t;
return r;
}
int compare(struct Data* info, struct Data* info2)
{
//checks to see if both pointers are valid.
if(!info || !info2) return 0;
return strcmp(info->text, info2->text);
}
//Destroys the data structure.
void DestroyData(struct Data* info)
{
free(info);
}
//A standard binary search algorithm.
int binarySearch(struct Data** arr, char* txt, int count, int* iter)
{
int low = 0, high = count - 1;
while(low <= high)
{
*iter = *iter + 1;
int cur = (low+high)/2;
int s = strcmp(arr[cur]->text,txt);
if(!s)
{
return cur;
}
else if(s < 0)
{
low = cur + 1;
}
else if(s > 0)
{
high = cur - 1;
}
}
return -1;
}
// This is the "Stable" Selection Sort
void insertionSort(struct Data** arr, int count)
{
struct Data* lowest, *temp;
int i,j;
for(i = 1; i < count; i++)
{
j = i-1;
lowest = arr[i];
while (j >= 0 && compare(arr[j], lowest) > 0)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = lowest;
}
}
// Print out the array.
void printArray(struct Data** arr, char* text, int count)
{
int s;
printf(" i | index[i] | word ");
printf("--------|---------------|-------- ");
for(s = 0; s < count; s++)
{
struct Data* info = arr[s];
if(info)
printf(" %i | %i | %s ", s, (int)(info->text) - (int)text, info->text);
}
}
// Go through the data structure array and free the memory.
void DestroyDataArray(struct Data** arr, int count)
{
int s;
for(s = 0; s < count; s++)
{
DestroyData(arr[s]);
}
free(arr);
}
int main()
{
// Create buffers to store the input and perform data manipulation in.
char *text, *text2, *text3;
text = (char *)malloc(LINE_SIZE * 64);
text2 = (char *)malloc(LINE_SIZE);
text3 = (char *)malloc(LINE_SIZE);
//Read in the first line.
scanf("%[^ ]", text);
//Create Values for Parsing Help
int offset = 0, val = 1;
int count = 1;
//Cleans the input.
while(text[offset] != 0)
{
//Tries to predict how much memory is needed for allocation.
if(text[offset] == ' ' /*|| text[offset] == ' '*/ || text[offset] == ' ') count++;
//Makes all characters lower case.
if(text[offset] >= 'A' && text[offset] <= 'Z')
{
text[offset] += 'a' - 'A';
}
offset++;
}
//sets the offset to zero for further parsing.
offset = 0;
struct Data** arr = (struct Data**)malloc(sizeof(struct Data*)*count);
char *cur = text + offset;
count = 0;
while(text[offset] != 0)
{
if(text[offset] == ' ' /*|| text[offset] == ' ' */|| text[offset] == ' ' || text[offset] == ' ')
{
//sets it to zero to make it easy for my functions to utilize the pointer.
text[offset] = 0;
//this block is used to skip empty strings, and carry on.
if(!cur)
{
offset++;
continue;
}
//makes the new data structure.
arr[count++] = CreateDataStruct(cur);
//trims the string (if necessary)
if(text[offset - 1] == '.' || text[offset -1] == ',' || text[offset-1] == '!' || text[offset-1] == ':') text[offset-1] = 0;
//sets the current pointer to null.
cur = 0;
}
else if(cur == 0)
{
//sets the pointer to the current string.
cur = text + offset;
}
offset++;
}
if(cur)
{
//trims the final character, if necessary.
if(text[offset - 1] == '.' || text[offset -1] == ',' || text[offset-1] == '!') text[offset-1] = 0;
arr[count++] = CreateDataStruct(cur);
}
//Print the collection of words.
printf("Words and indexes (the printed words are 'cleaned'): ");
printArray(arr, text, count);
//Print the sorted collection of words.
printf(" The sorted data (the printed words are 'cleaned'): ");
insertionSort(arr, count);
printArray(arr, text, count);
printf(" Binary search. ---- Enter words to search for. Stop with -1. ");
scanf(" %[^ ]", text2);
val = 1;
int *iter = (int *)malloc(sizeof(int));
while(val)
{
offset = 0;
memcpy(text3, text2, 110);
while(text2[offset] != 0)
{
if(text2[offset] >= 'A' && text2[offset] <= 'Z')
{
text2[offset] += 'a' - 'A';
}
if(text2[offset] == ' ')
{
text3[offset] = text2[offset] = 0;
}
offset++;
}
val = strcmp("-1", text3);
if(!val) continue;
if(text2[offset-1] == '.' || text2[offset-1] ==',' || text2[offset-1] == '!' || text2[offset-1] == ' ' || text2[offset-1] == ':')
{
if(text2[offset-1] == ' ')
{
text3[offset-1] = 0;
}
text2[offset-1] = 0;
}
val = binarySearch(arr, text2, count, iter);
printf("(original: %s) cleaned: %s - ",text3, text2);
if(val == -1)
{
printf("not found ");
}
else
{
printf("found ");
}
printf("(%i iterations) ", *iter);
*iter = 0;
//Query for new input.
scanf(" %[^ ]", text2);
}
//free our memory
DestroyDataArray(arr, count);
free(iter);
free(text);
free(text2);
free(text3);
return 0;
}