Author |
Message |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
Posted: Sun Jul 04, 2004 4:04 am Post maybe stupid Post subject: |
 |
|
|
|
im assuming that you allocated memory for whatever *msg is pointing to. Also I have never deleted memory like that, and i dont think you can. Why not just ignore the first character and just do
char *s= msg;
if(blah)
{
s++;
}
i dont like using returns except at the end of functions and never in voids, it will work though
To my knowledge, there are only 2 ways to delete memory. delete blah; and delete [] blah2; , the [] are for arrays |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Sun Jul 04, 2004 4:15 am Post maybe stupid Post subject: |
 |
|
|
|
thanks XD |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sun Jul 04, 2004 10:37 am Post maybe stupid Post subject: |
 |
|
|
|
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, _________________ This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him. |
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
Posted: Sun Jul 04, 2004 12:23 pm Post maybe stupid Post subject: |
 |
|
|
|
They make code confusing to read, a return in the middle of a function translates into a return from subrutine, it pops the program counter+some other stuff off the stack and loads them into thier places. All a goto is, is a memory address to jump to, i suppose it takes the location of where the program starts and adds it to the offset it wants to go to or it takes it's locatio n and adds a number to the program counter to move forward/back. |
|
Back to top |
|
 |
Mine GO BOOM Hunch Hunch What What

Age:42 Gender: Joined: Aug 01 2002 Posts: 3615 Location: Las Vegas Offline
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Sun Jul 04, 2004 2:26 pm Post maybe stupid Post subject: |
 |
|
|
|
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, |
If you get into the habit of early returns, you start producing bugs. It also means for messy hard-to-read code.
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);
} |
I never use a return in the middle of a function, ever. I would write the above like this...
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);
} |
All the cleanup in a single place. |
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Sun Jul 04, 2004 4:44 pm Post maybe stupid Post subject: |
 |
|
|
|
But if you didn't fopen and/or want to allocate memory; more or less if the function pre conditions fail, can't you without issue do a premature return?
ie:
int blah(int x) { if (x == 0) return; return 1/x; }
Last edited by CypherJF on Sun Jul 04, 2004 8:16 pm, edited 2 times in total |
|
Back to top |
|
 |
Jackmn Newbie
Joined: Apr 02 2004 Posts: 13 Offline
|
Posted: Sun Jul 04, 2004 5:31 pm Post maybe stupid Post subject: |
 |
|
|
|
Better:
return x ? 1/x : 0;
( Probably with a comment stating what it does, since that is a little ugly ) |
|
Back to top |
|
 |
-Smong- Guest
Offline
|
Posted: Sun Jul 04, 2004 6:16 pm Post maybe stupid Post subject: |
 |
|
|
|
@Cyan~Fire
A goto is a jump and an in most cases an if statement is a subtraction and a conditional jump (ok it's less code, but do you want better readability?).
@50% Packetloss
If you think an early return increases file size because of the stack cleanup, I would imagine a sensible compiler would jump to the end of the subroutine so the cleanup code is shared (may depend on compile flags and the length of the jump).
@Mr Ekted
Why use 'goto Exit' over 'else if'? Should goto be avoided where ever possible (porting to another language could be awkward)? |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sun Jul 04, 2004 6:24 pm Post maybe stupid Post subject: |
 |
|
|
|
Alright, I see your point.
And why would you ever want to port a C++ program?  |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Sun Jul 04, 2004 6:24 pm Post maybe stupid Post subject: |
 |
|
|
|
If you use early return in functions where they are ok, you will end up coming back later and adding code to break them. It's just a bad habit. Even though I am in favor of people using their own coding styles for their own files, I refuse to allow early returns for any project that I am in charge of.
If-else works only in very simple cases. My example above uses "blah blah" to show were much more code could break if-else possibilities. There are always cases where you have to use goto, or some crazy crap like while loops with special exit conditions (or breaks), which makes the code even more unreadable.
If you write your stuff in C to begin with, then porting to another language is never an issue, since you started with the most sensible one. |
|
Back to top |
|
 |
-Smong- Guest
Offline
|
Posted: Sun Jul 04, 2004 6:50 pm Post maybe stupid Post subject: |
 |
|
|
|
I know a couple of bytecode languages that don't support goto, I guess that fits in with 'I am in favor of people using their own coding styles for their own files' as people have preferred languages and target platforms too. |
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
|
Back to top |
|
 |
Jackmn Newbie
Joined: Apr 02 2004 Posts: 13 Offline
|
Posted: Mon Jul 05, 2004 8:54 am Post maybe stupid Post subject: |
 |
|
|
|
It's far better to use a goto when cleanup needs to be done prior to the return, which is true for almost everything except small utility functions. |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Mon Jul 05, 2004 2:14 pm Post maybe stupid Post subject: |
 |
|
|
|
Ever wonder why MS apps are so buggy? |
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Mon Jul 05, 2004 4:12 pm Post maybe stupid Post subject: |
 |
|
|
|
Could be  |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Mon Jul 05, 2004 4:19 pm Post maybe stupid Post subject: |
 |
|
|
|
Look at some of the run-time and MFC source from VC sometimes. Their code is terrible. |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Mon Jul 05, 2004 4:27 pm Post maybe stupid Post subject: |
 |
|
|
|
Here's a function from inside malloc.c. Imagine someone telling you they think there's a bug in it, and to figure it out...
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) );
} |
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Mon Jul 05, 2004 4:42 pm Post maybe stupid Post subject: |
 |
|
|
|
I have to admit that the MFC source is better than their CRT source, even though the compiled product is crappier.
And, of course, MFC has the added feature of people not knowing what the heck is actually happening behind the scene. "What, this program's in a loop? Oh." |
|
Back to top |
|
 |
|