---
title: 函数：numpy_to_ptr
description: "该接口即将废弃，建议使用acl.util.bytes_to_ptr接口。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/runtimeapi/aclpythondevg_01_0052.html
sourcePath: /source/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0052.html
indexId: 64aaa4b2e029d5fdda6cc045e86121aa3744b730bc8baeea8c96e60adf46389b71
---
# 函数：numpy_to_ptr

该接口即将废弃，建议使用acl.util.bytes_to_ptr接口。


| C函数原型 | 无 |
| --- | --- |
| Python函数 | ptr = acl.util.numpy\_to\_ptr(data) |
| 函数功能 | 获取numpy.ndarray数组的内存数据指针地址。 |
| 输入说明 | data：numpy.ndarray类型数据。 |
| 返回值说明 | ptr：int，numpy数组的内存数据指针地址。 |
| 约束说明 | 无 |
| 注意事项 | 若要继续使用该接口，需要运行环境为python ≥ 3.8且numpy ≥ 1.22.0。 修改示例如下： np\_ptr = acl.util.numpy\_to\_ptr(np\_arr\_in) 修改后使用： bytes\_in = np\_arr\_in.tobytes() bytes\_ptr = acl.util.bytes\_to\_ptr(bytes\_in) 使用本函数返回的指针地址前，需要确保传入的numpy.ndarray对象生命周期还没有结束（没有被删除或被Python的GC回收），否则将会产生未定义行为。 若传入的numpy.ndarray数组在内存上是非行连续的，在接口调用时会打印如下提示信息： Warning:The input ndarray is discontiguous. Please use acl.util.numpy\_contiguous\_to\_ptr instead. 请使用acl.util.numpy\_contiguous\_to\_ptr接口获取numpy数组的地址对象，否则在使用acl.rt.memcpy拷贝数据时，由于acl.rt.memcpy拷贝的是连续内存地址，会导致拷贝的数据和目标拷贝数据不一致的问题。 可以通过numpy数组的“flags”属性来查看numpy数组在内存上是否连续： print(data.flags) 得到结果如下： C\_CONTIGUOUS : True F\_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False 其中“C\_CONTIGUOUS：True”，则说明numpy数组在内存上行连续。“F\_CONTIGUOUS : False”，则说明numpy数组在内存上列不连续。 |
