Here is the full code. it keeps freezing now
typedef struct {
char *name;
} NAME;
setting the array to be null and then later on i expand it depending on how many entries i need to add.
NAME *array = NULL;
int itemCount = 0; // items inserted
int arraySize = 0; // size of array
int arrayCount; //gets size of file.
int found = 0;
int Add(NAME item)
{
if(itemCount == arraySize) {
if (arraySize == 0)
arraySize = 3;
else
arraySize *= 2; // double size of array
//relocate memory
void *tempMemory = realloc(array, (arraySize * sizeof(NAME)));
//error relocating memory
if (!tempMemory)
{
printf("Couldn't relocate the memory \n");
return(-1);
}
array = (NAME*)tempMemory;
}
array[itemCount] = item;
itemCount++;
return itemCount;
}
void printStruct()
{
int i;
for(i = 0; i < arrayCount; i++)
{
printf("%s \n", array[i].name);
}
}
int readFromFile()
{
int checkResult(char[]);
FILE *fp;
fp = fopen("names.txt", "r");
char names[arrayCount];
if (fp == NULL)
{
printf("Cannot access file \n");
}else{
while(!feof(fp))
{
fscanf(fp, "%s", names);
arrayCount++;
checkResult(names);
}
}
fclose(fp);
return 1;
}
int checkResult(char names[]){
NAME tempStruct;
int i;
if(array == NULL)
{
tempStruct.name = malloc((strlen(names) + 1) * sizeof(char));
strcpy(tempStruct.name, names);
tempStruct.count = 1;
}
else
{
for(i = 0; i < arrayCount; i++)
{
if(strncmp(array[i].name, names, arrayCount)==0)
{
printf("MATCH %s", names);
break;
}
}
if(i == arrayCount)
{
tempStruct.name = malloc((strlen(names) + 1) * sizeof(char));
strcpy(tempStruct.name, names);
}
if (Add(tempStruct) == -1)
{
return 1;
}
}
}
In the main i am freeing the memory and calling the other functions
int main(){
void printStruct();
int readFromFile();
readFromFile();
printStruct();
int i;
for (i = 0; i < arrayCount; i++)
{
free(array[i].name);
}
free(array);
return 0;
}
What we're doing here is looping through looking for the first match and breaking the loop when we find it. If we search without finding it anywhere in the array, then we add it...
NAME** array = calloc( MAX_NAMES, sizeof( NAME* ) );
int count = 0;
int checkName(char names[])
{
if(!count)
{
array[0] = calloc( 1, sizeof( NAME ) );
array[0]->name = malloc((strlen(names) + 1) * sizeof(char));
strcpy(array[0]->name, names);
count = 1;
}
else
{
int i;
for(i = 0; i < count; i++)
{
if(strcmp(array[i]->name, names)==0)
{
printf("MATCH %s", names);
break;
}
}
if( i == count && count < MAX_NAMES )
{
array[count] = calloc( 1, sizeof( NAME ) );
array[count]->name = malloc((strlen(names) + 1) * sizeof(char));
strcpy(array[count]->name, names);
count++;
}
}
}
The above code first tests to see that the array is not empty... if it is, then the first element is created for the array and the name is assigned.
Otherwise, it parses the array to see if any entry in the array already matches the name... if it does, it will break the loop and the i == count test will fail, so the name is not added.
If the loop finishes without matching the name, then the i == count test will return true and we add the new name to the end of the array, if there is room in the array.
Related
I created 2 2D arrays containing words. For example:
loadKey[25][30] =
{
{'J','a','v','a','\0'},
{'P','y','t','h','o','n','\0'},
{'C','+','+','\0'},
{'H','T','M','L','\0'},
{'S','Q','L','\0'}
// ... 20 other words here
};
resume[189][30] =
{
{'L','a','l','a','\0'},
{'H','i','h','i','h','i','\0'},
{'C','+','+','\0'},
{'Y','o','Y','o','\0'},
{'S','Q','L','\0'}
// ... 184 other words here
};
I would like to compare each word of a loadKey[] to all words of resume[] to count how many times 25 words of loadKey[] matched of words of resume[]. I tried the strcmp(loadKey[i], resume[j]) but it's pointer of array.
Anyone can help me to solve this problem? Thanks so much!
My program code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PAUSE myPause()
#define KEYWORD 25
#define WORDS 250
#define MAX_LETTER 30
//*********************************************
// FUNCTION PROTOTYPES
void compAndCount(char loadKey[][MAX_LETTER], char resume[][MAX_LETTER]);
void myPause();
void readAndLoadKeyword(char loadKey[][MAX_LETTER]);
// MAIN FUNCTION
int main() {
char loadKey[KEYWORD][MAX_LETTER];
char resume[WORDS][MAX_LETTER];
// load keywords from keywords file into array loadKey[]
readAndLoadKeyword(loadKey);
for (int j = 0; j < KEYWORD; j++)
{
puts(loadKey[j]);
}
puts("\n");
// compare and count the occurrences of keyword in resumes file
compAndCount(loadKey, resume);
}
// FUNTIONS
void compAndCount(char loadKey[][MAX_LETTER], char resume[][MAX_LETTER]) {
FILE* fpr;
fpr = fopen("resumes.txt", "r");
int r = 0, count = 0, num = 0, res = 0;
char temp;
while ((temp = fgetc(fpr)) != EOF) {
if (temp != ' ' && temp != '\n') {
resume[res][r] = temp;
r++;
}
else
{
resume[res][r] = '\0';
r = 0;
res++;
}
}
printf("words in resume file %i\n", res);
for (int j = 0; j < res; j++)
{
puts(resume[j]);
}
puts("\n");
/*
// way 1 to compare and count (WRONG?)
for (int i = 0; i < res; i++) {
if (i < KEYWORD) {
scanf(" %[^\n]", loadKey[i]);
}
scanf(" %[^\n]", resume[i]);
}
for (int k = 0; k < KEYWORD; k++) {
for (int l = 0; l < res; l++) {
if (strcmp(loadKey[k], resume[l]) == 0)
count++;
}
}*/
/*
// way 2 to compare and count (WRONG?)
char key[MAX_LETTER] = {'\0'}, r[MAX_LETTER] = {'\0'};
for (int i = 0; i < KEYWORD; i++) {
strcpy(key, loadKey[i]);
for (int l = 0; l < res; l++) {
strcpy(r, resume[l]);
if (strcmp(key, r) == 0)
count++;
}
}
*/
printf("Resume Rating: %i\n", count);
fclose(fpr);
} // end compAndCount
void myPause() {
puts("\nPress ENTER to continue\n");
exit(0);
}
void readAndLoadKeyword(char loadKey[][MAX_LETTER]) {
FILE* fp;
fp = fopen("keywords.txt", "r");
char ch;
int row = 0, col = 0;
if (fp == NULL) {
puts("Not able to open keyword file!");
PAUSE;
}
// load 25 keywords and ',' into an array line[]
char line[181]; // 180 characters + '\0'
fgets(line, 181, fp);
puts(line);
puts("\n");
// load 25 words in array line[] into array loadKey[]
for (int i = 0; i < 180; i++) {
ch = line[i];
if (ch != ',') {
loadKey[row][col] = ch;
col++;
}
else {
loadKey[row][col] = '\0';
col = 0;
row++;
}
}
fclose(fp);
} // end readAndLoadKeyword
You can use string literals instead of {} of chars
Arrays can have a different number of columns and rows.
char loadKey[25][30] =
{
"Java",
"Python",
// ... more words here
};
char resume[189][30] =
{
"Lala",
"Hihihi",
"C++",
// more
};
//lkr - number of loadKer rows
//lkr - number of loadKer columns
//rr - number of resume rows
//rc - number of resume columns
//rep - count duplicates
size_t count(size_t lkr, size_t lkc, size_t rr, size_t rc, char (*loadKey)[lkc], char (*resume)[rc], int rep)
{
size_t result = 0;
for(size_t lkrow = 0; lkrow < lkr; lkrow++)
{
for(size_t rrow = 0; rrow < rr; lrow++)
{
if(!strcmp(loadKey[lkrow], resume[rrow]))
{
result++;
if(!rep) break;
}
}
}
return result;
}
If the same string can be present in the resume array more than once and oyu want to count duplicates as well rep parameter should be non-zero.
Example usage:
int main(void)
{
size_t cnt = count(25, 30, 189, 30, loadKey, resume, 0);
printf("%zu\n", count);
}
I'm allocating memory for my int *occurrences int *wordCounts and char **uniqueWords pointers and then at the end of the function that allocates the memory, i free them. However, when i compile the program i get an double free or corruption (!prev) aborting error. Is it caused by malloc,free or could it be due to how i initialize them inside the for loop ?
PS: I'm talking about the sortedCount() method, located towards the end
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define MAX_STRING_SIZE 512 /* each line in the file can have up to 512 chars */
void populateWordsArray(int);
void reverse(int);
void first(int);
void middle(int);
void last(int);
int count(int, char*, int);
void sortedCount(int);
void determineUniqueWords(int *,char **, int);
void *malloc_or_end(size_t);
void* malloc_or_end(size_t sz) {
void *pointer;
pointer = malloc(sz);
if(pointer == NULL) {
printf("Out of memory, terminating.\n");
exit(-1);
}
return pointer;
}
/* turn into local */
FILE *file;
char **wordList;
void determineUniqueWords(int *occurrences, char **word, int N) {
int i = 0;
int j = 0;
for(i = 0; i < N; i++) {
if(occurrences[i] < 1) {
continue;
}
for(j = i + 1; j < N; j++) {
if(occurrences[j] == 1 && (strcmp(word[i],word[j])) == 0) {
occurrences[i]++;
occurrences[j] = 0;
}
}
}
}
/**
* Function populateWordsArray: reads N words from
* the given file and populates the wordList array with them.
* Has one argument: int N - the number of words to read.
* */
void populateWordsArray(int N) {
int i = 0;
while(i < N && (fscanf(file,"%s",wordList[i]) == 1)) { /* fscanf returns the number of successfully read items. If it's not 1, the read failed. Same as checking if fscanf reads the eof char. */
i++;
}
}
/**
* Function reverse: prints the words of the
* text file in reverse order.
* */
void reverse(int N) {
int i = 0;
for(i = N-1; i >= 0; i--) {
if(i == 0) {
printf("%s \n",wordList[i]);
} else if(strcmp(wordList[i],"") == 0) { /* improve this in main-> memory allocation */
continue;
}else {
printf("%s ",wordList[i]);
}
}
return;
}
/**
* Function first: Prints the first char of each
* word in the file.
* */
void first(int N) {
char firstChar;
int i = 0;
for(i = 0; i < N; i++) {
firstChar = *wordList[i];
printf("%c",firstChar);
}
printf("\n");
return;
}
/**
* Function middle: Prints the middle char of each word
* from the given file.
* */
void middle(int N) {
int middleIndex = 0;
int i = 0;
char midChar;
for(i = 0; i < N; i++) {
if((strlen(wordList[i]) % 2) == 0) { /* artios */
middleIndex = ((strlen(wordList[i]) / 2) - 1);
midChar = wordList[i][middleIndex];
}
else { /* peritos */
middleIndex = (int) ceil((strlen(wordList[i]) / 2));
midChar = wordList[i][middleIndex];
}
printf("%c",midChar);
}
printf("\n");
return;
}
/**
* Function last: Prints the last char of each
* word from the given file.
* */
void last(int N) {
int i = 0;
char lastChar;
int lastPos;
for(i = 0; i < N; i++) {
lastPos = strlen(wordList[i]) - 1;
lastChar = wordList[i][lastPos];
printf("%c",lastChar);
}
printf("\n");
return;
}
/**
* Function count: Prints the number of times
* that the selected word is found inside the N first words
* of the file.
* */
int count(int N, char *word, int callID) {
int i = 0;
int count = 0;
for(i = 0; i < N; i++) {
if(strcmp(word,wordList[i]) == 0) {
count++;
}
}
if(callID == 0) { /* if callID == 0 (main called count and we want the output) */
printf("%d",count);
printf("\n");
}
return count;
}
void sortedCount(int N) {
int i,j = 0;
int *occurrences;
int *wordCounts;
char **uniqueWords;
/* mem allocation */
uniqueWords = malloc_or_end(N * sizeof(char*)); /* worst case: every word is unique */
wordCounts = malloc_or_end(N * sizeof(int));
occurrences = malloc_or_end(N * sizeof(int));
/* initialize rootWord and occurrences for the "each word is unique and occurs only once" scenario */
for(i = 0; i < N; i++) {
uniqueWords[i] = malloc_or_end(MAX_STRING_SIZE * sizeof(char));
occurrences[i] = 1;
}
determineUniqueWords(occurrences,wordList,N);
/* populate the wordCounts & uniqueWords "arrays" with the appropriate data in order to sort them successfully */
for(i = 0; i < N; i++) {
if(occurrences[i] > 0) {
wordCounts[i] = count(N,wordList[i],1);
uniqueWords[i] = wordList[i];
}
}
for(i = 0; i < N; i++) {
free(uniqueWords[i]);
}
free(uniqueWords);
free(occurrences);
free(wordCounts);
return;
}
int main(int argc,char *argv[]) { /* argv[1] = op argv[2] = name argv[3] = <word> */
int N = -1;
int i = 0;
int spaceNum,nlNum = -1;
file = fopen(argv[2],"r");
if(file == (FILE *) NULL) { /* check if the file opened successfully */
fprintf(stderr,"Cannot open file\n");
}
fscanf(file,"%d",&N); /* get the N number */
wordList = malloc_or_end(N * sizeof(char *)); /* allocate memory for pointers */
for(i = 0; i < N; i++) {
wordList[i] = malloc_or_end(MAX_STRING_SIZE * sizeof(char)); /* allocate memory for strings */
}
populateWordsArray(N);
if(strcmp(argv[1],"-reverse") == 0) {
reverse(N);
} else if(strcmp(argv[1],"-first") == 0) {
first(N);
} else if(strcmp(argv[1],"-middle") == 0) {
middle(N);
} else if(strcmp(argv[1],"-last") == 0) {
last(N);
} else if((strcmp(argv[1],"-count") == 0) && argv[3] != NULL) {
i = count(N,argv[3],0);
} else if((strcmp(argv[1],"-sorted") == 0) && (strcmp(argv[3],"-count") == 0)) {
sortedCount(N);
} else {
/* i only wish i could print something here */
}
/* End of program operations */
for(i = 0; i < N; i++) {
free(wordList[i]);
}
free(wordList);
fclose(file);
return 0;
}
You are overwriting the value of a pointer to heap memory on line 185:
uniqueWords[i] = wordList[i];
This means that when you free it later, you are actually freeing the allocated rows in wordList. Now you have two problems:
When you free the wordList rows on lines 244-246, it will be a double-free
You are losing your reference to the uniqueWords rows.
Use strcpy to assign to a dynamically-allocated string rather than the = operation.
I'm attempting to read in a list of words to be sorted and I start out with a fairly small array (10 elements) and then would like to increase the size of the array by 10 if the current capacity isn't enough. This seems to work with the first realloc, but I get a SIGABRT when trying to call realloc again. I'm sure it is a simple thing I'm not seeing, but I can't seem to figure it out. Here is my program:
int main(int argc, char *argv[]){
char *string = malloc(100);
// Array of pointers starting with 100 elements
char **toSort = malloc(100*sizeof(char *));
if(toSort == NULL) {
exit(1);
}
for(int i = 0; i < 100; i++) {
// Each string can be up to 100 characters long
toSort[i] = malloc(101);
if(toSort[i] == NULL) {
exit(1);
}
}
// Get all lines in the file
int counter = 0;
int max = 10;
char *toAdd;
FILE *txt = fopen("wlist0.txt", "r");
while(fgets ( string, 100, txt ) && counter < max) {;
toAdd = malloc(100);
if(toAdd == NULL) {
exit(1);
}
strcpy(toAdd, string);
toSort[counter] = string;
counter++;
//if the array needs to be enlarged
if(counter == max) {
char **new = realloc(toSort, (max+10) * sizeof(char));
if(new == NULL) {
exit(1);
}
for(int i = max; i < max + 10; i++) {
toSort[i] = malloc(101);
if(toSort[i] == NULL) {
exit(1);
}
}
toSort = new;
max += 10;
}
};
for(int i = 0; i < max; i++) {
char *word = toSort[i];
printf("%s", word);
}
for(int i = 0; i < max; i++) {
free(toSort[i]);
}
free(toSort);
return 0;
};
Like my comments say, my strings have a maximum length of 100 characters. I suppose I could dynamically allocate memory for the strings as well, but I'll worry about that when I have the other realloc working. Any help would be greatly appreciated.
This code assigns values to toSort after it the memory it points to is freed/modified by realloc():
//if the array needs to be enlarged
if(counter == max) {
char **new = realloc(toSort, (max+10) * sizeof(char));
if(new == NULL) {
exit(1);
}
for(int i = max; i < max + 10; i++) {
toSort[i] = malloc(101); <--- toSort is invalid here
if(toSort[i] == NULL) {
exit(1);
}
}
toSort = new;
max += 10;
}
This will work better:
//if the array needs to be enlarged
if(counter == max) {
char **new = realloc(toSort, (max+10) * sizeof( *new )); <-- fixed here, too
if(new == NULL) {
exit(1);
}
toSort = new;
for(int i = max; i < max + 10; i++) {
toSort[i] = malloc(101);
if(toSort[i] == NULL) {
exit(1);
}
}
max += 10;
}
There may be other errors in your code. I haven't fully examined it.
Hey I'm trying to figure out why the following code gets invalid write of size error from Valgrind at the line: array[i-1] = I;
I really don't now why my allocate_array function doesn't work. I tried so many things.
There are couple errors more but I just wanted to check first why this line is false or why my array isn't allocated.
Hope you can help me to figure out my error.
#include<stdio.h>
#include<stdlib.h>
//Programm to check Gaussian function
int read_number_from_stdin(int* value) {
printf("Number for the Gaussian Function: ");
int return_value = scanf("%d", value);
if (return_value == 0) {
while (fgetc(stdin) != '\n')
;
}
if (return_value == EOF) {
return_value = 0;
}
return return_value;
}
int read_number_from_string(char* string, int* value) {
printf("Reading input...\n");
int return_value = sscanf(string, "%d", value);
if (return_value == 0 || return_value == EOF) {
printf("\t... Error your input is not a Number!\n");
return_value = 0;
} else {
printf("\t... Number %d read and saved.\n", *value);
}
return return_value;
}
int* allocate_array(int* size) //allocating memory for the array
{
int* result = (int*) malloc(sizeof(int) * (*size));
return result;
}
void initialize_array(int array[], int size) {
for (int i = 0; i < size; i++) {
array[i] = i+1;
}
}
int compute_sum_and_place_in_first_elem(int array[], int* size) {
int sum_array = 0;
for (int i = 0; i < *size; i++) {
sum_array += array[i];
}
return sum_array;
}
void free_memory(int array[], int* N) {
free(array);
free(N);
}
int main(int argc, char* argv[]) {
int* N = malloc(sizeof(int));
if (argc == 1) {
while (read_number_from_stdin(N) != 1)
;
} else if (argc == 2) {
if (read_number_from_string(argv[1], N) == 0) {
printf("Error: No valid number!\n", argv[1]);
return -1;
}
} else {
printf("No valid number!\n");
return -1;
}
int* array = allocate_array(N); //allocate via function
initialize_array(array, *N); //initialize the array up to n
int result = compute_sum_and_place_in_first_elem(array, N);
int result_gauss = ((*N + 1) * (*N) / 2);
if (result == result_gauss) {
printf("Gauss was right your calculations match with his function");
} else {
printf(
"\nGauss was not right!\n"
"The summ of %d is %d and therefore not equal to(%d+1)*%d/2\n\n",
*N, result, *N, *N);
}
//free memory
free_memory(array, N);
}
As I can see, for the initialize_array() function, for the for loop, very first iteration, i is 0, and you're executing
array[i-1] = i;
which translates to
array [-1] = ....
which is illegal.
You can fix that using the default C-array property of 0-based indexing scheme. Something like
for(int i = 0; i < size; ++i)
{
array[i] = i;
}
I'm leaking a very small amount of memory in a wf function I'm writing, and I can't seem to exactly locate it. I'm using a hash table to hold freqency, but my testing makes it look like it's not in the hashing functions. Here are my functions to open/read files and free the data at the end. I'm sure it's a simple bug, but I've been looking at this code too long to be able to see it.
typedef struct {
int noInFiles, numFiles, numToPrint;
char** fileNames;
FILE** files;
Hash hash;
} Freq;
void handleInput(int argc, char* argv[], Freq* freq) {
int num = 0, i, j = 0;
char* crap;
printf("memcurrent pre fileName alloc: %d\n\n", memCurrent());
freq->fileNames = calloc(argc - 1, sizeof(char**));
printf("memcurrent post filename alloc: %d\n\n", memCurrent());
freq->numToPrint = 10;
if(argc < 2) {
freq->noInFiles = 1;
freq->numFiles = 0;
return;
}
for(i = 1; i < argc; i++) {
if(argv[i][0] == '-') {
if(argv[i][1] == 'n') {
num = strtol(argv[i] + 2, &crap, 10);
freq->numToPrint = num;
}
else {
fprintf(stderr, "Usage: wf [-nX] [file...]\n");
exit(EXIT_FAILURE);
}
}
else {
freq->fileNames[j] = calloc(strlen(argv[i]) + 1 ,sizeof(char));
strcpy(freq->fileNames[j], argv[i]);
j++;
freq->numFiles++;
}
}
}
void openFiles(Freq* freq) {
int i;
char* str;
printf("Memcurrent pre open: %d\n",memCurrent());
freq->files = calloc(freq->numFiles,sizeof(FILE**));
printf("Memcurrent post open: %d\n",memCurrent());
for(i = 0; i < freq-> numFiles; i++) {
freq->files[i] = fopen(freq->fileNames[i],"r");
if(freq->files[i] == NULL) {
str = malloc(strlen(freq->fileNames[i]) + 5);
sprintf(str,"wf: %s",freq->fileNames[i]);
perror(str);
free(str);
exit(EXIT_FAILURE);
}
}
}
void freeFreq(int argc, Freq* freq) {
int i;
for(i = 0; i < argc - 1 ; i++) {
free(freq->fileNames[i]);
}
free(freq->fileNames);
free(freq->files);
}
Hash functions
typedef struct {
Entry* arr;
int size, numValid;
} Hash;
void initHash(Hash* hash) {
hash->arr = calloc(BASESIZE, sizeof(Entry));
TOTALALLOC =+ (BASESIZE * sizeof(Entry));
hash->size = BASESIZE;
hash->numValid = 0;
}
void freeTable(Hash* hash) {
int i;
for(i = 0; i < hash->numValid - 1; i++) {
if(hash->arr[i].correct == 1 && hash->arr[i].valid == 1) {
wordsFreed++;
free(hash->arr[i].word);
}
}
free(hash->arr);
}
This might be it:
for(i = 0; i < hash->numValid - 1; i++) {
If you have numValid set to 0 at start, I'm presuming that you increment it each time you add an entry to the array.
So if numValid is 1, then you will never loop, which means you will leak one of your entries. It seems that each time you free the hash, you will leak one entry, unless the hash has no entries at all.
This may not fix your problem, but ..
There is a mismatch between the number of allocations and deallocations for freq->fileNames.
Allocation:
else {
freq->fileNames[j] = calloc(strlen(argv[i]) + 1 ,sizeof(char));
strcpy(freq->fileNames[j], argv[i]);
j++;
freq->numFiles++;
}
Deallocation:
for(i = 0; i < argc - 1 ; i++) {
free(freq->fileNames[i]);
}
Assuming the logic for allocation is correct, the logic for deallocation needs to be:
for(i = 0; i < freq->numFiles ; i++) {
free(freq->fileNames[i]);
}
PS
I noticed that you have calls to fopen but no calls to fclose in your posted code.