Learning C trying to figure out this malloc stuff via functions - c

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;
}

Related

Create and fill a dynamic array within function C

I need to scan n numbers and then fill an array with the following scanned n numbers. I have to use both n and array inputs in a function as well as to use dynamic memory.
void input(int* buff, int* n);
int main() {
int* data = NULL;
int n;
input(data, &n);
for (int i = 0; i < n; ++i) {
printf("%d ", data[i]);
}
return 0;
}
void input(int* buff, int *n) {
if (scanf("%d", n) == 1 && getchar() == '\n') {
if (*n <= 0) {
error = 1;
}
}
if (error == 0) {
buff = (int*)calloc(*n, sizeof(int));
if (buff == NULL) {
error = 1;
}
}
if (error == 0) {
int count = 0;
int p;
for (int i = 0; i < *n; i++) {
if (1 == scanf("%d", &p)) {
if (count < *n) {
*(buff + count) = p;
count++;
}
}
}
for (int i = 0; i < *n; i++) {
printf("%d, ", *(buff + i));
}
}
I didn't insert all the logic of my program in the first for since it doesn't really mater.
The last for prints proper elements however I have a warning about an empty pointer.
When I return to the main and trying to print I've got an error about read access prohibition.
Where did I do wrong?
data in main isn't changed when you change buff inside input. In other words - data will still be NULL when the input function returns.
It's just like the n... For n you correctly pass a pointer to n and you need to do exactly the same for data.
Like:
void input(int** buff, int *n) {
^^
// Notice the two *, i.e. pointer-to-pointer
....
*buff = calloc(....
....
and call it like
input(&data, &n);
If you only need to allocate one single dynamic array in your function you can just return a pointer to it:
int *input(int *buf, int *n);
...
data = input(data, &n);
...
int *input(int* buff, int *n) {
...
return buff;
}

Print all combination of a string using recursion in C

I have an assignment to write code that printing all combinations of N char. For example, if the input is 3, the expected output must be "aaa aab aac aba ... ccc". But my code looping over and over again. Here's my code.
#include <stdio.h>
#ifndef MAX
#define MAX 5
#endif
void comb(char kar[], int size, int index) {
// string = aaa
// lim = 'd'
char lim = 'a' + size;
while (index != -1) {
if (kar[size-1] != lim) { // != c
for (int i = 0; i < size; i++) {
printf("%s ", kar);
kar[size-1]+=1;
}
return comb(kar, size, index);
} else {
while (kar[index-1] == lim && index != -1) {
kar[index-1]='a';
index--;
}
kar[index-1] += 1;
return comb(kar, size, size);
}
}
}
int main(int argc, char const *argv[]) {
int n;
char kar[MAX];
printf("Input N char : ");
scanf(" %d", &n);
for (int j = 0; j < n; j++) {
kar[j] = 'a';
}
comb(kar, n, n);
return 0;
}
I'm a little bit confused and I have no idea where is the mistake. Thank you.
The problem has been solved. I changed some elements in the comb() and added the pow() function to define the recursion limit.
int comb(char kar[], int size, int index, int limit) {
char lim = 97 + size;
int limit_value = pow(size,size);
if(limit == limit_value){
return 1;
} else {
if (index < size-1) {
printf("%s ", kar);
kar[size-1]+=1;
return comb(kar, size, index+1, limit+1);
} else {
int cek = index;
printf("%s ", kar);
while (kar[cek] == lim-1 ) {
kar[cek]=97;
cek-=1;
}
kar[cek] += 1;
return comb(kar, size, 0, limit+1);
}
}
}

double free or corruption when using char**

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.

Char array comparison

I am trying to compare two arrays. Both of these arrays contain 15 characters. I want to see if they have the same element or not.
characters being read in to the array
Array1: ATGGAATTCTCGCTC
Array2: TTGGAATTCTAGCTC
These are both in the form of arrays and are being passed in as arrayt and arraymt. n_size and m_size are the size of each array.(this is because the arrays can be any length 1-15)
int mutations(char arrayt[],char arraymt[],int n_size,int m_size)
{
int i=0,mutation=0;
do{
for(i=0;i<n_size;i++)
{
if (arrayt[i]==arraymt[i])
{
mutation=1;
}
}
}while(n_size == m_size);
return mutation;
}
This is the code I have so far for finding the differences in the two arrays. Problem is I don't know of a way that I can return if there is a difference for each of the elements. 1 is for a change and 0 is no change.
If anything is unclear let me know and I will try and clarify.
The code you required may be:
int* mutations(char arrayt[],char arraymt[],int n_size,int m_size)
{
int i, size;
int* mutation;
// size = min(m_size,n_size)
if n_size>m_size
size=m_size;
else
size=n_size;
endif
mutation=*malloc( sizeof(int) * size); // allocate memory for return value
for(i=0;i<size;i++)
{
if (arrayt[i]==arraymt[i])
{
mutation[i]=1;
} else {
mutation[i]=0;
}
}
return mutation;
}
Let's use 15 bits of an unsigned to record the results.
unsigned mutations(char arrayt[], char arraymt[], int n_size, int m_size) {
int sizemin = min(n_size, m_size);
int sizemax = max(n_size, m_size);
unsigned dest = 0;
int i;
for (i = 0; i < sizemin; i++) {
if (arrayt[i] == arraymt[i])
dest |= 1 << i;
}
for (; i < sizemax; i++) {
dest |= 1 << i; // compares against empty values are set to 1
}
return dest;
}
void foo(void) {
const char *Array1 = "ATGGAATTCTCGCTC";
const char *Array2 = "TTGGAATTCTAGCTC";
unsigned result = mutations(Array1, Array2, strlen(Array1), strlen(Array1));
// LSBit contains the result of the compare of Array1[0] vs. Array1[1]
printf("%X\n", result);
}
void mutations(char arrayt[],char arraymt[], int *result, int max_index)
{
int i = 0;
for (i = 0; i < max_index; i++)
{
if (arrayt[i] == arraymt[i])
{
result[i] = 0;
continue;
}
else
{
result[i] = 1;
}
}
}
int main()
{
int i = 0;
int max_index;
int *res;
char arrayt[] = "aabbccduu";
char arraymt[] = "aabcccd";
max_index = strlen(arrayt) <= strlen(arraymt) ? strlen(arrayt) : strlen(arraymt);
res = calloc (1, sizeof(int) * max_index);
if (res != NULL)
{
mutations(arrayt, arraymt, res, max_index);
while (i < max_index)
{
printf("res[i] = %d\n", res[i]);
i++;
}
free(res);
}
return 0;
}

using strcmp with an array

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.

Resources