gpt4 book ai didi

c - 如何将整数数组传递给库函数

转载 作者:太空宇宙 更新时间:2023-11-04 03:05:20 24 4
gpt4 key购买 nike

我正在尝试编写一个调用库函数 ( http://www.diku.dk/hjemmesider/ansatte/pisinger/3dbpp.c ) 的程序来解决装箱问题。自从大学毕业后我就没学过任何 C,我很生疏。

我已经编译了库。并且静态链接,所以我不会得到关于函数不存在的错误,但现在我在 binpack3d() 函数上得到了一个 segfualt,根据 gdb。我认为它是某种指针错误。这是引用库函数的代码:

#include <stdio.h>
#include "3dbin.h"

int main(void)
{


int w[2];
int h[2];
int d[2];

w[0]=5;
h[0]=6;
d[0]=7;

w[1]=5;
h[1]=6;
d[1]=7;


int x[2];
int y[2];
int z[2];
int bno[1];
int lb;
int ub;

binpack3d(1, 12, 12, 24,
w, h, d,
x, y, z, bno,
lb, ub, 10);


return(1);

}

函数定义如下:

void binpack3d(int n, int W, int H, int D,
int *w, int *h, int *d,
int *x, int *y, int *z, int *bno,
int *lb, int *ub, int timelimit)
{

//code

还有头文件(也不确定我是否做对了)

void binpack3d(int , int , int , int ,
int *, int *, int *,
int *, int *, int *, int *,
int , int , int );

这是它的文档

 * This file contains the callable routine binpack3d with prototype
*
* void binpack3d(int n, int W, int H, int D,
* int *w, int *h, int *d,
* int *x, int *y, int *z, int *bno,
* int *lb, int *ub, int timelimit);
*
* the meaning of the parameters is the following:
* n Size of problem, i.e. number of boxes to be packed.
* This value must be smaller than MAXITEMS defined below.
* W,H,D Width, height and depth of every bin.
* w,h,d Integer arrays of length n, where w[j], h[j], d[j]
* are the dimensions of box j for j=0,..,n-1.
* x,y,z,bno Integer arrays of length n where the solution found
* is returned. For each box j=0,..,n-1, the bin number
* it is packed into is given by bno[j], and x[j], y[j], z[j]
* are the coordinates of it lower-left-backward corner.
* lb Lower bound on the solution value (returned by the procedure).
* ub Objective value of the solution found, i.e. number of bins
* used to pack the n boxes. (returned by the procedure).
* timelimit Time limit for solving the problem expressed in seconds.
* If set to zero, the algorithm will run until an optimal
* solution is found; otherwise it terminates after timelimit
* seconds with a heuristic solution.

我做错了什么?我将如何调用此函数并显示结果。

最佳答案

看起来您的调用应该如下所示。原始代码只传递整数而不是地址(这两个是库函数返回的值)。

binpack3d(1, 12, 12, 24,
w, h, d,
x, y, z, bno,
&lb, &ub, 10);

原型(prototype)需要更改以反射(reflect)这两个“返回值”是 int*:

void binpack3d(int , int , int , int ,
int *, int *, int *,
int *, int *, int *, int *,
int *, int *, int );

关于c - 如何将整数数组传递给库函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5997707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com