cod naspa

Acest forum este dedicat intrebarilor de programare care nu-si au locul in unul din celelalte forumuri
User avatar
bu7ch3r
Membru++
Membru++
Posts: 326
Joined: 17 May 2011, 15:17
Judet: Iaşi
Location: Sofia
Contact:

Re: cod naspa

Post by bu7ch3r » 25 Mar 2013, 13:59

Un coleg sturlubatic s-a gandit sa verifice pointerii mai riguros :))

Code: Select all

#include <stdio.h>

int foo(int *pPseudoPointer)
{
    int nRetVal= -1;
    if(sizeof(pPseudoPointer) > 0)
    {
        printf("The value is %d !", *pPseudoPointer);
        nRetVal= 0;
    }
    else
    {
        printf("NULL POINTER!!!!");
    }

    return nRetVal;
}

int main (void)
{
    int *pThePOINTER = NULL;
    foo(pThePOINTER);

    return 0;

}


Cu stima,
Lupu Claudiu

User avatar
Marius Bancila
Fondator
Fondator
Posts: 2343
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

Re: cod naspa

Post by Marius Bancila » 29 Mar 2013, 14:39

Am vazut multe lucruri tampite la viata mea, dar asta e in top. E clar ca omul habar nu are ce e aia pointer.
Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog

User avatar
bu7ch3r
Membru++
Membru++
Posts: 326
Joined: 17 May 2011, 15:17
Judet: Iaşi
Location: Sofia
Contact:

Re: cod naspa

Post by bu7ch3r » 04 Apr 2013, 22:52

Mi-am adus aminte de ceva epic:

Code: Select all

while(i != 0 &&
         i != 1 &&
         i != 2 &&
         .....
         i != 12)
{
.....
}
Cu stima,
Lupu Claudiu

User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: cod naspa

Post by Ovidiu Cucu » 04 Apr 2013, 23:26

Iata si una pentru "eliminat if-uri imbricate":

Code: Select all

   while(1)
   {
      if(conditie_1)
      {
         // do something...
         break;
      }
      if(conditie_2)
      {
         // do something...
         break;
      }

      // s.a.m.d.

      if(conditie_N)
      {
         // do something...
         break;
      }
      // si in final... cireasa de pe tort :)
      break;
   }

User avatar
bu7ch3r
Membru++
Membru++
Posts: 326
Joined: 17 May 2011, 15:17
Judet: Iaşi
Location: Sofia
Contact:

Re: cod naspa

Post by bu7ch3r » 05 Apr 2013, 00:22

Asta-i din automotive :))))) Cunosc :P
Cu stima,
Lupu Claudiu

User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: cod naspa

Post by Ovidiu Cucu » 02 Aug 2013, 14:09

Daca nu stiati inca, una dintre cele mai importante constante din Univers este MAX_PATH. :)

Code: Select all

    TCHAR pszText[MAX_PATH];
    ::GetWindowText(hWnd, pszText, MAX_PATH);

User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: cod naspa

Post by Ovidiu Cucu » 17 Aug 2013, 14:13

Scurt:

Code: Select all

    if(a > b)
        return ERROR_FILE_NOT_FOUND;

User avatar
Marius Bancila
Fondator
Fondator
Posts: 2343
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

Re: cod naspa

Post by Marius Bancila » 27 Aug 2013, 12:34

Code: Select all

pDBTable->iTblID = (long)iTableID; // iTblID si iTableID sunt amandoua int

Code: Select all

ptr->lGroupID = (long)iGroupID; // lGroupID e long, iGroupID e int

Code: Select all

INT foo::Fetch(BOOL)
{
   return (long) GetSomething(); // GetSomething returneaza long
}
Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog

User avatar
bu7ch3r
Membru++
Membru++
Posts: 326
Joined: 17 May 2011, 15:17
Judet: Iaşi
Location: Sofia
Contact:

Re: cod naspa

Post by bu7ch3r » 27 Aug 2013, 13:19

Threaduri pentru avansati :)
Daca omu a invatat recent mutexi ce s-a gandit - decat sa crape mai bine pun pentru orice variabila. Asta e doar o mica parte din cod.
Cel mai epic e ca seteaza variabila aia globala pe false la inceputul threadului - ca sa fie sigur ca ramne in join pana la adanci batraneti daca apeleaza terminate inainte de primul lock.

Code: Select all

bool_t      g_shouldThreadExit = False;

// Protects the access to g_shouldThreadExit 
static pthread_mutex_t                  un_mutex = PTHREAD_MUTEX_INITIALIZER;

