2011-05-13 26 views
1

我正在嘗試編寫一個程序,它調用一個庫函數(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. 

我在做什麼錯?我將如何調用這個函數並顯示結果。

回答

2

它看起來像你的電話應該是以下幾點。原始代碼只傳遞整數而不是地址(這兩個值是庫函數返回的值)。

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

你需要找到哪些是指針的項目地址:

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