Как передать целочисленные массивы в библиотечную функцию - PullRequest
1 голос
/ 14 мая 2011

Я пытаюсь написать программу, которая вызывает библиотечную функцию (http://www.diku.dk/hjemmesider/ansatte/pisinger/3dbpp.c), которая решает проблему упаковки бина. Я не делал C с тех пор, как поступил в колледж, и я довольно ржавый.

У меня есть библиотека, скомпилированная. И статически связан, так что я не получаю ошибок о том, что функция не существует, но теперь я получаю segfualt для функции binpack3d(), в соответствии с 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 ]

2 голосов
/ 14 мая 2011

Похоже, ваш звонок должен быть следующим. Исходный код передавал только целое число, а не адрес (эти два значения являются значениями, возвращаемыми библиотечной функцией).

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 голосов
/ 14 мая 2011

Вам необходимо найти адрес предметов, которые являются указателями:

binpack3d(1, 12, 12, 24,
          w, h, d,
         x, y, z, bno,
         &lb, &ub, 10);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...