S
Snis Pilbor
Howdy,
Forgive me for not looking this up in the manuals or just trying
it outright, as I don't have access to a shell right now. Anyway, I
thought of this while juggling earlier: it would be neat if there were
a way to do a "sizeof" on the total set of declarations at the top of a
given function. For example:
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char **argv )
{
char a;
char b;
int x,y,z;
printf( "The 'main' function declares %d bytes of data.\n\r", sizeof(
*main ) );
return 0;
}
Why would this be useful, you ask? Think nested recursion. Often,
programmers will clumsily use a semi-magical number to limit the max
depth before a recursion will be forcefully halted with an error.
Compare the following (you'll forgive the "code fragment" nature here,
since I'm using made-up syntax in the 2nd version anyway, there's
little point being pedantic elsewhere) (and yes, I know these use more
memory than needed but I'm doing that on purpose to illustrate my
point):
int my_recursive_fnc( int n )
{
static int depth;
int x,y,z,answer;
if ( ++depth > MY_MAGIC_NUMBER )
{
printf( "Fatal: recursion nest too deep\n\r" );
exit(0);
}
x = do_some_stuff( &n, &y, &z );
answer = my_recursive_fnc( n + x + y + z );
depth--;
return answer;
}
versus:
int my_recursive_fnc( int n )
{
extern int recursion_bytes;
int x,y,z,answer;
recursion_bytes += sizeof( *my_recursive_fnc );
if ( recursion_bytes > sysdata.max_space_for_recursion )
{
printf( "Fatal: recursion exceeded user-defined RAM limit\n\r" );
exit(0);
}
x = do_some_stuff( &n, &y, &z );
answer = my_recursive_fnc( n + x + y + z );
recursion_bytes -= sizeof( *my_recursive_fnc );
return answer;
}
In the 2nd version, instead of a magical hardcoded nest limit, we have
a realtime user-defined RAM limit which gives more leeway to more
efficient recursive functions and can nicely track recursion memory
throughout interwoven recursive functions that call eachother. This
could be done by manually adding the sizeof's of all the declared
variables as well as the passed-in arguments, but that would be a
maintainance and readability nightmare, and might even skip over
padding.
Forgive me for not looking this up in the manuals or just trying
it outright, as I don't have access to a shell right now. Anyway, I
thought of this while juggling earlier: it would be neat if there were
a way to do a "sizeof" on the total set of declarations at the top of a
given function. For example:
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char **argv )
{
char a;
char b;
int x,y,z;
printf( "The 'main' function declares %d bytes of data.\n\r", sizeof(
*main ) );
return 0;
}
Why would this be useful, you ask? Think nested recursion. Often,
programmers will clumsily use a semi-magical number to limit the max
depth before a recursion will be forcefully halted with an error.
Compare the following (you'll forgive the "code fragment" nature here,
since I'm using made-up syntax in the 2nd version anyway, there's
little point being pedantic elsewhere) (and yes, I know these use more
memory than needed but I'm doing that on purpose to illustrate my
point):
int my_recursive_fnc( int n )
{
static int depth;
int x,y,z,answer;
if ( ++depth > MY_MAGIC_NUMBER )
{
printf( "Fatal: recursion nest too deep\n\r" );
exit(0);
}
x = do_some_stuff( &n, &y, &z );
answer = my_recursive_fnc( n + x + y + z );
depth--;
return answer;
}
versus:
int my_recursive_fnc( int n )
{
extern int recursion_bytes;
int x,y,z,answer;
recursion_bytes += sizeof( *my_recursive_fnc );
if ( recursion_bytes > sysdata.max_space_for_recursion )
{
printf( "Fatal: recursion exceeded user-defined RAM limit\n\r" );
exit(0);
}
x = do_some_stuff( &n, &y, &z );
answer = my_recursive_fnc( n + x + y + z );
recursion_bytes -= sizeof( *my_recursive_fnc );
return answer;
}
In the 2nd version, instead of a magical hardcoded nest limit, we have
a realtime user-defined RAM limit which gives more leeway to more
efficient recursive functions and can nicely track recursion memory
throughout interwoven recursive functions that call eachother. This
could be done by manually adding the sizeof's of all the declared
variables as well as the passed-in arguments, but that would be a
maintainance and readability nightmare, and might even skip over
padding.