首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python :异常:读取0x00000000的访问冲突

Python :异常:读取0x00000000的访问冲突
EN

Stack Overflow用户
提问于 2019-01-25 07:11:50
回答 1查看 8K关注 0票数 0

我试图用ctype使用python的DLL文件,但是遇到了

OSError:异常:访问冲突读取0x00000000

守则是:

代码语言:javascript
复制
import ctypes as ctypes
NS = ctypes.oledll.LoadLibrary ("D:\\XXX\\nsga2.dll")
NS.Initialise(360,2,2,5,0,5,2)
NS.Randomise()

在DLL文件中,函数Randomise()定义如下:

代码语言:javascript
复制
ADDAPI  void
ADDCALL Randomise(){
randomize();
initialize_pop (parent_pop);
ngen = 0;}

函数randomize()定义为:

代码语言:javascript
复制
 # include "rand.h"
   double seed;
   double oldrand[55];
   int jrand;
   void randomize()
   {
       int j1;
       for(j1=0; j1<=54; j1++)
       {
          oldrand[j1] = 0.0;
       }
       jrand=0;
       warmup_random (seed);
       return;
   }

我的问题是为什么我会遇到这个问题,以及如何解决它?

谢谢大家!

按要求编辑1

函数Initialise()定义为:

代码语言:javascript
复制
ADDAPI  void 
ADDCALL Initialise(int populationSize, int objectives, int constraints,  
int realvars, int binvars, int intvars, int nfix)
{
    popsize = populationSize;
    nobj = objectives;
    ncon = constraints;
    nreal = realvars;
    nint = intvars;
    nbin = binvars;
    nfixed = nfix;

    nbits = (int *)malloc(binvars*sizeof(int));
    for (int i = 0 ; i <binvars  ; i++)
        {
            nbits[i]=24 ;
        } 

    nints = (int *)malloc(nint*sizeof(int));
    bints = (int *)malloc(nint*sizeof(int));    

    for (int i = 0 ; i <nfixed/(2*nswitch+1); i++) 
        {
                bints[i*(2*nswitch+1)]=0; 
                nints[i*(2*nswitch+1)]=1; 
                //nints[i*(2*nswitch+1)]=2;

                 for (int j =(2*nswitch+1)*i+1 ; j<(2*nswitch+1)*(i+1); i++) 

                 {
                    bints[j]=0; 
                    nints[j]=TimeLength; 
                    //nints[j]=25;
                }
        }

    min_realvar = (double *)malloc(nreal*sizeof(double));
    max_realvar = (double *)malloc(nreal*sizeof(double));

    intvar = NULL;
    min_binvar = NULL;
    max_binvar = NULL;

    parent_pop = (population *)malloc(sizeof(population));
    child_pop = (population *)malloc(sizeof(population));
    mixed_pop = (population *)malloc(sizeof(population));

     allocate_memory_pop (parent_pop, popsize);
     allocate_memory_pop (child_pop, popsize);
     allocate_memory_pop (mixed_pop, 2*popsize);

     seed = 0.232; // for random functions

     bitlength = 0;
     pcross_bin = 0;
     pcross_int = 0;
     pmut_bin = 0 ;
     pmut_int = 0;
     eta_c = 50 ; // distribution index for crossover
     eta_m = 100 ; // distrubution index for mutations
     nbits = 0 ;
     nints = 0;

     fitness_func = NULL;
 }
EN

回答 1

Stack Overflow用户

发布于 2019-01-26 02:34:44

Initialise的实现并不重要,但声明是重要的,包括ADDAPIADDCALL的定义。假设ADDAPI类似于__declspec(dllexport),而ADDCALL__cdecl (或空的),那么使用:

代码语言:javascript
复制
import ctypes
NS = ctypes.CDLL("D:\\XXX\\nsga2.dll")
NS.Initialise(360,2,2,5,0,5,2)
NS.Randomise()

如果ctypes.WinDLL__stdcall,则使用__stdcall

理想情况下,定义argtypesrestype,但整数参数无论如何都是默认的,例如:

代码语言:javascript
复制
NS.Initialise.argtypes = ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.c_int
NS.Initialise.restype = None
NS.Randomise.argtypes = ()
NS.Randomise.restype = None
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54360555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档