W
weidongtom
Hi,
I've written the code that follows, and I use the function add_word(),
it seems to work fine
*before* increase_arrays() is called that uses realloc() to allocate
more memory to words. But *after* calling increase_arrays(), I
received segmentation fault. I tried to step it through gdb, and I
found out that after calling increase_arrays(), words[0]'s original
value is modified, and if I tried to access it, I get <address 0x11
out of bound>. It's there something wrong with the way I used
realloc()? Please help me out, my limited knowledge of C can carry as
far as here, and I don't think I can figure this out by myself. (I've
checked the C Faq, but still can't figure it out, now I see why people
says that memory management is a pain.). Thanks in advance.
#define INC_SIZE 3 /* number of elements the arrays should
increase.*/
static int array_size; /* size of arrays words, word_counts */
static int total_words; /* total number of words found */
static char **words; /* an array to of words occured */
static int *word_counts; /* number of the corresponding word in arrays
words appeared */
/* Increases the storage for the arrays words, word_counts
* and adjust related variables
*/
static void increase_arrays(){
int i;
int new_size = array_size + INC_SIZE;
char **tmp = realloc(*words, sizeof(char*)*(new_size+1));
int *tmp1 = realloc(word_counts, sizeof(int)*new_size);
if(!(*tmp) || ! tmp1){
fprintf(stderr, "Fatal Error: failed to allocate memory.\n");
exit(1);
}
memset(tmp1 + array_size, 0, sizeof(int)*INC_SIZE);
*words = *tmp;
for(i = array_size; i < new_size; i++)
words = NULL;
word_counts = tmp1;
array_size = new_size;
}
/* return the index of word in the array words on success,
* -1 if word does not exist in the array.
*/
static int word_index(const char *word){
int index;
for(index = 0; index < total_words; index++){
if(!strcmp(words[index], word)) /* matches*/
return index;
}
/* word does not exist */
return -1;
}
/* add word to the array words if it does not exist,
* else words is not modified.
* adjust related the word_counts, and other related
* variables.
*/
static void add_word(const char *word){
int index;
int length;
index = word_index(word);
if(index == -1){
/* word does not exist*/
if(total_words >= array_size)
increase_arrays();
length = strlen(word)+1;
words[total_words]=calloc(length, sizeof(char*));
if(words[total_words]){
strcpy(words[total_words], word);
word_counts[total_words] = 1;
total_words++;
}
else{
fprintf(stderr, "Fatal Error:failed to allocate memory.\n");
exit(1);
}
}
else{
/* word exists */
word_counts[index]++;
}
}
I've written the code that follows, and I use the function add_word(),
it seems to work fine
*before* increase_arrays() is called that uses realloc() to allocate
more memory to words. But *after* calling increase_arrays(), I
received segmentation fault. I tried to step it through gdb, and I
found out that after calling increase_arrays(), words[0]'s original
value is modified, and if I tried to access it, I get <address 0x11
out of bound>. It's there something wrong with the way I used
realloc()? Please help me out, my limited knowledge of C can carry as
far as here, and I don't think I can figure this out by myself. (I've
checked the C Faq, but still can't figure it out, now I see why people
says that memory management is a pain.). Thanks in advance.
#define INC_SIZE 3 /* number of elements the arrays should
increase.*/
static int array_size; /* size of arrays words, word_counts */
static int total_words; /* total number of words found */
static char **words; /* an array to of words occured */
static int *word_counts; /* number of the corresponding word in arrays
words appeared */
/* Increases the storage for the arrays words, word_counts
* and adjust related variables
*/
static void increase_arrays(){
int i;
int new_size = array_size + INC_SIZE;
char **tmp = realloc(*words, sizeof(char*)*(new_size+1));
int *tmp1 = realloc(word_counts, sizeof(int)*new_size);
if(!(*tmp) || ! tmp1){
fprintf(stderr, "Fatal Error: failed to allocate memory.\n");
exit(1);
}
memset(tmp1 + array_size, 0, sizeof(int)*INC_SIZE);
*words = *tmp;
for(i = array_size; i < new_size; i++)
words = NULL;
word_counts = tmp1;
array_size = new_size;
}
/* return the index of word in the array words on success,
* -1 if word does not exist in the array.
*/
static int word_index(const char *word){
int index;
for(index = 0; index < total_words; index++){
if(!strcmp(words[index], word)) /* matches*/
return index;
}
/* word does not exist */
return -1;
}
/* add word to the array words if it does not exist,
* else words is not modified.
* adjust related the word_counts, and other related
* variables.
*/
static void add_word(const char *word){
int index;
int length;
index = word_index(word);
if(index == -1){
/* word does not exist*/
if(total_words >= array_size)
increase_arrays();
length = strlen(word)+1;
words[total_words]=calloc(length, sizeof(char*));
if(words[total_words]){
strcpy(words[total_words], word);
word_counts[total_words] = 1;
total_words++;
}
else{
fprintf(stderr, "Fatal Error:failed to allocate memory.\n");
exit(1);
}
}
else{
/* word exists */
word_counts[index]++;
}
}