why is realloc() crashing after the 2nd loop? - c

this is just a test code i've been using to figure out how the code works before impelmenting it into my program. basically the same issue happens in both programs, im able to realloc 2 times then it crashes. the final project is a 2d pointer, list length and size lengths. ach of these has to be able to be resized to reasonable limits.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char** tasks;
char buff[1089];
int size, i, list_size=1;
tasks = (char**)malloc(list_size * sizeof(char*));
printf("input a string!\n");
gets(buff);
*tasks[0]=(char*)malloc((strlen(buff)+1));
tasks[0]=buff;
printf("%s\n", tasks[0]);
for (i=1;i<8 ;i++){
list_size++;
printf("%d\n", list_size);
*tasks = realloc(*tasks, list_size* sizeof(char*));
printf("input a string!\n");
gets(buff);
*tasks[i]=(char*)malloc((strlen(buff)+1));
tasks[i]=buff;
printf("%s\n", tasks[i]);
}
free(tasks);
return 0;
}
what am i messing up with here?

Several problems here.
*tasks[0]=(char*)malloc((strlen(buff)+1));
As pointed out by David Heffernan in the comments, you are assigning a pointer to a char. You probably just meant to assign to tasks[0].
tasks[0]=buff;
This is not how you copy a string. You're setting tasks[0] to point to your fixed buffer, and leaking the memory you allocated in the previous step.
*tasks = realloc(*tasks, list_size* sizeof(char*));
This is not a safe way to realloc, and you are also reallocing the wrong pointer (the first entry in the list, rather than the list itself). If the allocation fails, you have lost the original pointer, and leaked that memory. You should realloc to a temporary variable first, like this:
char *temp = realloc(tasks, list_size* sizeof(char*));
if (temp != NULL)
tasks = temp;
And finally, don't cast the result of malloc().

Related

Dynamically allocating array of strings

I want to dynamically allocate array of strings, but I'm not sure how I can do this. So I thought of making a struct and dynamically allocate that struct. So I made the code below, but this code creates assertion failure.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char str1[20];
char str2[20];
} String;
int main(void)
{
String * list;
list = (String *)malloc(sizeof(String));
int i = 1;
for (; i < 6; i++) {
realloc(list, i * sizeof(String));
printf("Input String 1: ");
scanf("%s", list[i - 1].str1);
printf("Input String 2: ");
scanf("%s", list[i - 1].str2);
}
for (i = 0; i < 5; i++)
printf("%s\t%s\n", list[i].str1, list[i].str2);
free(list);
}
What have I done wrong and how can I fix this problem?
Thanks :)
The man page for realloc says:
The realloc() function returns a pointer to the newly allocated
memory, which is suitably aligned for any kind of variable and may be
different from ptr, or NULL if the request fails.
The new pointer can be different from the one you passed to realloc, so you need to collect and use the pointer returned by realloc.
A structure always has the same size so with this implementation you'd be stuck with always having an array of size 2.
A way to declare an array of strings (which are themselves arrays of characters) s doing
char **string;
If you want an array of 20 strings then that'd be:
string = malloc(sizeof(char*)*20);
Structs must have constant size, so i don't think the compiler will like you trying to allocate more memory for a structure than what it was defined with.

If I am reducing the size of a char ** pointer using realloc, does it internally frees up the individual char * pointers of the reduction size

