`
tcspecial
  • 浏览: 901890 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Structure array elements must use contiguous memory (bad backing address at Stru

    博客分类:
  • java
阅读更多

JNA以结构体数组为参数进行调用:

 

////// C++
// student 结构体定义
typedef struct  
{
	int age;
	char name[20];
}Student;

// 修改java对象的属性值
JNAAPI bool changeObjs(Student stu[],int size)
{
	for(int i=0;i<size;i++)
	{
		stu[i].age*=10;
		strcpy(stu[i].name,"wokettas");
	}
	return true;
}

/////// Java
// JNA调用方法
Student[] stus=new Students[2];
Student s1=new Student();
Student s2=new Student();
s1.age=1;
s1.name=Arrays.copyOf("k1".getBytes(), 20);
s2.age=2;
s2.name=Arrays.copyOf("k2".getBytes(),20);
stus[0]=s1; //数组赋值
stus[1]=s2;
Gui.gui.changeObjs(stus, stus.length);

 

   运行报错:

        Structure array elements must use contiguous memory (bad backing address at Structure array index 1)

   结构体数组必须使用连续的内存区域, 上例s1,s2都是new出来的新对象,不可能连续; 也就是说传统方式的初始化数组不能解决, 查询JNA api发现里面提供了:

toArray

public Structure[] toArray(int size)
Returns a view of this structure's memory as an array of structures. Note that this Structure must have a public, no-arg constructor. If the structure is currently using a Memory backing, the memory will be resized to fit the entire array.
 

 修改后的代码:

// 测试对象数组
Student student=new Student();
Student[] stus=(Student[]) student.toArray(2); //分配大小为2的结构体数组
stus[0].age=1;
stus[0].name=Arrays.copyOf("k1".getBytes(), 20);
stus[1].age=2;
stus[1].name=Arrays.copyOf("k2".getBytes(),20);
Gui.gui.changeObjs(stus, stus.length);

 

 

分享到:
评论

相关推荐

    dma-contiguous.rar_memory

    "dma-contiguous.rar_memory"这个压缩包文件可能包含的是关于Linux内核中连续内存分配器(Contiguous Memory Allocator)的源代码和头文件,用于DMA映射框架。 首先,我们来了解什么是连续内存。在DMA操作中,硬件...

    实验三:数组及其应用.doc

    A one-dimensional array is a collection of elements of the same data type stored in contiguous memory locations. Each element is identified by an index or subscript. The syntax for declaring a one-...

    contiguous()方法

    contiguous()方法

    linux memory management study note

    在Linux中,内存管理涉及到多个层次和策略,包括页面回收、内存分配器、CMA(Contiguous Memory Area)、内存初始化以及固定映射等。 首先, `/proc/meminfo` 是一个用于查看系统内存状态的文件,它提供了关于系统...

    Contiguous_List.cpp

    Contiguous_List.cpp

    pytorch-contiguous() 函数.pdf

    pytorch pytorch_contiguous() 函数

    Finding Maximum Contiguous Subsequence Sum using divide-and-conquer approach

    最大连续子序列和(Maximal Contiguous Subsequence Sum,简称MCSS)问题是一个经典的计算机科学问题,主要涉及算法设计和分析,尤其是动态规划和分治策略。在这个问题中,目标是从一个整数数组中找到一个子序列,...

    book3s_hv_cma.rar_memory

    标题"book3s_hv_cma.rar_memory"暗示了我们正在讨论的是一个针对PowerPC(ppc)平台的KVM(Kernel-based Virtual Machine)实现的连续内存分配器,它基于CMA(Contiguous Memory Allocator)并用于DMA(Direct ...

    LinuxCMA-cewg43.pdf

    linux cma学习PPT,Linux Contiguous Memory Allocator的相关知识,很好的PPT。

    Contiguous-Memory-Allocation:I_Jemin 使用 Python 的连续内存分配器类

    连续内存分配使用 Python 的连续内存分配器类 by I_Jemin ijemin.com 使用 Python 的连续内存分配器模拟类。 不包括接口或测试类。 您可以将 MemoryManager 类导入到您的界面类或 GUI 类中。

    dma-contiguous.rar_For Real

    在IT领域, DMA(Direct Memory Access,直接内存访问)是一种技术,允许硬件设备如I/O控制器直接读取或写入系统内存,而无需CPU介入每一项数据传输。这极大地提高了数据传输效率,尤其是在大量数据传输时。在"DMA-...

    英飞凌Tasking lsl文件内存分段和指定变量\函数在物理内存中存放

    – run_addr = address (运行时地址,address at run-time) – load_addr = address (在复制到运行地址前,初始地址在ROM中(initial address)) – ordered (以group中定义的排序方式来将sections定位到地址...

    操作系统原理10.pdf

    Directory Structure Fil S t M ti File System Mounting File Sharing g Protection Southeast University 10.2 Operating System Concepts File Concept e Co cept Contig o s logical address space ...

    cmb.rar_physical

    在IT领域,物理内存(Physical Memory)是计算机系统中用于存储数据和指令的实际硬件资源,它是计算机运行程序的基础。Linux操作系统对物理内存的管理是非常关键的一环,它涉及到系统的性能和稳定性。标题“cmb.rar_...

    西电软工oop上机题目6 12.9.cpp

    Consider:class Char_vec {intsz ;charelement [1];...Define new_char_vec() to allocate contiguous memory for a Char_vec object so that the elements can be indexed through element as shown.

    数据结构实验报告-顺序表与链表.pdf

    顺序表是一种线性表,元素 stored in contiguous memory locations。顺序表的存储表示可以用以下结构体表示: typedef struct Sqlist{ ElemType *slist; // 存储空间的基地址 int length; // 表长度 int list...

    Pytorch之contiguous的用法

    在PyTorch中,`contiguous()`是一个非常重要的函数,它涉及到张量(Tensor)的内存布局和计算效率。理解其用法对于优化代码性能至关重要。本文将深入探讨`contiguous()`的含义、作用以及如何使用。 首先,我们需要...

    mmzone_32.rar_memory

    描述中提到“memory comes in 64Mb contiguous chunks”,这表明内存被划分为64兆字节(MB)的连续块,这种分块管理方式是为了优化内存分配和回收。 在嵌入式系统中,内存通常有限且需要高效利用。64MB的内存块大小...

    CONTIGUOUS -- 连续运行的开始和停止索引:RUNS = CONTIGUOUS(A,NUM) 返回元素 NUM 连续运行的开始和停止索引-matlab开发

    RUNS = CONTIGUOUS(A,NUM) 返回向量 A 中元素 NUM 的连续运行的开始和停止索引。A 和 NUM 可以是整数或字符的向量。 输出 RUNS 是一个 2 列元胞数组,其中第一列的第 i 行包含来自向量 NUM 的第 i 个值,第二列的第 ...

    数据结构 - 顺序表(Contiguous-List)

    **数据结构 - 顺序表(Contiguous-List)** 顺序表是数据结构中的一种基本类型,它是线性表的一种具体实现方式。在计算机科学中,数据结构的选择与操作直接影响到程序的效率和性能。理解并掌握顺序表的概念、特点以及...

Global site tag (gtag.js) - Google Analytics