Tags down


Why am I getting a double free or corruption error with realloc()?

By : tong
Date : July 26 2020, 07:00 PM
it helps some times As a general rule, you should never do a free or realloc on a user provided buffer. You don't know where the user allocated the space (in your module, in another DLL) so you cannot use any of the allocation functions on a user buffer.
Provided that you now cannot do any reallocation within your function, you should change its behavior a little, like doing only one replacement, so the user will be able to compute the resulting string max length and provide you with a buffer long enough for this one replacement to occur.
code :
void  strrep(char *input, char *search, char *replace);
char* strrepm(char *input, char *search, char *replace);
void  strrepmfree(char *input);

Share : facebook icon twitter icon

realloc and free causes "double free or corruption"

By : Todd Ryan Patrick
Date : March 29 2020, 07:55 AM
will help you The problem is that while stringAppend reallocates the pointers, only stringAppend is aware of this fact. You need to modify stringAppend to take pointer-to-pointers (char **) so that the original pointers are updated.

Simple test of malloc and free with int pointer causes double free or corruption error

By : Dattatraya
Date : March 29 2020, 07:55 AM
wish help you to fix your issue The pointer passed to free() must be returned from a previous call to malloc() (or calloc() or realloc()). This is not the case as p has the address of a after:
code :
p = &a;
free(p); //ERROR
// printf("p: %x\n", p); // Shall cause error, because space was freed
// printf("*p: %x\n", *p); // Shall cause error, because space was freed
printf("a: %s\n", a); /* Must be '%d' as 'a' is an 'int'. */
printf("p: %x\n", p);

Issues with C in realloc() or free() or double free or corruption

