Code: Show/Hide void botInfo::handleGuess(Player *p, char *msg) ... if (msg[0] == '`' || msg[0] == '-' || msg[0] == ',') { char *s = &msg[1]; if (*s == NULL) return; delete [0]msg; msg = s; } ... |
Code: Show/Hide void ParentFunc ()
{ char *msg; char buffer[256]; // put message into buffer msg = buffer; if (*msg == '`' || *msg == '-' || *msg == ',') msg++; // now use msg as you like } |
Code: Show/Hide switch (msg[0])
{ case '`': case '-': case ',': strcpy(msg, msg + 1); break; } |
Cyan~Fire wrote: |
I don't understand what's so bad about an early return. It only translates into a goto, with less code than using an if statement or something, |
Code: Show/Hide void Func ()
{ if (!(file = fopen(...))) return; if (!(mem = malloc(...))) return; // <-- bug if (something) { // blah blah return; // <--- bug } if (something else) { // blah blah fclose(file); // <-- wasted code, messy free(mem); return; } // blah blah fclose(file); free(mem); } |
Code: Show/Hide void Func ()
{ file = 0; mem= 0; if (file = fopen(...)) { if (mem = malloc(...)) { if (something) { // blah blah goto Exit; } if (something else) { // blah blah goto Exit; } // blah blah } } Exit: if (file) fclose(file); if (mem) free(mem); } |
CypherJF wrote: |
Reading the Goto; and return things on MSDN, they said to use break, return, and all other jump statements before using a goto... :/
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/statem_19.asp I think if I read that right... |
Code: Show/Hide void * __cdecl _heap_alloc_base (size_t size)
{ #ifdef WINHEAP void * pvReturn; #else /* WINHEAP */ _PBLKDESC pdesc; _PBLKDESC pdesc2; #endif /* WINHEAP */ #ifdef WINHEAP if ( __active_heap == __V6_HEAP ) { if ( size <= __sbh_threshold ) { #ifdef _MT _mlock( _HEAP_LOCK ); __try { #endif /* _MT */ pvReturn = __sbh_alloc_block(size); #ifdef _MT } __finally { _munlock( _HEAP_LOCK ); } #endif /* _MT */ if (pvReturn) return pvReturn; } } else if ( __active_heap == __V5_HEAP ) { /* round up to the nearest paragraph */ if ( size ) size = (size + _OLD_PARASIZE - 1) & ~(_OLD_PARASIZE - 1); else size = _OLD_PARASIZE; if ( size <= __old_sbh_threshold ) { #ifdef _MT _mlock(_HEAP_LOCK); __try { #endif /* _MT */ pvReturn = __old_sbh_alloc_block(size >> _OLD_PARASHIFT); #ifdef _MT } __finally { _munlock(_HEAP_LOCK); } #endif /* _MT */ if ( pvReturn != NULL ) return pvReturn; } return HeapAlloc( _crtheap, 0, size ); } if (size == 0) size = 1; size = (size + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1); return HeapAlloc(_crtheap, 0, size); } #else /* WINHEAP */ /* try to find a big enough free block */ if ( (pdesc = _heap_search(size)) == NULL ) { if ( _heap_grow(size) != -1 ) { /* try finding a big enough free block again. the * success of the call to _heap_grow should guarantee * it, but... */ if ( (pdesc = _heap_search(size)) == NULL ) { /* something unexpected, and very bad, has * happened. abort! */ _heap_abort(); } } else return NULL; } /* carve the block into two pieces (if necessary). the first piece * shall be of the exact requested size, marked inuse and returned to * the caller. the leftover piece is to be marked free. */ if ( _BLKSIZE(pdesc) != size ) { /* split up the block and free the leftover piece back to * the heap */ if ( (pdesc2 = _heap_split_block(pdesc, size)) != NULL ) _SET_FREE(pdesc2); } /* mark pdesc inuse */ _SET_INUSE(pdesc); /* check proverdesc and reset, if necessary */ _heap_desc.proverdesc = pdesc->pnextdesc; return( (void *)((char *)_ADDRESS(pdesc) + _HDRSIZE) ); } |