Thread: Fast memory copy and realloc()

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    20

    Fast memory copy and realloc()

    Hello,

    Could you tell me how to build a grid of data that can be update very fast.
    For instance I would like to cut the top of the grid and move up all the rest
    of the data. I do not use memcpy() because it wast time.

    I give bellow a sample of program and it gives me an "segmentation fault".

    What is wrong in my code ?

    Thanks.

    Code:
    // The goal of this program is to remove
    // the head of an int array structure. At first the structure have 10 lines of int
    // , at end it must have 9 lines.
    //The old location  [0] will be filled by value of [1] and so on.
    // This works under windows/Watcom compiler not for linux GCC/GLIBC. Why ?
    
    #include <stdio.h>
    #include <stdlib.h>
    
    void **data;
    void *temp;
    
    int main(void)
    {
     data  = (void**) malloc(10);
     data[0]= (int*)malloc(sizeof(int)); 
     data[1]= (int*)malloc(sizeof(int)); 
     //****************** Set data ***********
     data[0]=100; data[1]=200;
     //*********** Cut the head of array ***** 
     temp=data[1];
     data[0]= (int*) realloc(temp, sizeof(int));
    	
     printf("%d\n", data[0]); // I expect to obtain 200 here as result not 100.
    
     return(0);
    }
    Last edited by intmail; 10-28-2006 at 06:11 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
     
    data  = (void**) malloc(10);//why do you need 10 bytes of memory there is no pointer that can take 5 bytes ;)
     
    //data can be NULL - why not check it before using this pointer?
    
     data[0]= (int*)malloc(sizeof(int)); //data[0] is now a pointer to the allocated memory
    //and can be NULL also...
    
     //****************** Set data ***********
     data[0]=100; //and now your pointer is equal to 100 good address, but do you know what is stored there?
    //and what about your allocated memory - you get a memory leak
     
    data[1]=200;
     //*********** Cut the head of array ***** 
     temp=data[1];
     data[0]= (int*) realloc(temp, sizeof(int));//realloc awaits a pointer allocated by malloc
    //but you give 200 - maybe its a good address ;) but it obviously was not allocated by
    //malloc...
    //there are other issues - but you should fix these first...

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    And why are you so sure memcpy wastes time? Since your code can't possibly work, how did you profile it to find out that it's faster than memcpy?

    Don't assume something is faster or slower until your test it. Make it work first, worry about fast later on.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > This works under windows/Watcom compiler not for linux GCC/GLIBC. Why ?
    Luck?

    Almost every line of your program has issues.

    > I do not use memcpy() because it wast time.
    What do you think realloc will do if it decides to move the block?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Issues with free and dynamic memory
    By dan s in forum C Programming
    Replies: 3
    Last Post: 01-14-2007, 03:44 AM
  3. realloc() doesnt copy the block correctly!
    By TalosChen in forum C Programming
    Replies: 7
    Last Post: 05-19-2006, 05:33 AM
  4. A bullet-proof fgets wrapper?
    By n7yap in forum C Programming
    Replies: 15
    Last Post: 12-16-2004, 05:19 PM
  5. Using realloc to make memory smaller?
    By electrolove in forum C Programming
    Replies: 6
    Last Post: 02-06-2003, 10:00 AM