c - Is not passing all the arguments to a function bad? -


i've been experimenting 'dynamically calling functions' using source code below. after testing code testing_function accepting first 2 arguments, added in third , decided 'not supply argument' when call function. i've noticed when this, value of third argument not (necessarily) 0, 'random' value not know origin of.

questions follow:

  • where these values originating from?
  • additionaly, how arguments passed functions?
  • is bad practice not pass arguments?
  • can 1 prepared additions function's arguments without recompiling code utilizing function? (example: dynamically loaded library's function gains accepted argument code utilizing function isn't going recompiled).

foreword source code follows:

i running using linux, compiling/calling linker gcc 4.6.3, , receive no compilation/linking warnings/errors when utilizing code. code executes 'perfectly'. call gcc following:

gcc -x c -ansi -o (output file) (input file, .c suffix) 

source code follows:

#include <errno.h> #include <stdio.h> #include <stdlib.h>  /* function testing. */ int testing_function(char* something, char* somethingelse, int somethingadditional) {     int alt_errno = errno;     if ((something != null)&&(somethingelse != null))     {         errno = 0;         if (fprintf(stdout, "testing_function(\"%s\", \"%s\", %d);\n", something, somethingelse, somethingadditional) <= 0)         {             if (errno != 0)             {                 int alt_alt_errno = errno;                 perror("fprintf(stdout, \"testing_function(\\\"%%s\\\", \\\"%%s\\\", %%d);\\n\", something, somethingelse, somethingadditional)");                 errno = alt_errno;                 return alt_alt_errno;             }             else             {                 errno = enosys;                 perror("fprintf(stdout, \"testing_function(\\\"%%s\\\", \\\"%%s\\\", %%d);\\n\", something, somethingelse, somethingadditional)");                 errno = alt_errno;                 return enosys;             }         }         else         {             errno = alt_errno;             return 0;         }     }     else     {         errno = enosys;         perror("testing_function(char* something, char* somethingelse, int somethingadditional)");         errno = alt_errno;         return enosys;     } }  /* main function. */ int main(int argc, char** argv) {     int (*function)(char*, char*);     *(void**) (&function) = testing_function;     exit(function("hello", "world!")); } 

function parameters passed depending on c abi used compiler. can mean passed on stack or in registers or in combination of both. believe 32-bit intel systems commonly pass in stack while 64-bit intel pass in registers overflow going on stack.

where random values unpassed arguments come from? come register or stack position should have held value. called function not know argument wasn't passed pulls anyway.

if of arguments supposed on stack can lead bad problems because function pull off more stack items exist. in worst case wipe out function return address.

when using registers isn't of problem except random value.

from above information should able gather isn't supported , shouldn't , in general won't work.

what will work variable argument lists. example, printf it. open() posix function. open declaration looks following:

extern int open (__const char *__file, int __oflag, ...); 

see triple dot? declares variable argument list. can contain 0 number of arguments. accessed using special functions. way know how many arguments expect 1 of previous arguments. in case of open(), oflag value. printf() format string.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -