Thread: Error when realloc in C

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    17

    Lightbulb Error when realloc in C

    Hi everyone,
    below is my program compiled with eclipse,

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int *p_array;
    
    void initArr(int size){
        p_array = (int *)malloc(sizeof(int)*size);     
    
    }
    void storeData(int size){
        p_array =(int *) realloc(p_array, size);
        int idx;
        for(idx = 0; idx < size; ++idx){
                p_array[idx] = idx;
                printf("%d\n",p_array[idx]);
        }
        free(p_array);
    }
    
    int main(void) {
        initArr(5); //OK
        storeData(5); //!
        storeData(8); //!
        return EXIT_SUCCESS;
    }
    in main function. I only can use one storeData to init an array, If I use both, error will occur. Please help to solve my problem.
    Thank you so much!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,730
    Do you want realloc to give you "size" bytes or "sizeof(int)*size" bytes of memory? I'm asking because you do the first, while maybe you want the second.
    Devoted my life to programming...

  3. #3
    Registered User GulluButt's Avatar
    Join Date
    Jun 2016
    Posts
    2
    you are freeing the Memory inside storeData function which is wrong. What happens, look when you call first time it realloc it only allocate 5 bits (your should write sizeof(int)*size) I guess it is a typeerror as you have written it above. If you free this Memory you cannot realloc to freed Memory this is undefined. realloc can only be happend if you have already a malloc etc. Put free at the end of the program before final return.

  4. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Quote Originally Posted by GReaper View Post
    Do you want realloc to give you "size" bytes or "sizeof(int)*size" bytes of memory? I'm asking because you do the first, while maybe you want the second.
    Thanks for your reply.
    yes, I want to a space of "sizeof(int)*size" bytes of memory.
    Please give me some suggestions.

  5. #5
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Dear @GulluButt,
    Thanks for your reply, I put at the end, but still error. please give me some suggestion

  6. #6
    Registered User GulluButt's Avatar
    Join Date
    Jun 2016
    Posts
    2
    Quote Originally Posted by thachdovan View Post
    Dear @GulluButt,
    Thanks for your reply, I put at the end, but still error. please give me some suggestion
    It should work. I have just made changes as I said in previous answer.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int *p_array;
     
    void initArr(int size){
        p_array = (int *)malloc(sizeof(int)*size);     
     
    }
    void storeData(int size){
        p_array = realloc(p_array, sizeof(int)*size);
        int idx;
        for(idx = 0; idx < size; ++idx){
                p_array[idx] = idx;
                printf("%d\n",p_array[idx]);
        }
        
    }
     
    int main(void) {
        initArr(5); //OK
        storeData(5); //!
        storeData(8); //!
        free(p_array);
        return EXIT_SUCCESS;
    }
    Here is my Output that clang Compiler Outputs

    Code:
    0
    1
    2
    3
    4
    0
    1
    2
    3
    4
    5
    6
    7
    As a side note you do not Need to write (int *) when malloc and realloc

  7. #7
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Quote Originally Posted by GulluButt View Post
    It should work. I have just made changes as I said in previous answer.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int *p_array;
     
    void initArr(int size){
        p_array = (int *)malloc(sizeof(int)*size);     
     
    }
    void storeData(int size){
        p_array = realloc(p_array, sizeof(int)*size);
        int idx;
        for(idx = 0; idx < size; ++idx){
                p_array[idx] = idx;
                printf("%d\n",p_array[idx]);
        }
        
    }
     
    int main(void) {
        initArr(5); //OK
        storeData(5); //!
        storeData(8); //!
        free(p_array);
        return EXIT_SUCCESS;
    }
    Here is my Output that clang Compiler Outputs

    Code:
    0
    1
    2
    3
    4
    0
    1
    2
    3
    4
    5
    6
    7
    As a side note you do not Need to write (int *) when malloc and realloc
    Finally, It worked, but I have a question. Should I use DMA in runtime application and in a small memory device?
    Thank you so much!

  8. #8
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    I have another problem need your help:
    Code:
    void initArr(int size){
        p_array = (int *)malloc(sizeof(int)*size);      // allocate 50 ints
        int idx;
            for(idx = 0; idx < size; ++idx){
                    *p_array = idx;
                    printf("%d\n",p_array[idx]);
                    ++p_array;
            }
        free(p_array);
    }
    in this case, how I can free the pointer.
    Thank you so much!

  9. #9
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by thachdovan View Post
    In this case, how I can free the pointer.
    Thank you so much!
    You can only free what you allocated with malloc. In the above code 'p_array' looks like a global variable or something which means you don't have to free it.

    Quote Originally Posted by thachdovan View Post
    Finally, It worked, but I have a question. Should I use DMA in runtime application and in a small memory device?
    Thank you so much!
    What do you mean by DMA ? I take this to mean Direct Memory Access which is irrelevant here.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,127
    > *p_array = idx;
    > printf("%d\n",p_array[idx]);
    > ++p_array;
    This should be
    p_array[idx] = idx;
    printf("%d\n",p_array[idx]);


    Do NOT modify the pointer variable used to hold the result of malloc, otherwise you won't be able to free it later on.

    If you really like doing p++ on your pointers, then do it on a copy of the pointer, not the original variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Do NOT modify the pointer variable used to hold the result of malloc, otherwise you won't be able to free it later on.

    If you really like doing p++ on your pointers, then do it on a copy of the pointer, not the original variable.
    In my knowledge, p_array[idx] = *(p_array + idx). could you explain what is difference here? Thank you!

  12. #12
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Quote Originally Posted by Aslaville View Post
    What do you mean by DMA ? I take this to mean Direct Memory Access which is irrelevant here.
    Of course, Dynamic memory allocation, Thank you.

  13. #13
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by Salem View Post
    > *p_array = idx;
    > printf("%d\n",p_array[idx]);
    > ++p_array;
    This should be
    p_array[idx] = idx;
    printf("%d\n",p_array[idx]);


    Do NOT modify the pointer variable used to hold the result of malloc, otherwise you won't be able to free it later on.

    If you really like doing p++ on your pointers, then do it on a copy of the pointer, not the original variable.
    True enough but ++p_Array == p_array = p_array+sizeof(pointer); which in effect looses the primary address creating a memory leak.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,127
    Quote Originally Posted by thachdovan View Post
    In my knowledge, p_array[idx] = *(p_array + idx). could you explain what is difference here? Thank you!
    There isn't any difference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Quote Originally Posted by Aslaville View Post
    You can only free what you allocated with malloc. In the above code 'p_array' looks like a global variable or something which means you don't have to free it.
    In above code, I init p_array with malloc, so I need to free it. why in this case, I dont have to free it. Please share in more detail. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-13-2013, 11:35 AM
  2. Error using realloc inside function
    By NotAProgrammer in forum C Programming
    Replies: 10
    Last Post: 12-29-2012, 08:13 AM
  3. Replies: 3
    Last Post: 08-22-2008, 11:12 AM
  4. Error when freeing memory (allocated with realloc)
    By svdrjeug in forum C Programming
    Replies: 18
    Last Post: 01-03-2008, 11:16 AM
  5. use of realloc
    By alzar in forum C Programming
    Replies: 3
    Last Post: 09-17-2007, 12:18 PM

Tags for this Thread