By : user6713551
Date : March 29 2020, 07:55 AM
Hope that helps I am trying to capture audio packets of variable size, strip the RTP header, then concatenate the audio data to 20 bytes each. My goal is to create something like a queue and just use pointer arithmetic to chop up data before I copy 20 bytes to a buffer. The issue occurs when I get a large amount of audio bytes coming into the queue(probably greater than 20). Here is the while loop that captures, copies to queue, and chops up the data: , This is a significant problem:
code :
void cpy_to_q(unsigned char **qs_ptr, unsigned char **qcur_ptr, unsigned char **qsa_ptr, 

    unsigned char *data, int num_bytes, int tot_bytes) {
        if(*qs_ptr == NULL) {
                *qs_ptr = (unsigned char *)malloc(num_bytes*sizeof(unsigned char) + 1);
                *qcur_ptr = *qs_ptr;
                *qsa_ptr = *qs_ptr;
                memcpy(*qs_ptr, data, num_bytes);
        } else {
                *qs_ptr = (unsigned char *)realloc(*qs_ptr, tot_bytes*sizeof(unsigned char) + 1);
                printf("size of q: %d\n", tot_bytes);
                *qsa_ptr += num_bytes;
                memcpy(*qsa_ptr, data, num_bytes);
void cpy_to_q
    unsigned char **qs_ptr,
    unsigned char **qcur_ptr,
    unsigned char **qsa_ptr,
    unsigned char *data,
    int num_bytes,
    int tot_bytes
    if(*qs_ptr == NULL)
        *qs_ptr = malloc(num_bytes*sizeof(unsigned char) + 1);
        *qcur_ptr = *qs_ptr;
        *qsa_ptr = *qs_ptr;
        memcpy(*qs_ptr, data, num_bytes);
        size_t cur_diff = *qcur_ptr - *qs_ptr;
        size_t sa_diff = *qsa_ptr - *qs_ptr;

        // now realloc (note: you really should error check this)
        *qs_ptr = realloc(*qs_ptr, tot_bytes*sizeof(unsigned char) + 1);
        printf("size of q: %d\n", tot_bytes);

        // now reposition your old pointers.
        *qcur_ptr = *qs_ptr + cur_diff;
        *qsa_ptr = *qs_ptr + sa_diff;

        // and finally continue as before
        *qsa_ptr += num_bytes;
        memcpy(*qsa_ptr, data, num_bytes);

double free or corruption error happens when call free in c

By : aikzTortuga
Date : March 29 2020, 07:55 AM
hope this fix your issue I'm a newer in c, for learning it, i'm trying to write a function to manually read characters from std input. The program will read lines from std and output them, ant it will end when meets an empty line. , Because you are freeing your data and then using it:
code :
        temp = realloc(s, sizeof(char) * size);
        if (s != NULL && temp != s) free(s);
void *realloc(void *ptr, size_t new_size)
    void* newptr = malloc(size);
    size_t oldsize = find_size(ptr);
    memcpy(newptr, ptr, oldsize);
    return newptr;

Malloc/free double free or corruption error

By : Ioanna
Date : March 29 2020, 07:55 AM
I wish this help you I have a problem with a simple malloc/free functions I use in a more complex program and I can't find how to get rid of this problem. , Here
code :
char a[512] = "blablabla";
char a[512];
strcpy(a, "blablabla");
sprintf( (char*)MessageOut, "%s%s",a, b ); 
  MessageOut = malloc(strlen(a) + strlen(b) + 1); /* There is no need to cast 
                                                     the result fo malloc in C. */
Related Posts Related Posts :
  • Passing a pointer to place in array
  • What is int a=(i*+3); and how c compiler will execute it?
  • How do I define a structure with variable members based off another member in C?
  • Program instantly stopping in C?
  • Incomplete definition when using struct in header file
  • bsearch() in C giving segmentation fault in new gcc version 7.4.0
  • Why does this factorial function break after a certain point?
  • When is it "allowed" not to free dynamically allocated memory?
  • Hangman Game Bug
  • What is the purpose of listen() in Socket Programming?
  • Why is the function only returning the multiplied value?
  • "Initializer element is not constant" when defining an object as a static member of a function
  • How to completely overwrite output in c
  • Is there a way to interchange mathematical operators in a while or for loop?
  • How do you continue to scan until new line C without using (fgets)
  • How to read a very large dataset from an HDF5 file?
  • Check if an array at a certain index has an uninitialized value in C
  • Pointer of Strings and Integers
  • The difference between atomic_load and __atomic_load?
  • Why does printing a return carriage significantly reduce the rate at which a printf statement executes in C
  • C char pointer get a specific character
  • Segmentation fault when using setenv (C)
  • Why parenthesis are necessary for typecasting in C?
  • Handling Backslash Escape Sequences in C
  • Is there a C snippet that computes overflow-safe addition efficiently without using compiler builtins?
  • Is there a way to avoid code duplication in multiple similar functions?
  • Why doesn't my function to transform letters into uppercase work if I pass in strings as variables?
  • Copying one file to another: scanf keeps looping
  • Can C cope with sub-byte instruction addressing?
  • What does the fdim acronym stand for?
  • How to fix "error: control reaches end of non-void function"?
  • Surprising expansion of variadic GNU C preprocessor macros in the presence of the ## operator
  • Minimum time quantum needed in nanosleep(), usleep() to yield the CPU
  • Is there a fixed point representation available in C or Assembly
  • Why is my program skipping my first loop?
  • Counting the number of characters in C language
  • Valgrind + C: Handling uninitialized pointers without error
  • Reading a string from a file with C. Fopen with w+ mode is not working
  • Why can't char works but int does
  • POSIX message queue connection issue: mq_open No such file or directory
  • Why isn't the "gets()" function declared when I include stdio.h?
  • Finding the maximum and minimum sum of all but 1 integer(s) in a given array
  • struct/constructor/pointer - C programming language
  • C - remove students from an array(array of structure) except 10 with the highest average grade
  • Trouble reading a .txt file and storing into an array
  • how do I get a different result on compiler?
  • Why is it that my pipe does not read in the printf despite replacing stdin?
  • read indefinite amount of integers until one is negative and then print them
  • My second scanf is not working - what can I do?
  • Is output always determined by the %eax register in inline assembly in C?
  • What exactly happens when a character array is initialized with data larger than its size?
  • Ask about char in array
  • In a code segment of Hackerrank it was advised to make the string static. Why isn't the code working without static stri
  • Using rdmsr/rdpmc for branch prediction accuracy
  • How to make a function find the address of the function pointer it's been called by?
  • C - charting a bar graph using 2D array - wont chart negative values
  • Segmentation fault in C
  • Pass String to argument of Syscall in Go
  • envp in execve system call contain nothing execpt of the string" LS_C"
  • Copy a byte to another register in GNU C inline asm, where the compiler chooses registers for both operands
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org