c - How does Stack Activation Frame for Variable arguments functions works? -


i reading http://www.cs.utexas.edu/users/lavender/courses/cs345/lectures/cs345-lecture-07.pdf try understand how stack activation frame variable arguments functions works?

specifically how can called function knows how many arguments being passed?

the slide said: the va_start procedure computes fp+offset value following argument past last known argument (e.g., const char format). rest of arguments computed calling va_arg, ‘ap’ argument va_arg fp+offset value.*

my question fp (frame point)? how va_start computes 'fp+offset' values? , how va_arg 'some fp+offset values? , va_end supposed stack?

the function doesn't know how many arguments passed. @ least not in way matters, i.e. in c cannot query number of arguments.

that's why varargs functions must either:

  • use non-varying argument contains information number , types of variable arguments, printf() does; or
  • use sentinel value on end of variable argument list. i'm not aware of function in standard library this, see instance gtk+'s gtk_list_store_set() function.

both mechanisms risky; if printf() format string doesn't match arguments, you're going undefined behavior. if there way printf() know number of passed arguments, of course protect against this, there isn't. can't.

the va_start() macro takes argument last non-varying argument, can somehow (this compiler internals, there's no single correct or standard answer, can side of interface reason available data) use know first varying argument located on stack.

the va_arg() macro gets type "argument", makes possible use compute offset, , increment state in va_list object point @ next varying argument.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -