[solved]Cva_Arg() never returns NULL ?

General FreeBASIC programming questions.
Post Reply
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

[solved]Cva_Arg() never returns NULL ?

Post by D.J.Peters »

After the address of "a,"b" and "c" on stack arg will be any other address of stack space but not NULL ?
(Maybe I use it wrong in this case please show me the right code)

Joshy

Code: Select all

sub test cdecl(byref first as string,...)
  dim as zstring ptr arg   
  dim as Cva_List list
  Cva_Start(list, first)
  arg=strptr(first)  
  while arg
    print arg,*arg,"press any key"
    arg = Cva_Arg(list, Any Ptr)  
    sleep
  wend          
end sub
test("a", "b", "c")
sleep
Last edited by D.J.Peters on Jul 16, 2022 19:51, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Cva_Arg() never returns NULL ?

Post by D.J.Peters »

from the wiki example

Code: Select all

do
  pn = Cva_Arg(args, Any Ptr)
  If pn = 0 Then Exit Do
  ...
loop
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Cva_Arg() never returns NULL ?

Post by coderJeff »

D.J.Peters wrote: Jul 16, 2022 19:24 from the wiki example

Code: Select all

do
  pn = Cva_Arg(args, Any Ptr)
  If pn = 0 Then Exit Do
  ...
loop
Yes, any garbage might be on the stack. Make a terminal in the caller like NULL aka zero (0), to know when to stop:

Code: Select all

sub test cdecl(byref first as string,...)
  dim as zstring ptr arg   
  dim as Cva_List list
  Cva_Start(list, first)
  arg=strptr(first)  
  while arg
    print arg,*arg,"press any key"
    arg = Cva_Arg(list, Any Ptr)  
  wend          
end sub
test( "a", "b", "c", 0 ) '' <<<< ---- NULL (0) stops the list
sleep
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [solved]Cva_Arg() never returns NULL ?

Post by fxm »

So see the Programmer' Guide page: Variadic Arguments

Extract from this page:
.....
The fixed parameters(s) can provide information about how many variadic arguments there are, by an unspecified mechanism.
Otherwise a terminal argument can be added in the variable length argument list, but this reserves a special argument value forbidden to the useful variadic arguments.
(if one choose to pass the variable arguments all by pointer, in this case an obvious terminal argument is the null pointer)
.....
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [solved]Cva_Arg() never returns NULL ?

Post by fxm »

For a proper code, don't forget to call Cva_End to conclude a Cva_Start (or Cva_Copy)

Extract from Variadic Arguments:
.....
Cva_Start (or Cva_Copy) is like a constructor (or a copy-constructor) for the variadic argument_list object and must finally have a matching call to Cva_End, which is like a destructor.
After Cva_End for argument_list has been called, argument_list can be reused and reinitialized with another call to Cva_Start (or Cva_Copy).
The Cva_Start (or Cva_Copy) and Cva_End calls must both be called in pairs in the same procedure (for cross platform compatibility).
.....
See the examples.
Post Reply