argv 内存分配

发布于 2024-08-28 13:37:00 字数 1714 浏览 7 评论 0原文

我想知道是否有人可以告诉我我做错了什么,我收到此 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 );
}

谢谢你,

I was wondering if someone could tell me what I am doing wrong that I get this Unhandled Exception error message:

0xC0000005: Access violation reading location 0x0000000c.

with a green pointer pointing at my first Prolog code (fid_t):

Here is my header file:

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( );
};

And here is my main code:

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 );
}

Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-09-04 13:37:00

这部分很糟糕:

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

argv 是一个悬空指针 - 没有分配存储空间。改成这样:

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

This part here is nasty:

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

argv is a dangling pointer - no storage has been allocated. Change it to this:

char* argv[2];
argv[ 0 ] = "libpl.dll";
argv[ 1 ] = NULL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文