日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 空指针_Python ctypes模块:扩展指针数组时进行NULL指针访问

發(fā)布時間:2024/4/17 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 空指针_Python ctypes模块:扩展指针数组时进行NULL指针访问 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我試圖在項(xiàng)目中使用ctypes模塊.我正在創(chuàng)建一個動態(tài)分配的“ max_entries”對數(shù)組,一旦該數(shù)組用完,我將創(chuàng)建一個新的大小為(1.5 * max_entries)的數(shù)組,并將內(nèi)容從舊數(shù)組復(fù)制到新數(shù)組.

不幸的是,當(dāng)我嘗試訪問此new_array的內(nèi)容時,出現(xiàn)了“空指針訪問”異常.相應(yīng)的C代碼似乎可以正常工作. (請參見下面的代碼.)

我想知道我是否缺少有關(guān)ctypes模塊工作方式的信息.任何幫助將不勝感激. (不確定這是否是適合我的問題的郵件列表.)

/謝謝!

#!/usr/bin/env python

from ctypes import *

import math

import random

class PAIR(Structure):

_fields_ = [("a", c_long),

("b", c_long)]

class MY_ARR(Structure):

_fields_ = [("no_entries", c_longlong),

("max_entries", c_longlong),

("entries", POINTER(POINTER(PAIR)))

]

def extendArray(x):

print "Extending Array"

print "Before: %d/%d" % (x.no_entries, x.max_entries)

old_arr = x.entries

# Create a new array

new_max_entries = int(math.ceil(1.5 * x.max_entries))

x.entries = (POINTER(PAIR) * new_max_entries)()

# Copy the entries from the old array to the new array

for i in range(x.no_entries):

x.entries[i] = old_arr[i]

x.max_entries = new_max_entries

print "After: %d/%d" % (x.no_entries, x.max_entries)

return x

def printPair(x):

print x.contents.a, x.contents.b

def printArray(x):

print "Printing %d/%d Entries" % (x.no_entries, x.max_entries)

for i in range(x.no_entries):

printPair(x.entries[i])

if __name__ == "__main__":

x = MY_ARR(0, 10, (POINTER(PAIR) * 10)())

for i in range(100):

if x.no_entries == x.max_entries:

print "

Printing Before Extension"

printArray(x)

extendArray(x)

print "

Printing After Extension"

printArray(x)

my_pair = PAIR(i, random.randint(0, 100))

x.entries[x.no_entries] = pointer(my_pair)

x.no_entries += 1

printPair(x.entries[i])

printArray(x)

現(xiàn)在,不幸的是,當(dāng)我嘗試運(yùn)行此代碼時,出現(xiàn)“空指針訪問”異常:

$python TestExtension.py

0 40

1 40

2 11

3 36

4 82

5 73

6 93

7 100

8 75

9 80

Printing Before Extension

Printing 10/10 Entries

0 40

1 40

2 11

3 36

4 82

5 73

6 93

7 100

8 75

9 80

Extending Array

Before: 10/10

After: 10/15

Printing After Extension

Printing 10/15 Entries

Traceback (most recent call last):

File "TestExtension.py", line 55, in

printArray(x)

File "TestExtension.py", line 42, in printArray

printPair(x.entries[i])

File "TestExtension.py", line 37, in printPair

print x.contents.a, x.contents.b

ValueError: NULL pointer access

相應(yīng)的C代碼可以完美地工作:

#include

#include

#include

typedef struct {

long a;

long b;

} pair;

typedef struct {

long long no_entries;

long long max_entries;

pair **entries;

} my_arr;

my_arr *extend_array(my_arr *x) {

int i;

pair **old_entries = x->entries;

long long new_max_entries = ceil(1.5 * x->max_entries);

printf("Extending Array

");

printf("Before: %lld/%lld

", x->no_entries, x->max_entries);

x->entries = malloc(sizeof(pair *) * new_max_entries);

for (i = 0; i < 100; ++i) {

x->entries[i] = old_entries[i];

}

x->max_entries = new_max_entries;

free(old_entries);

printf("After: %lld/%lld

", x->no_entries, x->max_entries);

return x;

}

void print_pair(pair *p) {

printf("%ld%ld

", p->a, p->b);

}

void print_array(my_arr *x) {

int i;

printf("Printing %lld/%lld entries

", x->no_entries, x->max_entries);

for (i = 0; i < x->no_entries; ++i) {

print_pair(x->entries[i]);

}

}

int main(int argc, char *argv[])

{

int i;

my_arr x = {

0,

10,

malloc(sizeof(pair *) * 10)

};

for (i = 0; i < 100; ++i) {

if (x.no_entries == x.max_entries) {

extend_array(&x);

}

pair *my_pair = malloc(sizeof(pair));

my_pair->a = i;

my_pair->b = rand() % 100;

x.entries[x.no_entries++] = my_pair;

print_pair(x.entries[i]);

}

print_array(&x);

return 0;

}

總結(jié)

以上是生活随笔為你收集整理的python 空指针_Python ctypes模块:扩展指针数组时进行NULL指针访问的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。