I have a structure called House and it includes a char array for neighborhood. I am trying to read a database of houses and create an array for the houses with the same neighborhood. Reading part is working fine but while I'm creating a new array for a neighborhood my function only enters the loop for 23 times and finds first 23 houses with the same neighborhood and gives this error.
pro(4740,0x10d06f5c0) malloc: * error for object 0x7ff724d00030: pointer being realloc'd was not allocated
pro(4740,0x10d06f5c0) malloc: * set a breakpoint in malloc_error_break to debug
typedef struct house{
int id;
int lotarea;
char street[5];
int saleprice;
char neighborhood[10];
int yearbuilt;
int overallqual;
int overallcond;
char kitchenqual[3];
} House;
House* get_neighborhoods(House house, House* array) {
printf("Get neighborhoods of house with id %d\n", house.id);
House* temp = array;
int counter = 1;
House* newarray = malloc(sizeof(House));
if (newarray == NULL) {
printf("Malloc error...");
}
while (temp != NULL) {
if (!strcmp(temp->neighborhood, house.neighborhood)) {
if (counter > 1) {
realloc(newarray, sizeof(House) * counter);
}
copy_house(newarray + counter - 1, temp);
print_house(newarray[counter - 1]);
counter++;
}
temp++;
}
return newarray;
}
You must assign the return value from realloc. You have
realloc(newarray, sizeof(House) * counter);
The second time you call it (in the loop), the pointer you previously passed to it has already been freed, but you discarded the new value.
newarray = realloc(newarray, sizeof(House) * counter);
Ideally you would assign the return value to a temporary variable, so you can check if the realloc succeeded.
Related
I am trying to get into C and as a training example, I decided to write a simple dynamically sized list. But I am facing a weird problem, where the code only works up to an initial list size of 4. Starting at List size 5, I get an error.
typedef struct {
int* data;
int alloc_size;
int length;
} List;
List create(int init_size) {
List out;
out.data = (int*) malloc(init_size * sizeof(int));
out.alloc_size = init_size;
out.length = 0;
return out;
}
void list_push(List* list, int elem) {
if (list->length == list->alloc_size) {
list->data = (int*) realloc(list->data, 2 * list->alloc_size);
list->alloc_size *= 2;
}
*(list->data + list->length) = elem;
list->length++;
}
int list_pop(List* list) {
list->length--;
return *(list->data + list->length);
}
int main() {
List list = create(5);
for (int i = 0; i < 100; i++) {
list_push(&list, i);
}
while (list.length > 0) {
printf("%d\n", list_pop(&list));
}
return 0;
}
Up to create(4), everything works as expected. But if the list is created with create(5) (i.e. an initial size of 5), I get the following error: malloc: Incorrect checksum for freed object 0x7f7ff5c01778: probably modified after being freed. Corrupt value: 0x700000006. I can't really wrap my head around what would cause this to only work up to specific initial sizes, as the list size is dynamically reallocated anyway.
There are a couple of problems with this line
list->data = (int*) realloc(list->data, 2 * list->alloc_size);
The most evident is that 2 * list->alloc_size should be multiplied by the size in bytes of each element (sizeof(int) or sizeof(*(list->data)) in this case).
The most subtle is that the return value of realloc (and of the previous malloc) is not checked, but unconditionally assigned to list->data. The problem is that, on failure, it returns NULL, while the passed pointer (list->data) is not invalidated and should be freed to avoid leaks.
change to reallocation statement
list->data = (int*) realloc(list->data,sizeof(int) * 2 * list->alloc_size);
Second time you are trying to re-allocate lesser bytes than you already allocated, that's the reason for this
I'm doing a school assignment, I've I've run into 2 problems. I have to simulate stacks, with arrays.
My current code is as follows:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int capacity;
int * array;
int size;
} stack_tt;
int pop(stack_tt * stack_p);
void push(stack_tt * stack_p, int value);
int top(stack_tt * stack_p);
stack_tt * newStack(void);
int empty(stack_tt * stack_p);
int main() {
stack_tt * myStack = newStack();
push(myStack, 123);
push(myStack, 99);
push(myStack, 4444);
while (!empty(myStack)) {
int value;
value = pop(myStack);
printf("popped: %d\n", value);
}
return 0; }
stack_tt * newStack(){
stack_tt * newS = malloc(sizeof(stack_tt) * 20);
(*newS).capacity = 1;
(*newS).size = 0;
return newS;
}
void push(stack_tt * stack_p, int value){
if ((*stack_p).size >= (*stack_p).capacity) {
(*stack_p).capacity*=2;
//realloc(stack_p, stack_p->capacity * sizeof(stack_tt));
}
(*stack_p).array = &value;
(*stack_p).size++;
}
int pop(stack_tt * stack_p){
(*stack_p).size--;
int fap = *(*stack_p).array;
return fap;
}
int empty(stack_tt * stack_p){
if ((*stack_p).size >= 1)
return 0;
return 1;
}
Fist of, when I call the line
while(!empty(myStack))
It changes the value in my array to 1.
secondly I'm not able to change individual values in my array, whenever I try things like:
(*stack_p).array[0] = value;
It doesn't know where in the memory to look.
I hope someone is able to help me out :)
There are a couple of problems with the code as I see it.
Lets take the push function where you do
(*stack_p).array = &value;
That will make the array structure member point to the local variable value, and once the function returns the variable cease to exist leaving you with a stray pointer and using that pointer will lead to undefined behavior.
The second problem with that code is that your stack will only be pointing (illegally) to the last element added.
You must allocate memory explicitly for array and use capacity to keep track of how much memory is allocated. The use size as an index into the allocated array for the pushing and popping. Something like
stack_tt * newStack(){
stack_tt * newS = malloc(sizeof(stack_tt)); // Only allocate *one* structure
newS->capacity = 0; // Start with zero capacity
newS->size = 0;
newS->array = NULL;
return newS;
}
void push(stack_tt * stack_p, int value){
if (stack_p->size + 1 > stack_p->capacity){
// Increase capacity by ten elements
int new_capacity = stack_p->capacity + 10;
int * temp_array = realloc(stack_p->array, new_capacity * sizeof(int));
if (temp_srray == NULL)
return;
stack_p->capacity = new_capacity;
stack_p->array = temp_array;
}
stack_p->array[stack_p->size++] = value;
}
int pop(stack_tt * stack_p){
if (stack_p->size > 0)
return stack_p->array[--stack_p->size];
return 0;
}
int empty(stack_tt * stack_p){
return stack_p->size == 0;
}
There is no need to allocate space for 20 structs of type stack_tt, you only need to allocate space for one:
stack_tt * newS = malloc(sizeof(stack_tt));
however you need to allocate space for elements of the struct member array:
newS->array = malloc( sizeof(int)*20);
newS->size = 0;
newS->capacity = 20;
now you can use the array member.
When you push a value to the 'stack', you shouldn't overwrite the array member with the address of the local variable, that doesn't make sense and will cause undefined behavior in addition of loosing the previously allocated memory. Instead simply assign the value to the member array, in the function push:
stack_p->array[stack_p->size] = value;
stack_p->size++;
Similarly when you pop an element, take the current element from the member array:
stack_p->size--;
int fap = stack_p->array[stack_p->size];
The rest of the functions and code should be fixed in the same manner.
You're code is good, but probably you didn't understand the usage of realloc:
//realloc(stack_p, stack_p->capacity * sizeof(stack_tt));
This function returns a pointer to the newly allocated memory, or NULL if the request fails.
The realloc (as the function suggests) takes the memory pointed by the pointer you pass, and copies that memory block in a new and resized block. So the right code should be.
stack_p->array = realloc(stack_p->array, stack_p->capacity * sizeof(stack_tt));
This other line is wrong:
(*stack_p).array = &value;
Change it with:
stack_p->array[stack_p->size] = value;
Another little suggestion, every (*stack_p). can be replaced by stack_p->, which is more elegant.
In the newStack() you're mallocing 20 structs which is kinda useless. You just need one.
Then you should malloc the array for the first time:
newS->array = malloc(sizeof(int));
newS->capacity = 1;
This is an address:
struct Adress {
char name[31], lastname[31], email[48];
};
The goal is to have an address book in the main function, and the user should be able to type in a string, and the program lists out all of the people from the address book whose name or the last name contains the given string.
For example, if the address book contains "john" "doe", "jane" "doey" and "george" "johnson", and the user types in "doe", the output is:
1. john doe johndoe#email.com
2. jane doey janedoey#email.com
This part of the main function should use a function
int search(struct Adress array[], int size_of_the_addressbook, char* string_to_search)
which returns the index of the first found address, and -1 in case no address has been found.
Here's my try:
In the snippet from my main function (there 's no need to post input stuff here):
struct Adress adressbook[1000], *member;
int i = 0;
member = adressbook;
if (search(member, number_of_elements, string_to_seach)) == -1)
printf("No person found.\n");
else while((search(member, number_of_elements, string_to_seach)) != -1)
{
member = adressbook + search(member, number_of_elements, string_to_seach);
++i;
printf("%d. %s %s - %s\n", i, (*member).name, (*member).lastname, (*member).email);
++member;
}
And here's the search function:
int search(struct Adress array[], int size_of_the_addressbook, char* string_to_search)
{
int j, index;
struct Adress *i;
i = array;
while (strstr((*i).name, string_to_search) == 0 && strstr((*i).lastname, string_to_search) == 0)
{
index = ((i - array)/(sizeof (struct Adress)));
if (index == size_of_the_addressbook) return -1;
++i;
}
index = ((i - array)/(sizeof (struct Adresa)));
return index;
}
However, this program gets stuck in an infinite loop in pretty much any case when there is more than one member in the address book. I'm suspecting that in the while loop the search doesn't go on from the previously found member, but rather it starts from the begin each time, therefore it keeps finding the same, firstly found member each time.
Your search never actually returns -1, and your invoke of that search doesn't thusly have an exit condition. Further, you should be adjust each starting point of the next search to be one slot beyond the last discovery point.
I'm nearly certain this is what you're trying to do. I've not tested this (have no data to do so nor any info on the invocation of this functionality), but I hope the point is obvious:
int search(const struct Adress array[],
int size_of_the_addressbook,
const char* string_to_search)
{
const struct Adress *end = array + size_of_the_addressbook;
const struct Adress *i = array;
for (; i != end; ++i)
{
if (strstr(i->name, string_to_search) != NULL ||
strstr(i->lastname, string_to_search) != NULL)
break;
}
return i == end ? -1 : (int)(i - array);
}
void do_search(const struct Adress *array,
int number_of_elements,
const char *string_to_search)
{
int i = search(array, number_of_elements, string_to_search), base=0;
if (i == -1)
{
printf("No person found.\n");
return;
}
while (i != -1)
{
base += i;
printf("%d. %s %s - %s\n", base,
array[base].name,
array[base].lastname,
array[base].email);
base += 1;
// note adjustment of starting point using pointer arithmetic.
i = search(array + base,
number_of_elements - base,
string_to_search);
}
}
Hope it helps. Best of luck.
You have a few problems to mention
You call search() twice in your main loop which is absolutely unnecessary, you should call it once and store it's return value.
Your member pointer, never points after the first match, so the first match will always be found,
leading to an infinite loop.
You increase the member pointer and still pass number_of_elements to the search function. When you increase the member pointer the number of elements left to the right of it's resulting position is decreased by the same number that you increase member.
This expression is not giving the value you think
((i - array)/(sizeof (struct Adress)));
because you are computing the distaince between the two pointers i and array and then dividing it by sizeof(struct Address) which is 110, and as another answer mentioned, the value is automatically scaled, so
((i - array)/(sizeof (struct Adress))); -> i - array;
to see what I mean you may try to print this values
printf("\t%d\n", ((void*)member - (void*)adressbook));
printf("\t%d\n", ((void*)member - (void*)adressbook) / sizeof(*member));
printf("\t%d\n", member - adressbook);
Note: if your OS is 64bit, change the format specifier to "%ld".
This is the code that will do what you need
int search(struct Adress **array, int size_of_the_addressbook, char* string_to_search)
{
int index;
struct Adress *pointer;
if ((size_of_the_addressbook == 0) || (array == NULL) || (*array == NULL))
return -1;
pointer = *array;
index = 0;
while (strstr(pointer->name, string_to_search) == 0 &&
strstr(pointer->lastname, string_to_search) == 0)
{
/* check that we are not at the end of the array. */
if (++index == size_of_the_addressbook)
return -1;
/* not found yet, increment both arrays */
(*array)++;
pointer = *array;
}
return index;
}
and in main()
int index;
int foundIndex;
index = 1;
while ((foundIndex = search(&member, number_of_elements, string_to_seach)) != -1)
{
printf("%d. %s %s - %s\n", index, member->name, member->lastname, member->email);
index += 1 + foundIndex;
number_of_elements -= 1 + foundIndex;
++member;
}
in this approach, the member pointer is increased inside the search() function to point to the found element, a counter is added to reflect how much was advanced.
After the search() function returns, member should be increased by 1 again to point to the next element, and number_of_elements should be decreased by the number of elements advanced in the search function + 1 for the found element.
Also, keep a variable that you update on each iteration that gives you the actual index of the element in the array.
So I'm having a problem where I have an array of char*s (declared as) char** where the array is dynamically allocated (by calloc), but the char*s within it are static.
This works fine for me until I attempt to free the array (during resizing), at which point I get
*** glibc detected *** ./hash_table_test: free(): invalid next size (normal): 0x0891f028 ***
I tried setting all of the ptrs in the array to NULL, but then I got the error
*** glibc detected *** ./hash_table_test: double free or corruption (!prev): 0x0815b028 ***
Here is the relevant code:
Table struct:
struct string_hash_table {
//Array of c-strings
char** table;
//number of elements in the table
int num_elements;
//size of table
int table_size;
//Primes
int *primes;
//Current position in primes array
int primes_index;
//the size of the primes array
int primes_size;
};
//TypeDefs--------------------------------
typedef struct string_hash_table HashTable;
Rehash Function (source of errors)
void rehash_string(HashTable *table) {
int prev_size = table->table_size;
int i;
table->table_size = table->table_size * 2;
//create new array
char** new_table = calloc(table->table_size, sizeof(char*));
printf("new table created\n");
int index;
printf("before loop prev_size is %d\n", prev_size);
//add all elements to new_table
for (i = 0; i < prev_size; i++) {
printf("on %d\n", i);
index = find_spot_string(new_table, table->table_size, table->table[i]);
printf("after find_spot_string\n");
if (index != -1) {
table->table[index] = table->table[i];
}
}
//free and swap
printf("before free\n");
empty_string_array(table->table, table->table_size);
free(table->table);
table->table = new_table;
Initialization of HashTable struct:
//Takes a HashTable and initializes it
void init_hash_table(HashTable *table) {
table->primes_index = 0;
table->num_elements = 0;
table->primes_size = 297;
table->primes = prime_list;
table->table_size = table->primes[0];
table->table = calloc(table->table_size, sizeof(char*));
}
declarations of static strings within:
char* temp = "hello";
add_hash_table_string(table, temp);
temp = "luck";
add_hash_table_string(table, temp);
temp = "stuck";
add_hash_table_string(table, temp);
temp = "buck";
add_hash_table_string(table, temp);
temp = "muck";
add_hash_table_string(table, temp);
temp = "much";
add_hash_table_string(table, temp);
Currently I'm just testing my code here, everything works except for the rehashing function above. Anyone have any ideas? or leads I should follow?
EDIT: adding code for add_hash_table_string
void add_hash_table_string(HashTable *table, char* element) {
//if non-null element, and element is not in the HashTable
if (element != NULL && contains_hash_table_string(table, element) == 1) {
//if the table is full
if (table->table_size / 2 < table->num_elements) {
rehash_string(table);
}
int index = find_spot_string(table->table, table->table_size, element);
table->table[index] = element;
table->num_elements++;
}
}
EDIT2:
forgot to be precise, the error is occurring at the line with free(table->table) in the rehash function
One possible problem, you free the old table with the new size
empty_string_array(table->table, table->table_size);
Another one might be
index = find_spot_string(new_table, table->table_size, table->table[i]);
printf("after find_spot_string\n");
if (index != -1) {
table->table[index] = table->table[i];
If this is supposed to copy the entries to the new_table, it doesn't AFAICS. When index is greater than prev_size, you write beyond the end of table.
this might be a bit long so my apologies.
consider the following code (i've left some irrelevant parts from it). this code receives a pointer to a struct (BoardP theBoard), x & y coords and a value.
the goal is to place the value in a 2D array that is found in the struct.
if the coords are out of bounds, i have to increase the size of the table, copy old data to new data and place the value in its place.
well this code works the first call but in the second call it crashes and writes:
*** glibc detected *** ./b: double free or corruption (top): 0x092ae138 ***
i couldn't find an answer to it and i hope you will help.
These are the calls from main()
BoardP p = CreateNewBoard(10,10);
PutBoardSquare(p,10,5,'X');
PutBoardSquare(p,5,10,'O');
Boolean PutBoardSquare(BoardP theBoard, int X, int Y, char val) {
if (inBounds(X,Y,theBoard->_rows,theBoard->_cols)) {
theBoard->_board[X * theBoard->_cols + Y] = val;
return TRUE;
}
else {
int newRows = (X>=theBoard->_rows) ? (2*X) : theBoard->_rows;
int newCols = (Y>=theBoard->_cols) ? (2*Y) : theBoard->_cols;
BoardP newBoard = CreateNewBoard(newCols,newRows); //this creates a new Board with the new dimensions
if (newBoard == NULL) {
//ReportError(MEM_OUT);
return FALSE;
}
else {
copyData(theBoard,newBoard);
freeBoardArray(&theBoard->_board[0]); //free old array
theBoard->_board = newBoard->_board; //old array point to new array
FreeBoard(newBoard); //free the temp copy THIS CAUSES THE PROBLEM
PutBoardSquare(theBoard,X,Y,val);//recursion, will be in bounds now
return TRUE;
}
}
}
These are the Free functions:
void FreeBoard(BoardP board) {
if (board != NULL) {
printf("FREE 1\n");
//free the board array:
if (board->_board != NULL) {
printf("FREE 2\n");
freeBoardArray(&board->_board[0]);
printf("FREE 3\n");
}
free(board);
}
}
static void freeBoardArray(char * arrP) {
free(arrP); //**PROGRAM CRASH HERE**
}
This is how i create a new board:
BoardP CreateNewBoard(int width, int high) {
BoardP board = (BoardP) malloc(sizeof(Board));
if (board != NULL) {
board->_board = allocateBoardArray(high,width);
if ( board->_board == NULL) {
FreeBoard(board);
//TODO make file ReportError(MEM_OUT);
return NULL;
}
initializeBoard(board,high,width,X_SIGN,SPACE);
return board;
}
else {
FreeBoard(board);
//TODO make file ReportError(MEM_OUT);
return NULL;
}
}
static char* allocateBoardArray(int row, int col) {
char* newBoard = (char*) malloc(row * col * sizeof(char));
if (newBoard == NULL) {
return NULL;
}
return newBoard;
}
this is BoardP:
typedef struct Board* BoardP;
You have to free memory which you have allocated and no longer want to hold a reference too.
from your code i can see the following line.
theBoard->_board = newBoard->_board;
Now you maintain reference to a allocated pointer and then free that same pointer itself.
Example code:
char *foo()
{
char *ref1;
char *ref2;
ref1 = malloc(256);
ref2=ref1;// Holding reference to a pointer in another pointer
strcpy(ref1,"stackoverflow");
printf("%s %s",ref1,ref2); // This prints stackoverflow twice
free(ref1); // This is valid but you can access ref2 or ref1 after this point
return ref2; /// This will cause problems
}
Try this:
copyData(theBoard, newBoard);
/* swap the _board pointers */
char * b = theBoard->_board;
theBoard->_board = newBoard->_board;
newBoard->_board = b;
FreeBoard(newBoard); /* cleanup the temp struct and the old array */
This errors says that you are trying to free the memory which is already freed by you. What i am suspecting here is this block of code
if (board != NULL) {
printf("FREE 1\n");
//free the board array:
if (board->_board != NULL) {
printf("FREE 2\n");
freeBoardArray(&board->_board[0]);
printf("FREE 3\n");
}
free(board);
once you are freeing the part of structure freeBoardArray(&board->_board[0]); and then you are freeing the whole structure free(board);, and it looks to me causing the problem.Why you passing the address of the _board pointer?I wrote the code on the same line of code,which causing the problem.
struct a{
int * next;
};
int main(){
struct a *aptr = (struct a *)malloc(sizeof(struct a));
aptr->next=(int *)malloc(5*sizeof(int));
free(&aptr->next);
free(aptr);
return 0;
}
this code will cause the same issue as you shown. Now again try this code after removing '&' from free(&aptr->next);statement.It will work fine.
So i think you got a clue where you have to modify.
Running this code under valgrind will tell you exactly on which line you a.) first freed the memory and b.) when you tried to free it again.
It will also tell you if you try and access any addresses which are inside a block that you have freed.