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; Originally Posted by sergioms THEN i need use unsigned char *Test; strcpy (Test, "LENKE AUTOMACAO"); EscreveDisplay (Test, 0); ? But ...

  1. #46
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    THEN i need use

    unsigned char *Test;

    strcpy (Test, "LENKE AUTOMACAO");

    EscreveDisplay (Test, 0);

    ?
    But why - there is a perfectly fine solution there already which uses the "new" string which you CAN modify. And of course, as Laserlight says, you must either use a char array or allocate some memory (and make sure that's at least 41 bytes, if we use the above strcat() type code).

    But I would prefer to not use strcat at all.

    --
    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. #47
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    I MAKE A TEST WITH THIS CODE:

    Code:
    void EscreveDisplay (unsigned char *pMensagem, unsigned int pDelaySec){
    	unsigned char cDispPacote [52];
    	int iComDisplay = 2;
    	unsigned int iLims;
    	unsigned int iLenMsg, iLenCont, iContIndex;
    	int iBcc;
    	
    	iDisplaySend = 1;
    	if (pDelaySec > 0)
    		Delay(pDelaySec * 1000);
    	
    	iLims  = 46;
    	
    	cDispPacote [0] = 129;
    	cDispPacote [1] = 126;
    	cDispPacote [2] = iLims % 256;
    	cDispPacote [3] = iLims / 256;
    	cDispPacote [4] = 1 % 256;
    	cDispPacote [5] = 1 / 256;
    	cDispPacote [6] = 0 % 256;
    	cDispPacote [7] = 0 / 256;
    	cDispPacote [8] = 76;
    	cDispPacote [9] = 202;
    
    	iLenMsg = strlen (pMensagem);
     
    	 for (iLenCont=10; iLenCont<=49; iLenCont++)
    		cDispPacote [iLenCont] = '.';
    	 
    	 for(iLenCont=0; iLenCont<iLenMsg; iLenCont++){
    		iContIndex = iLenCont + 10;
    		cDispPacote [iContIndex] = pMensagem [iLenCont];
    	 }
    
    	iBcc = cDispPacote [4];
    	cDispPacote [50] = iBcc;
    	cDispPacote [51] = '\0';
    	
    	for(iContIndex=0; iContIndex<=51; iContIndex++){
    		ToCom (iComDisplay, cDispPacote[iContIndex]);
    		DelayMs (2);
    	}
    	
    	iDisplaySend=0;
    }
    and call this:

    Code:
    void UserCount(void)
    {
    	if (iDisplaySend == 0){
    		VerificaBotoes ();
    		
    		if (iVazia)
    			RecebeSerial ();
    		
    		if (iPesaItem)
    			RecebeSerial ();
    			
    		if (iStepTimer >= 1 && iStepTimer < 10)
    			iStepTimer++;
    	}
    }
    
    void UserInit(void)
    {
    	unsigned char *MsgDisplay;
    	
    	InitLib();
    	
    	PortUser = 5050;	// Habilita porta 5050 para Controle do Usuário (Aplicação Objeto de Comunicação (Listener))
    	
    	// Transforma os Int em Char (ASCII)
    	cSTX = 2;
    	cCR  = 13;
    	cLF  = 10;
    	cTab = 9;
    	
    	IniciaSerial ();
    	IniciaMemoria();
    	
    	strcpy (MsgDisplay, "LENKE AUTOMACAO 1");
    	EscreveDisplay (MsgDisplay, 2);
    
                    strcpy (MsgDisplay, "LENKE AUTOMACAO 2");
    	EscreveDisplay (MsgDisplay, 2);
    
                    strcpy (MsgDisplay, "LENKE AUTOMACAO 3");
    	EscreveDisplay (MsgDisplay, 2);
    	
        AddUserTimerFunction(UserCount, 200); // Adiciona  Timer para Chamar Função UserCount a cada 200  ms
    }
    and the resul is ok:

    LENKE AUTOMACAO 1................................
    LENKE AUTOMACAO 2................................
    LENKE AUTOMACAO 3................................

    BUT, AFTER THE SYSTEM HAS STOPPPED :\\\\\\\

  3. #48
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > unsigned char *MsgDisplay;
    > strcpy (MsgDisplay, "LENKE AUTOMACAO 1");
    Seriously, learn C before you start messing about with embedded systems.

    You're simply making far too many fundamental mistakes to have any success at all.

    Embedded systems do not take prisoners, they do not negotiate and they won't tell you a damn thing about where you screwed up.
    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.

  4. #49
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Quote Originally Posted by Salem View Post
    > unsigned char *MsgDisplay;
    > strcpy (MsgDisplay, "LENKE AUTOMACAO 1");
    Seriously, learn C before you start messing about with embedded systems.

    You're simply making far too many fundamental mistakes to have any success at all.

    Embedded systems do not take prisoners, they do not negotiate and they won't tell you a damn thing about where you screwed up.

    ok, but now i have to resolve this case.

  5. #50
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Whenever you see a * in your code, start paying attention.

    Like, answering questions such as
    - has it been initialised
    - how much memory is it pointing at
    - am what I am about to do with that memory OK from an array bounds perspective

    Just declaring a pointer, then using it for strcpy takes none of these questions into account, and you get hosed for it.
    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.

  6. #51
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Quote Originally Posted by sergioms View Post
    ok, but now i have to resolve this case.
    You do it by learning C.
    Step back. Get a book. Study the fundamentals. Mess around with PC software. Get the grap of all features.
    Done all that? OK. Now you can get back to your project.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #52
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    if i am use:

    Code:
    void EscreveDisplay (char pMensagem[17], unsigned int pDelaySec){
    
    // then call by:
    
    char cMsgtest[17];
    
    strcpy (cMsgtest, "LENKE AUTOMACAO");
    
    EscreveDisplay (cMsgtest);
    its compile, run but my program crash at next call to EscreveDisplay...

  8. #53
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    if i am use:

    Code:
    void EscreveDisplay (char pMensagem[17], unsigned int pDelaySec){
    
    // then call by:
    
    char cMsgtest[17];
    
    strcpy (cMsgtest, "LENKE AUTOMACAO");
    
    EscreveDisplay (cMsgtest);
    its compile, run but my program crash at next call to EscreveDisplay...
    Well, you do not need to specify [17] in the called function, just empty [] will do, or char *.

    I do not see why you need to change the code to use strcpy() at all here - that would be the right solution if you update the string that you are passing into EscreveDisplay - but in the code you posted most recently, you are not actually doing that, so it should work fine just passing "LENKE AUTOMACAO" as the input to EscreveDisplay() [obviously with the correct number of parameters].

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

Page 4 of 4 FirstFirst 1234
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