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

发布于 2024-11-07 03:35:51 字数 2547 浏览 0 评论 0原文

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

我已经编译了库。并且静态链接,因此我不会收到有关该函数不存在的错误,但现在根据 gdb 的说法,我在 binpack3d() 函数上收到了一个段。我认为这是某种指针错误。这是引用库函数的代码:

#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. 

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

I'm trying to write a program which calls a library function (http://www.diku.dk/hjemmesider/ansatte/pisinger/3dbpp.c) that solves a bin packing problem. I haven't done any C since college and I'm pretty rusty.

I've got the library compiled. And statically linked so I don't get errors about the function not existing, but now I'm getting a segfualt on the binpack3d() function, according to gdb. I think its some kind of pointer error. Here is the code that references the library function :

#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);

}

Here is the function definition:

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

And the header file (not sure if i did this right either)

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

And Here is the documentation for it

 * 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. 

What am I doing wrong? How would I call this function and display the results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2024-11-14 03:35:51

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

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

并且原型需要更改以反映这两个“返回值”是 int*

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

It looks like your call should be the following. The original code was passing just the integer rather than the address (those two are values that are returned by the library function).

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

And the prototype needs to change to reflect that those two "return values" are int*:

void binpack3d(int , int , int , int ,
               int *, int *, int *, 
               int *, int *, int *, int *,
               int *, int *, int );
烟凡古楼 2024-11-14 03:35:51

您需要找到指针项的地址:

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

You need to find the address of the items which are pointers:

binpack3d(1, 12, 12, 24,
          w, h, d,
         x, y, z, bno,
         &lb, &ub, 10);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文