ThreadRoutine
{ 
   bool_t      shoudlThreadExit = False;   

   pthread_mutex_lock(un_mutex);
       g_shouldThreadExit = False;
   pthread_mutex_unlock(un_mutex);   

    while(1)
    {
        [....COD.....]
        (void)usleep(delay);
        pthread_mutex_lock(un_mutex);
        shoudlThreadExit = g_shouldThreadExit 
        pthread_mutex_unlock(un_mutex);

        if (True == shoudlThreadExit)
        {            
            break;
        }
    }
}

void
ThreadTerminate(void)
{
    pthread_mutex_lock(un_mutex);
        g_shouldThreadExit = True;
    pthread_mutex_unlock(un_mutex);

    pthread_join(thread)
    pthread_mutex_destroy(un_mutex);
}

Cu stima,
Lupu Claudiu

User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: cod naspa

Post by Ovidiu Cucu » 03 Sep 2013, 19:32

Write some code... then take a break. :)

Code: Select all

        switch(CommDlgExtendedError())
        {
        case CDERR_DIALOGFAILURE:
            break;
        case CDERR_FINDRESFAILURE:
            break;
        case CDERR_INITIALIZATION:
            break;
        case CDERR_LOADRESFAILURE:
            break;
        case CDERR_LOADSTRFAILURE:
            break;
        case CDERR_LOCKRESFAILURE:
            break;
        case CDERR_MEMALLOCFAILURE:
            break;
        case CDERR_MEMLOCKFAILURE:
            break;
        case CDERR_NOHINSTANCE:
            break;
        case CDERR_NOHOOK:
            break;
        case CDERR_NOTEMPLATE:
            break;
        case CDERR_REGISTERMSGFAIL:
            break;
        case CDERR_STRUCTSIZE:
            break;
        }

User avatar
Silviu Ardelean
Senior
Senior
Posts: 1175
Joined: 12 Jul 2007, 09:22
Judet: Timiş
Location: Timisoara
Contact:

Re: cod naspa

Post by Silviu Ardelean » 07 Sep 2013, 10:54

Un critic literar al acestui forum, in una din putinele bucati personale de cod expuse online, paseaza o referinta unei functii dar nici dracu' nu modifica ceva in acel obiect.

Code: Select all

int foo(type_t& a)
{
  if(a.a == 1)
      return 1;
   else
      return 0;
}
Dupa, a aflat si el de la altii ca ar fi bine sa foloseasca calificatorul const.

florin_b
Junior
Junior
Posts: 8
Joined: 10 Jun 2013, 10:38
Judet: Bucureşti

Re: cod naspa

Post by florin_b » 09 Sep 2013, 13:16

Un fost coleg scria un program de redenumit fisiere si s-a gindit sa-l faca multithreading, ca sa mearga mai repede. In functia de OnClick a butonului care pornea redenumirea avea ceva de genul asta:

Code: Select all

HANDLE t = CreateThread(NULL, 0, RenameFiles, NULL, 0, NULL);
WaitForSingleObject(t, INFINITE);
Dupa aia umbla prin birou si se intreba de ce tot UI-ul aplicatiei e blocat pe parcursul operatiunii, ca doar el facuse pe thread-uri. :)) In profilul de linkedin are trecut ca-i expert in multithreading si PPL. :))

User avatar
Silviu Ardelean
Senior
Senior
Posts: 1175
Joined: 12 Jul 2007, 09:22
Judet: Timiş
Location: Timisoara
Contact:

Re: cod naspa

Post by Silviu Ardelean » 09 Sep 2013, 13:24

Daca nu i-a spus nimeni unde se puna asteptarea... :D
Nu sunt sigur ca avea nevoie de asteptare (fisierele puteau fi redenumite intr-un loop pe functia threadului daca tot l-a facut, iar dupa sa notifice GUI-ul) dar cum nu cunosc contextul si codul ma opresc aici.

User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: cod naspa

Post by Ovidiu Cucu » 06 Dec 2013, 14:56

In caz ca sistemul sufera de Alzheimer. :)

Code: Select all

    if(!::CreateProcess(...))
    {
        DWORD dwError = ::GetLastError();
        ::SetLastError(dwError);
    }

User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: cod naspa

Post by Ovidiu Cucu » 19 Jun 2015, 11:27

Infinite loop: see Loop, Infinite
Loop, Infinite: see Infinite loop

Code: Select all

    std::vector<Item*>::iterator iter = m_Item.begin();
    while(iter != m_Item.end())
    {
        if (!(*iter)->bVisible)
            continue;

        // doesn't matter...

        ++iter;
    }
Genial! :)

Post Reply