<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="pl-pl">
<link rel="self" type="application/atom+xml" href="https://forum.atnel.pl/feed.php?f=46&amp;t=3108&amp;mode" />

<title>ATNEL tech-forum</title>
<link href="https://forum.atnel.pl/index.php" />
<updated>2013-05-14T21:24:22+01:00</updated>

<author><name><![CDATA[ATNEL tech-forum]]></name></author>
<id>https://forum.atnel.pl/feed.php?f=46&amp;t=3108&amp;mode</id>
<entry>
<author><name><![CDATA[Krauser]]></name></author>
<updated>2013-05-14T21:24:22+01:00</updated>
<published>2013-05-14T21:24:22+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=3108&amp;p=36956#p36956</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=3108&amp;p=36956#p36956"/>
<title type="html"><![CDATA[Re: Problem z dynamiczną alokacją pamięci struktury]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=3108&amp;p=36956#p36956"><![CDATA[
<div class="quotetitle">toch88 napisał(a):</div><div class="quotecontent"><br />(PRZED wywołaniem CreateChild)<br />tprs-&gt;string[0]=&quot;Silnik 3,5&quot;<br />tprs-&gt;string[1]=&quot;Silnik 5,25&quot;<br />tprs-&gt;string[2]=&quot;Silnika 7,45&quot;<br /></div><br />Już pierwszy string ma 10 znaków doliczając '\0' wychodzi 11 bajtów.<br />Funkcja do poprawki:<br /><div class="quotetitle">toch88 napisał(a):</div><div class="quotecontent"><br />[syntax=c]void createChild(TPARSER *tprs, const char *kryterium){<br />         uint8_t i;<br />         char temp&#91;10&#93;; //-------------tylko 9 znaków<br />        tprs-&gt;child = (struct TPARSER**)malloc (sizeof(TPARSER));   //dynamiczna allokacja pamieci     <br />        i=0;<br />        while(i&lt;tprs-&gt;ElementsOfTab){  <br />                tprs-&gt;child&#91;i&#93;=(struct TPARSER*)malloc (sizeof(TPARSER));<br />                strcpy(temp,tprs-&gt;string&#91;i&#93;);          //--------temp musi być na tyle duże by pomieścić tprs-&gt;string&#91;i&#93;, bo nie jest to sprawdzane<br />                initParser(tprs-&gt;child&#91;i&#93;, temp,kryterium);                            <br />                i++;<br />        }     <br />        tprs-&gt;nbOfChild=i;  <br />}[/syntax]<br /></div><br />Właściwie to zmienna temp nie jest wcale potrzebna, bo i tak ginie po zakończeniu działania funkcji. Dlaczego nie może być tak:<br />[syntax=c]void createChild(TPARSER *tprs, const char *kryterium){<br />        uint8_t i;<br />        tprs-&gt;child = (struct TPARSER**)malloc (sizeof(TPARSER));   //dynamiczna allokacja pamieci     <br />        i=0;<br />        while(i&lt;tprs-&gt;ElementsOfTab){  <br />                tprs-&gt;child&#91;i&#93;=(struct TPARSER*)malloc (sizeof(TPARSER));<br />                initParser(tprs-&gt;child&#91;i&#93;, tprs-&gt;string&#91;i&#93;, kryterium);                            <br />                i++;<br />        }     <br />        tprs-&gt;nbOfChild=i;  <br />}[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=465">Krauser</a> — 14 maja 2013, o 21:24</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[toch88]]></name></author>
<updated>2013-05-13T19:28:03+01:00</updated>
<published>2013-05-13T19:28:03+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=3108&amp;p=36852#p36852</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=3108&amp;p=36852#p36852"/>
<title type="html"><![CDATA[Problem z dynamiczną alokacją pamięci struktury]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=3108&amp;p=36852#p36852"><![CDATA[
Witam<br /><br />Mam dosyć spory problem z parsowaniem tekstu, mianowicie użytkownik może w terminalu wpisać sobie taką o to składnie.<br /><br /><strong>Silnik 3,5;Silnik 5,25;Silnika 7,45;</strong><br /><br />Ma to podzielić na &quot;Silnik 3,5&quot; , &quot;Silnika 5,25&quot;  itp;<br /><br /><br />Robię to w następujący sposób:<br />-gdy z USARTa przychodzi ciąg znaków zakończonych znakiem '\0' wychodzi do przerwania gdzie tworzona jest tablica (buforRx) przesłanych znaków, przekazywanych dalej do pętli głównej:<br /><br />[syntax=c]extern unsigned char buforRx&#91;&#93;;<br />extern unsigned char bufRxIndex; <br />extern bool odebranoDane;<br />#define SIZE_RX_BUF 100<br />void USART1_IRQHandler(void){<br /><br />  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  {<br /> buforRx&#91;bufRxIndex&#93;=USART_ReceiveData(USART1);<br /> if(buforRx&#91;bufRxIndex&#93;==0x0d){   <br /> odebranoDane=TRUE;<br />while(bufRxIndex&lt;SIZE_RX_BUF){<br />  buforRx&#91;bufRxIndex&#93;=0;<br />  bufRxIndex++;<br />}<br />bufRxIndex=0; <br /> }  <br /> else{<br />    bufRxIndex++;<br /> }<br /> if(bufRxIndex&gt;SIZE_RX_BUF-1){<br /> bufRxIndex=0;<br /> } <br />  }<br />}[/syntax]<br /><br />W pętli głównej obsługa tego wygląda tak:<br /><strong> MAIN </strong><br />[syntax=c]if(odebranoDane) {<br /><br /> tprs=(struct TPARSER*)malloc (sizeof(TPARSER));<br />initParser(tprs, buforRx,&quot;;&quot;);<br />createChild(tprs,&quot; &quot;);<br />freeStructure(tprs);<br />odebranoDane=FALSE;<br /><br />  }[/syntax]<br /><br />Po krótce dzięki temu kodowi bd miał:<br /><br />(PRZED wywołaniem CreateChild)<br />tprs-&gt;string[0]=&quot;Silnik 3,5&quot;<br />tprs-&gt;string[1]=&quot;Silnik 5,25&quot;<br />tprs-&gt;string[2]=&quot;Silnika 7,45&quot;<br /><br />wszystko ładnie pięknie teraz tworze &quot;dzieci&quot;<br /><br />(Wywołanie createChild)<br /><br />na podstawie każdego ze stringów dzielę wq kryterium &quot; &quot;:<br />po takim sparsowaniu mam:<br /><br />tprs-&gt;child[0]-&gt;string[0]=&quot;Silnik&quot;<br />tprs-&gt;child[0]-&gt;string[1]=&quot;3,5&quot;<br /><br />tprs-&gt;child[1]-&gt;string[0]=&quot;Silnik&quot;<br />tprs-&gt;child[1]-&gt;string[1]=&quot;5,25&quot;<br /><br />Dobrze tak wyglądają funkcje:<br /><strong> Inicjująca </strong><br />[syntax=c]void initParser(TPARSER *tprs,char *wsk, const char *kryterium){<br />uint8_t i;<br />i=0;<br /><br />  tprs-&gt;string&#91;i&#93; = strtok (wsk,kryterium); // tworzę tokeny według przyjętego kryterium // 0 token<br /><br />while( tprs-&gt;string&#91;i&#93; != NULL ){ <br />  <br />    i++;  // zwiększ wskaźnik tokenów <br />tprs-&gt;string&#91;i&#93; = strtok (NULL, kryterium); // przyporządkuj następny token        <br />   <br />    }<br /><br />tprs-&gt;ElementsOfTab=i;  <br />   <br />}[/syntax]<br /><br /><strong> Tworząca dzieci </strong><br /><br />[syntax=c]void createChild(TPARSER *tprs, const char *kryterium){<br /> uint8_t i;<br /> char temp&#91;10&#93;;<br />tprs-&gt;child = (struct TPARSER**)malloc (sizeof(TPARSER));   //dynamiczna allokacja pamieci<br />i=0; <br />while(i&lt;tprs-&gt;ElementsOfTab){<br />tprs-&gt;child&#91;i&#93;=(struct TPARSER*)malloc (sizeof(TPARSER));<br />strcpy(temp,tprs-&gt;string&#91;i&#93;);<br />initParser(tprs-&gt;child&#91;i&#93;, temp,kryterium);<br />i++;<br />}<br /><br />tprs-&gt;nbOfChild=i;<br /><br />}[/syntax]<br /><br /><br />Teraz pytanie w jaki sposób zwolnić tą pamięć przed przybyciem paczki do parsowania?  Mam napisane coś takiego ale niestety <strong>HardFault_Handler</strong><br />[syntax=c]void freeStructure(struct TPARSER *tprs){<br />uint8_t i;<br />i=0; <br /><br />if(hasChild(tprs)){<br />USART_PutString(USART1, &quot;Czyszczenie dzieci\r&quot;);<br />while(tprs-&gt;child&#91;i&#93;!=NULL){<br />   freeStructure(tprs-&gt;child&#91;i&#93;);<br />                   tprs-&gt;child&#91;i&#93;<br />                   i++;<br />}<br /><br />}<br />else{<br />USART_PutString(USART1, &quot;Czyszczenie dzieci else \r&quot;);<br />while(tprs!=NULL){<br />   free(tprs);<br />   tprs=NULL;<br />  }<br />}<br />}[/syntax]<br /><br />tak wygląda deklaracja structury<br />[syntax=c]typedef struct TPARSER{<br />uint8_t ElementsOfTab;<br />uint8_t LoopNb;<br />uint8_t nbOfChild;<br />uint8_t childId;<br />uint8_t thisId;<br />char *string&#91;10&#93;;<br />struct TPARSER **child;<br /><br />}TPARSER;[/syntax]<br />Bardzo proszę o pomoc naprawdę próbowałem wielu rzeczy, ale może mój zamysł w ogóle jest niepoprawny stąd te ceregiele. Wydaje mi się że to sprawa źle zwalnianych wskaźników bo w pewnych przypadkach program działa tzn kilka razy odbierze i przerobi polecenia, a np za 3 -4 się wysypie ;/ <br /><br />Pozdrawiam.<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=1138">toch88</a> — 13 maja 2013, o 19:28</p><hr />
]]></content>
</entry>
</feed>