argv内存分配

时间:2010-04-10 20:00:35

标签: visual-studio-2008 swi-prolog

我想知道是否有人可以告诉我我做错了什么我收到此Unhandled Exception错误消息:

0xC0000005: Access violation reading location 0x0000000c.

用绿色指针指向我的第一个Prolog代码(fid_t):

这是我的头文件:

class UserTaskProlog
{
  public:
             UserTaskProlog( ArRobot* r );
             ~UserTaskProlog( );
  protected:
             int cycles;
             char* argv[ 1 ];
             term_t tf;
             term_t tx;
             term_t goal_term;
             functor_t goal_functor;
             ArRobot* robot;
             void logTask( );
};

这是我的主要代码:

UserTaskProlog::UserTaskProlog( ArRobot* r ) : robot( r ), robotTaskFunc( this, &UserTaskProlog::logTask )
{
  cycles = 0;
  argv[ 0 ] = "libpl.dll";
  argv[ 1 ] = NULL;
  PL_initialise( 1, argv );
  PlCall( "consult( 'myPrologFile.pl' )" );
  robot->addSensorInterpTask( "UserTaskProlog", 50, &robotTaskFunc );
}

UserTaskProlog::~UserTaskProlog( )
{
  robot->remSensorInterpTask( &robotTaskFunc );
}

void UserTaskProlog::logTask( )
{
  cycles++;

  fid_t fid = PL_open_foreign_frame( );

    tf = PL_new_term_ref( );
    PL_put_integer( tf, 5 );
    tx = PL_new_term_ref( );
    goal_term = PL_new_term_ref( );
    goal_functor = PL_new_functor( PL_new_atom( "factorial" ), 2 );
    PL_cons_functor( goal_term, goal_functor, tf, tx );
    int fact;
    if ( PL_call( goal_term, NULL ) )
    {
      PL_get_integer( tx, &fact );
      cout << fact << endl;
    }

  PL_discard_foreign_frame( fid );

}

int main( int argc, char** argv )
{
  ArRobot robot;
  ArArgumentParser argParser( &argc, argv );
  UserTaskProlog talk( &robot );
}

谢谢,

1 个答案:

答案 0 :(得分:2)

这部分令人讨厌:

char** argv;
argv[ 0 ] = "libpl.dll";
argv[ 1 ] = NULL;

argv是一个悬空指针 - 没有分配存储空间。将其更改为:

char* argv[2];
argv[ 0 ] = "libpl.dll";
argv[ 1 ] = NULL;