I am trying to reduce the size of a char ** double pointer using realloc.
First the char ** pointer is allocated using malloc and then the individual char* pointers as given below.
char **first= (char**)malloc(num*sizeof(char*));
char **last=(char**)malloc(num*sizeof(char*));
for(int i=0;i<num;i++)
{
//each individual pointer holds 20 characters
first[i]=(char*)malloc(20*sizeof(char));
last[i]=(char*)malloc(20*sizeof(char));
}
Now after some operations, I want to reduce the size of first and last by one counter value where counter is less than num.
Purpose is to free last counter number of records.
If I do the following
//Freeing up internal pointer memories
i = 0;
while (i<counter)
{
free(first[num1-1 - i]);
free(last[num1-1-i]);
i++;
}
//Reducing the size of the double pointer using realloc
first = (char **)realloc(first, (num1-counter) * sizeof(char *));
last = (char **)realloc(last, (num1-counter) * sizeof(char *) );
After this if I print the records
for(i=0; i<num - counter; i++){
printf("First Name: %s ",first[i]);
printf("Last Name: %s ",last[i]);
}`
The last record seems to be corrupted.
If I just perform the following and do not free the individual char * pointers, it works fine.
first = (char **)realloc(first, (num1-counter) * sizeof(char *));
last = (char **)realloc(last, (num1-counter) * sizeof(char *) );
My question is does realloc of a double pointer to reduce the size, internally frees up the internal pointers and as I am doing both, it is some how corrupting the memory?
To answer your question: No, it doesn't.
Freeing the nested pointers in a char ** might be rude if the pointer is still in use somewhere else.
The C compiler doesn't follow memory pointers around, nor does it count object references, so it's up to you to free (or keep) the allocated memory of the nested strings within the char **.
As for the example code itself, it seems to have issues, but these were described in the comments to your question.

reading an input(int) and storing it into an array with malloc and realloc

I'm trying to read ints from stdin, but i don't know the length. I tried this but I have no idea why it doesn't work
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr = (int *) malloc(sizeof(int));
int sizeCounter = 0;
int tmp = 0;
while(scanf("%d", &tmp) != EOF)
{
arr = (int *) realloc(arr, sizeof(arr)+sizeof(int));
arr[sizeCounter] = tmp;
sizeCounter++;
}
}
Error - realloc(): invalid pointer: 0xb76d8000 *
This line is wrong.
arr = (int *) realloc(arr, sizeof(arr)+sizeof(int));
sizeof(arr)+sizeof(int) is a constant. It is equal to sizeof(int*)+sizeof(int).
What you need is:
arr = (int *) realloc(arr, (sizeCounter+1)*sizeof(int));
R Sahu gave the correct answer on why this is not working.
However, seeing your code I cannot help but cringe at the inefficiency of reallocating the memory block on each read. If you want to use a memory block/dynamic array to store the read-in integers, allocate a reasonable amount of memory (this depends on how much input you expect in a typical case and is entirely application specific) to start. Then reallocate the block when it is full. Memory allocation is a very expensive operation and reallocating the block on each read is incredibly wasteful and therefore should never be done.

Can't copy characters from pointer to another pointer(both with memory allocated)

I have a program that accepts a char input using argv from the command line. I copy the input argv[1] using strcpy to a pointer called structptr(it goes to structptr->words from struct) where memory has been allocated. I then copy character by character from the memory that the pointer structptr points to another pointer called words that points to memory that has been allocated. After i've copied one character i print that element [c] to make sure that it has been copied correctly(which it has). I then finish copying all of the characters and return the result to a char pointer but for some reason it is blank/null. After each copying of the characters i checked if the previous elements were correct but they don't show up anymore([c-2], [c-1], [c]). Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct StructHolder {
char *words;
};
typedef struct StructHolder Holder;
char *GetCharacters(Holder *ptr){
int i=0;
char *words=malloc(sizeof(char));
for(i;i<strlen(ptr->words);i++){
words[i]=ptr->words[i];
words=realloc(words,sizeof(char)+i);
}
words[strlen(ptr->words)]='\0';
return words;
}
int main(int argc, char **argv){
Holder *structptr=malloc(sizeof(Holder));
structptr->words=malloc(strlen(argv[1]));
strcpy(structptr->words, argv[1]);
char *charptr;
charptr=(GetCharacters(structptr));
printf("%s\n", charptr);
return 0;
At first I thought this was the problem:
char *words=malloc(sizeof(char)) is allocating 1 byte (sizeof 1 char). You probably meant char *words = malloc(strlen(ptr->words)+1); - You probably want to null check the ptr and it's member just to be safe.
Then I saw the realloc. Your realloc is always 1 char short. When i = 0 you allocate 1 byte then hit the loop, increment i and put a char 1 past the end of the realloced array (at index 1)
Also your strcpy in main is has not allocated any memory in the holder.
In these two lines,
structptr->words=malloc(strlen(argv[1]));
strcpy(structptr->words, argv[1]);
need to add one to the size to hold the nul-terminator. strlen(argv[1]) should be strlen(argv[1])+1.
I think the same thing is happening in the loop, and it should be larger by 1. And sizeof(char) is always 1 by definition, so:
...
words=realloc(words,i+2);
}
words=realloc(words,i+2); // one more time to make room for the '\0'
words[strlen(ptr->words)]='\0';
FYI: Your description talks about structptr but your code uses struct StructHolder and Holder.
This code is a disaster:
char *GetCharacters(Holder *ptr){
int i=0;
char *words=malloc(sizeof(char));
for(i;i<strlen(ptr->words);i++){
words[i]=ptr->words[i];
words=realloc(words,sizeof(char)+i);
}
words[strlen(ptr->words)]='\0';
return words;
}
It should be:
char *GetCharacters(const Holder *ptr)
{
char *words = malloc(strlen(ptr->words) + 1);
if (words != 0)
strcpy(words, ptr->words);
return words;
}
Or even:
char *GetCharacters(const Holder *ptr)
{
return strdup(ptr->words);
}
And all of those accept that passing the structure type makes sense; there's no obvious reason why you don't just pass the const char *words instead.
Dissecting the 'disaster' (and ignoring the argument type):
char *GetCharacters(Holder *ptr){
int i=0;
OK so far, though you're not going to change the structure so it could be a const Holder *ptr argument.
char *words=malloc(sizeof(char));
Allocating one byte is expensive — more costly than calling strlen(). This is not a good start, though of itself, it is not wrong. You do not, however, check that the memory allocation succeeded. That is a mistake.
for(i;i<strlen(ptr->words);i++){
The i; first term is plain weird. You could write for (i = 0; ... (and possibly omit the initializer in the definition of i, or you could write for (int i = 0; ....
Using strlen() repeatedly in a loop like that is bad news too. You should be using:
int len = strlen(ptr->words);
for (i = 0; i < len; i++)
Next:
words[i]=ptr->words[i];
This assignment is not a problem.
words=realloc(words,sizeof(char)+i);
This realloc() assignment is a problem. If you get back a null pointer, you've lost the only reference to the previously allocated memory. You need, therefore, to save the return value separately, test it, and only assign if successful:
void *space = realloc(words, i + 2); // When i = 0, allocate 2 bytes.
if (space == 0)
break;
words = space;
This would be better/safer. It isn't completely clean; it might be better to replace break; with { free(words); return 0; } to do an early exit. But this whole business of allocating one byte at a time is not the right way to do it. You should work out how much space to allocate, then allocate it all at once.
}
words[strlen(ptr->words)]='\0';
You could avoid recalculating the length by using i instead of strlen(ptr->words). This would have the side benefit of being correct if the if (space == 0) break; was executed.
return words;
}
The rest of this function is OK.
I haven't spent time analyzing main(); it is not, however, problem-free.

I have debug it for 4 hours,but I still can't find the BUG

This is program is input some string from a file, then, push strings into LineBuf one by one, after we push one string into LineBuf, print LineBuf,then, make LineBuf empty.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *LineBuf = NULL;
int BufLen = 0;
void PushToBuf(char c)
{
LineBuf = (char *)realloc(LineBuf, (BufLen+2)*sizeof(char));
LineBuf[BufLen] = c;
BufLen++;
LineBuf[BufLen] = '\0';
}
int main()
{
char temp[20];
int i;
FILE *fp;
fp = fopen("input", "r");
while (fgets(temp, 20, fp) > 0)
{
/*Push temp into buf*/
for (i = 0; i < strlen(temp); i++)
PushToBuf(temp[i]);
/*print buf*/
printf("%s\n", LineBuf);
printf("%d\n", BufLen);
/*make buf empty*/
free(LineBuf);
BufLen = 0;
}
return 0;
}
This is my input stream:
This is a test. Good evening
bye~
This is run result:
This is a test file
19
. Good evening
15
glibc detected ./a.out: double free or corruption (fasttop): 0x00000000023fa250
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x7f2ad01bf5b6]
/lib/libc.so.6(cfree+0x73)[0x7f2ad01c5e83]
./a.out[0x400868]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f2ad0166c4d]
./a.out[0x400699]
How realloc ( void * ptr, size_t size ) works:
The size of the memory block pointed to by the ptr parameter is
changed to the size bytes, expanding or reducing the amount of memory
available in the block. The function may move the memory block to a new location, in which
case the new location is returned.
In case that ptr is NULL, the function behaves exactly as malloc,
assigning a new block of size bytes and returning a pointer to the
beginning of it.
In your case the pointer is already freed, but still isn't NULL, so when the program tries to move this memory block, it causes memory corruption.
To solve it, you should do one of the following:
Remove free().
Use malloc instead of realloc.
Set LineBuf to NULL after free().
This does not make LineBuf empty. It free the storage space for LineBuf. When you later realloc LineBuff it attemps to realloc freed space.
/*make buf empty*/
free(LineBuf);
to solve the provlem move the free out of the while loop. and empty free buff byt setting all of the data it stores to null.
for(int i =0; i < BuffLen)
LineBuf[i]='\0';
You're trying to realloc a free'd pointer; you can't do that!
free(LineBuf) is freeing the memory, but you are using LineBuf again later when calling realloc. You should set LineBuf to NULL after freeing it, then the realloc will do malloc and not reallocate. Keep in mind, it is always good practice to set pointers to NULL after freeing them. This helps detect if you are using pointers to freed memory.
BTW, looking at your code I am not quite sure what you intend to do. Depending on what you want to do you might get rid of LineBuf or of fgets. Also: calling strlen for every i is not very performant, you might better check for temp[i] != '\0'.

Resources