How to Free All Memory ?

This is a discussion on How to Free All Memory ? within the C Programming forums, part of the General Programming Boards category; Code: for(iLenCont=iLenMsg; iLenCont<40; iLenCont++) strcat (pMensagem, "."); This code is _STILL_ very much wrong - because you are modifying a ...

  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
    Posts
    158
    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
    Posts
    158
    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
    Posts
    158
    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
    Posts
    158
    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 09: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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #42
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    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
    21,408
    That would be problematic. Test is only a (probably non-null) pointer that points to nowhere.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    158
    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.

Page 3 of 4 FirstFirst 1234 LastLast
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, 11:27 AM
  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, 04:16 PM
  5. free allocated memory on interrupts
    By scrappy in forum C Programming
    Replies: 4
    Last Post: 02-20-2004, 10:13 AM

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