Thread: A mind for program look clear and resource quickly release and easy error process

  1. #16
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by whiteflags View Post
    Most of your code only hides the fact that freeing at the end of the function is exactly what you wanted to happen. Code isn't faster just because you used a jump. It's often better if you just forgot about jumping and wrote something normal with ifs or loops.
    Freeing at the end of the function can waste memory,and the code can let a object's life go out early object's life ,like #6

  2. #17
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by cprogcs View Post
    Freeing at the end of the function can waste memory,and the code can let a object's life go out early object's life ,like #6
    Freeing malloc'd memory (When and where it is appropriate) is never a waste of memory or time. It is good programming.

    Small programs many times, grow into large programs, and poor techniques initially, compound themselves later.

  3. #18
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by rstanley View Post
    Freeing malloc'd memory (When and where it is appropriate) is never a waste of memory or time. It is good programming.

    Small programs many times, grow into large programs, and poor techniques initially, compound themselves later.
    My mind is all malloc variables must free at the end of function can waste memory.

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,670
    > My mind is all malloc variables must free at the end of function can waste memory.
    Then how would you deal with say strdup(), which makes a copy of a string in allocated memory, and returns the pointer to you.
    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.

  5. #20
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    > My mind is all malloc variables must free at the end of function can waste memory.
    Then how would you deal with say strdup(), which makes a copy of a string in allocated memory, and returns the pointer to you.
    I think some method
    1 Rewrite code,first strlen then malloc then MYstrdup(stringpointer,mallocpointer,length) then use then free,
    2 In some angle look strdup same as malloc( strlen( stringpointer ) ) to keep the code "style",

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    My mind is all malloc variables must free at the end of function can waste memory.
    I'm really confused as to how an easy to follow guideline has become a rule from me that we must follow and debate.

    1 Rewrite code,first strlen then malloc then MYstrdup(stringpointer,mallocpointer,length) then use then free,
    2 In some angle look strdup same as malloc( strlen( stringpointer ) ) to keep the code "style",
    Now imagine the angle that you made a small function:
    1) call strdup
    2) do something to the string
    3) print the result
    4) free

    It's a pretty easy guideline to understand how to use. I never promised it would work in every situation.

  7. #22
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by whiteflags View Post
    I'm really confused as to how an easy to follow guideline has become a rule from me that we must follow and debate.


    Now imagine the angle that you made a small function:
    1) call strdup
    2) do something to the string
    3) print the result
    4) free

    It's a pretty easy guideline to understand how to use. I never promised it would work in every situation.
    A very very important key thing is look clear,like the image
    A mind for program  look clear and resource quickly release and easy error process-cprogcs1-jpg

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Is it just me, or does this image fail to communicate anything at all? I can't understand this.

  9. #24
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by whiteflags View Post
    Is it just me, or does this image fail to communicate anything at all? I can't understand this.
    A object complete conntain a another,look clear,at the same time release resource quickly and easy error proces

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    You're very wrong, sorry.

    Swaps do not at any point release memory, only a call to free would. If you have two pointers A and B, and you swap them, all that has happened is B points to what A used to point to, and A points to what B used to point to.

  11. #26
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by whiteflags View Post
    You're very wrong, sorry.

    Swaps do not at any point release memory, only a call to free would. If you have two pointers A and B, and you swap them, all that has happened is B points to what A used to point to, and A points to what B used to point to.
    Swap can not free,it let free very very quickly and look clear,show in the hello world print program at #1

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    it let free very very quickly
    All you're saying is that it's faster.

    You could waste your life trying to prove that your program is faster by measuring the time it takes your program to complete, and comparing it to the time of Salem's code. Then you would have some evidence of your claim.

    Or you could stop now and realize that there is no important time difference between you two. The swap is nothing but an unnecessary step. I think if you actually measured where your program spent most of its time, you would see that the program can only be as fast as the time it takes printf to execute.

  13. #28
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by whiteflags View Post
    All you're saying is that it's faster.

    You could waste your life trying to prove that your program is faster by measuring the time it takes your program to complete, and comparing it to the time of Salem's code. Then you would have some evidence of your claim.

    Or you could stop now and realize that there is no important time difference between you two. The swap is nothing but an unnecessary step. I think if you actually measured where your program spent most of its time, you would see that the program can only be as fast as the time it takes printf to execute.
    I forget to say my one mind,if the code look clear then error can be found easy.
    The balance of save resource and to proces error easy and let code look clear and so on is very very precious .

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,670
    Well your first post fails to be clear on so many levels.
    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. #30
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    Well your first post fails to be clear on so many levels.
    The key point to save resource is clear in first post.In many cases it is so not bad to spend many many energy to the important things even they look few.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lnk 2005 error in release only
    By harryGrat in forum Windows Programming
    Replies: 1
    Last Post: 02-11-2009, 09:41 AM
  2. release resource for WebClient class
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-02-2008, 01:14 PM
  3. is there easy way to clear nested vector?
    By timchen in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2007, 10:18 PM
  4. MFC: LineDraw Release Error
    By mrafcho001 in forum Windows Programming
    Replies: 1
    Last Post: 06-25-2005, 10:24 AM
  5. my very cool new program has a mind boggling probem!!!
    By face_master in forum C++ Programming
    Replies: 11
    Last Post: 12-20-2001, 04:37 PM

Tags for this Thread