细分故障问题
Segmentation fault issue
我正在尝试使用xdotool进行 system()
命令。
以下是我的测试程序:
int main() {
int x = 10;
int y = 50;
char* str;
sprintf(str, "xdotool mousemove %d, %d", x, y);
system(str);
}
我会得到一个分段故障(核心倾倒)错误。您是否知道允许这样的命令工作的方法?顺便说一句,我已经尝试过它。我是C 的新手,您的帮助将不胜感激。
其他答案是正确的,但是,您可以在C 中使用更好的方法,而不是使用sprintf
int main() {
int x = 10;
int y = 50;
std::stringstream ss;
ss << "xdotool mousemove " << x << " " << y;
system(ss.str().c_str());
}
您的问题是您没有分配任何空间将字符串打印到。
例如,您可能想做以下操作:
int main() {
int x = 10;
int y = 50;
/* this assigns 255 characters of space for the string on the stack */
char str[255];
/* char* str -- this assigns no space, it just defines a pointer */
/* this function will put the format string with arguments into the
* space you provide... It will not provide it's own space. */
sprintf(str, "xdotool mousemove %d, %d", x, y);
system(str);
}
这将摆脱您的SEG故障,因为您不再访问未分配的内存。
如果您不知道函数如何工作,请考虑使用C 参考指南。
相关文章:
- 警告处理为错误这里有什么问题
- 分段故障(堆芯转储)矢量
- 最小硬币更换问题(自上而下方法)
- 分段故障背包问题
- 分割故障 - 遇到问题,创建新的数组实例
- C :分割故障(核心倾倒)问题
- 为什么最后一个拆分函数会导致分割故障?如何解决此问题
- AIX:未知分段故障问题
- Python C-API:PYDICT_GETITEM上的分割故障,可能的参考问题
- 使用指针和结构C 计算两个点之间的距离,分割故障问题
- SSE _mm_store_ps分段故障问题
- 细分故障问题
- R内部分段故障和链接问题
- 分段故障和指针问题
- 创建二进制搜索树时出现分段故障问题
- 递归呼叫分段故障问题
- 使用arm-linux- gnuabi -g++ -o时出现段故障,不使用-o也不会出现问题
- "Ambiguous Overload"回路中的问题和故障 CIN
- 单链表段故障问题
- 在c++和Linux中查找总线错误/Seg故障的问题