[ADMB Users] How to use Autodif as a regular external C++ library?

dave fournier davef at otter-rsch.com
Mon Nov 5 07:44:48 PST 2012


On 12-11-04 05:41 PM, Pedro Tabacof wrote:
> Dave, thanks a lot for your help. Indeed it was necessary to use new 
> and delete instead of malloc, now my code is working just as I 
> intended (in the end I used only classes to make the code more 
> elegant). May I ask you two more questions? They are not essential but 
> nonetheless important for a better code.
>
> 1) What do you think is the best way to read a dvariable value from a 
> file, if the value has been stored as a double? Right now I'm reading 
> it to a regular double variable and then casting it to a dvariable.

check out ifstream and cifstream.

with say

        cifstream cif("filename");


      dvariable x;

      cif >> x;

will read in x


>
> 2) This has been puzzling me: sizeof(dvariable) return 4, while 
> sizeof(double) returns 8, why is dvariable size smaller than double?
>


a dvariable contains a pointer which is 4 bytes onyour 32 bit compiler.
> Pedro.
>
> On Sun, Nov 4, 2012 at 10:02 PM, dave fournier <davef at otter-rsch.com 
> <mailto:davef at otter-rsch.com>> wrote:
>
>
>     I was curious to see if you needed to change struct to class.
>     It appears not. But you do need to use new and delete
>     as this simple example shows.
>
>
>     #include <fvar.hpp>
>
>     struct mys
>     {
>       dvariable x;
>     };
>
>     main()
>     {
>       gradient_structure gs(1000000);
>
>       // this will work
>       mys u;
>       u.x=5;
>       cout << u.x << endl;
>
>       // this will work
>       mys * pu2 = new mys;
>       pu2->x=7;
>       cout << pu2->x << endl;
>       delete pu2;
>       pu2=0;
>
>       // this will crash
>       mys * pu=(mys*) malloc(sizeof(mys));
>       pu->x=6;
>       cout << pu->x << endl;
>
>     }
>
>     _______________________________________________
>     Users mailing list
>     Users at admb-project.org <mailto:Users at admb-project.org>
>     http://lists.admb-project.org/mailman/listinfo/users
>
>
>
>
> -- 
> Pedro Tabacof,
> Unicamp - Eng. de Computação 08.
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.admb-project.org/pipermail/users/attachments/20121105/41ef8298/attachment.html>


More information about the Users mailing list