Like Tree3Likes

when should I use memcpy()?

This is a discussion on when should I use memcpy()? within the C Programming forums, part of the General Programming Boards category; let packets[5].pkt_data is 0X5646, the consumer didnt fetch the data in it. And it dint be freed. and now it ...

  1. #16
    Registered User
    Join Date
    May 2012
    Posts
    33
    let packets[5].pkt_data is 0X5646, the consumer didnt fetch the data in it. And it dint be freed. and now it will be rewrite, since the producer call malloc() again. Then packet[5].pkt_data equals to 0X5896.
    What will happen in memory 0X5646?

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by ppdouble View Post
    let packets[5].pkt_data is 0X5646, the consumer didnt fetch the data in it. And it dint be freed. and now it will be rewrite, since the producer call malloc() again. Then packet[5].pkt_data equals to 0X5896.
    What will happen in memory 0X5646?
    It is lost forever. This is called a memory leak. See my answer in post #15 for more info.

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    33
    Quote Originally Posted by anduril462 View Post
    Typically in a producer-consumer model, if the queue is full, the producer stops and waits for the consumer to consumer some data so there is room again. You typically don't want to just overwrite or discard data.

    Regardless of whether you follow that model, or the "just throw away data" model, the rule is still: only free memory when you are all done with it and never need to access it again. It's really that simple. If the consumer is done with it, free it. If the producer is throwing it away because the consumer is taking too long, free it.
    Yes, I wanner follow "just throw away data". Imagine that I wanner 100 packets, and set the buffer size as 10, It will call malloc() 100 times. Calling free() in each iterate may be better before call malloc() again to avoid memory leak.

  4. #19
    Registered User
    Join Date
    May 2012
    Posts
    33
    I dont care that the data is lost, I just worry about the memory errors and the performance things. I am starting in this, and I dont know when will the error occur.

  5. #20
    Registered User
    Join Date
    May 2012
    Posts
    33
    yes, ur right. thx again anduril462.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using memcpy?
    By nyekknyakk in forum C Programming
    Replies: 1
    Last Post: 08-19-2010, 08:47 PM
  2. memcpy
    By m37h0d in forum C++ Programming
    Replies: 28
    Last Post: 04-12-2008, 10:10 PM
  3. memcpy
    By mbooka in forum C Programming
    Replies: 10
    Last Post: 02-28-2006, 02:25 AM
  4. memcpy use?
    By jerrykelleyjr in forum C++ Programming
    Replies: 21
    Last Post: 02-17-2005, 07:39 AM
  5. Help: About memcpy()
    By naruto in forum C Programming
    Replies: 41
    Last Post: 06-25-2004, 04:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21