Thread: How to Free All Memory ?

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	for(iLenCont=iLenMsg; iLenCont<40; iLenCont++)
    		strcat (pMensagem, ".");
    
    This code is _STILL_ very much wrong - because you are modifying a constant variable, and who knows what happens if you do that. On my Windows system, compiling using gcc, I get a immediate crash.

    If your compiler supports it, you could add
    Code:
    const char *pMensagem
    to your header - but your compiler may not accept that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #32
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    i Have changed:

    Code:
    void EscreveDisplay (const char *pMensagem, unsigned int pDelaySec){
    But compiler warning says: Suspicious pointer conversion in this line:

    Code:
    strcat (pMensagem, ".");

  3. #33
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    i Have changed:

    Code:
    void EscreveDisplay (const char *pMensagem, unsigned int pDelaySec){
    But compiler warning says: Suspicious pointer conversion in this line:

    Code:
    strcat (pMensagem, ".");
    Exactly, because YOU SHOULD NOT DO THAT! You are writing to a constant - you have no right to modify that!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #34
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    do you believe in the suspect of this a time to process serial receive string ?

    I added DelayMs (2) after each of tocom and now, the system seems better, or no... i will test more this..

    the serial com is 2400 of baudrate.

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    do you believe in the suspect of this a time to process serial receive string ?

    I added DelayMs (2) after each of tocom and now, the system seems better, or no... i will test more this..

    the serial com is 2400 of baudrate.
    If you have not fixed the strcat() to be like your other piece of code that writes '.' into the local string inside the function escreve...(), then I would not put any trust into what happens and what consequences any other changes makes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #36
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    15/1/2009 14:00:53 - Rec: ..........................

    I had received ......... again, next to escreve () in the function EnviaPedidoPc.

    otherwise,

    sprintf can fill a specified repeated char in a string ?

    like some:

    sprintf (pMensagem, "%40c", '.');

  7. #37
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
     for (iLenCont=10; iLenCont<=49; iLenCont++)
      cDispPacote [iLenCont] = '.';
    There is NOTHING wrong with this code, as far as I can see. strcat or sprintf will definitely be slower an probably take up more code-space too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #38
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    yes, you're correct,

    but here not run andd crash my system,

    Or I have some problem and not see
    Or this equipment isvery........ing crazy

    strcat addd any \0 at end of string?
    Last edited by sergioms; 01-15-2009 at 10:19 AM.

  9. #39
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by sergioms View Post
    when the program crashs, all outputs stop, and stop execution.

    Yes so do them before it crashes
    You can use fflush to for it to write it's output.

  10. #40
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by esbo View Post
    Yes so do them before it crashes
    You can use fflush to for it to write it's output.
    Not relevant here, I should think.

    Figuring out WHY the system crashes is another better idea, I should think.

    Yes, strcat() will put a zero on the end of the string - but as I said earlier, you should DEFINITELY NOT modify incoming constant strings (e.g. "Hello, World" passed to your function) - that is DEFINITELY VERY BAD. Never EVER is that right.

    It is quite possible that you have other problems similar to this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #41
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I've no idea what the current code looks like.

    But anything involving strcat - of either the source or destination strings is broken.
    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.

  12. #42
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Quote Originally Posted by matsp View Post
    Not relevant here, I should think.

    Figuring out WHY the system crashes is another better idea, I should think.

    Yes, strcat() will put a zero on the end of the string - but as I said earlier, you should DEFINITELY NOT modify incoming constant strings (e.g. "Hello, World" passed to your function) - that is DEFINITELY VERY BAD. Never EVER is that right.

    It is quite possible that you have other problems similar to this.

    --
    Mats
    THEN i need use

    unsigned char *Test;

    strcpy (Test, "LENKE AUTOMACAO");

    EscreveDisplay (Test, 0);

    ?

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    That would be problematic. Test is only a (probably non-null) pointer that points to nowhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #44
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by sergioms View Post
    15/1/2009 14:00:53 - Rec: ..........................

    I had received ......... again, next to escreve () in the function EnviaPedidoPc.

    otherwise,

    sprintf can fill a specified repeated char in a string ?

    like some:

    sprintf (pMensagem, "%40c", '.');
    Are you not writing to a constant again?

  15. #45
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Quote Originally Posted by laserlight View Post
    That would be problematic. Test is only a (probably non-null) pointer that points to nowhere.
    i believe in this, now i will test to try.

    i use "TEXT" to argument,
    next i use strlen function, then i suspect this not have a \0 char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Memory leak with detached pthreads - how to free?
    By rfk in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2007, 06:50 AM
  4. free memory in structure
    By franziss in forum C++ Programming
    Replies: 22
    Last Post: 01-08-2007, 05:16 PM
  5. free allocated memory on interrupts
    By scrappy in forum C Programming
    Replies: 4
    Last Post: 02-20-2004, 11:13 AM