首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CUPS打印文件分割故障

CUPS打印文件分割故障
EN

Stack Overflow用户
提问于 2017-11-28 07:24:31
回答 1查看 356关注 0票数 0

我在试着制作一个杯子打印系统。我想得到打印机的状态,到目前为止打印了多少页等等。

为此,我正在执行CUPS示例中给出的示例程序。

代码语言:javascript
复制
#include <cups/cups.h>
#include <stdio.h>


int main(){

int num_options;
cups_option_t *options;

cups_dest_t *dests;
int num_dests = cupsGetDests(&dests);
cups_dest_t *dest = cupsGetDest("name", NULL, num_dests, dests);
int job_id;

/* Print a single file */
job_id = cupsPrintFile(dest->name, "testfile.txt", "Test Print", num_options, options);

cupsFreeDests(num_dests, dests);

return 0;
}

我使用gcc myfile.c -o myout -lcups编译它

当我尝试执行./myout

我得到了

分段故障

我使用树莓-π3板作为我的CUPS服务器。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-28 07:28:02

dest指向无效地址.

代码语言:javascript
复制
cups_dest_t *dest; // declared but not initialized or assigned afterwards 

所以去引用它(cupsPrintFile(dest->name.)是UB,可以导致SegFault。

这是您应该如何使用它(摘自这里):

代码语言:javascript
复制
#include <cups/cups.h>

cups_dest_t *dests;
int num_dests = cupsGetDests(&dests);
cups_dest_t *dest = cupsGetDest("name", NULL, num_dests, dests);

/* do something with dest */

cupsFreeDests(num_dests, dests);

更新:

您的代码不处理某些变量(即不初始化-坏)。我看到的第一个是cups_option_t *options;。照顾好所有的变量,如果不起作用的话-调试。

代码语言:javascript
复制
int main(){

int num_options;
cups_option_t *options; // add a call to "cupsAddOption("first", "value", num_options, &options);"

cups_dest_t *dests;
int num_dests = cupsGetDests(&dests);
cups_dest_t *dest = cupsGetDest("name", NULL, num_dests, dests);
int job_id;

/* Print a single file */
job_id = cupsPrintFile(dest->name, "testfile.txt", "Test Print", num_options, options); // options is used here but is uninitialized

cupsFreeDests(num_dests, dests);

return 0;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47526085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档