我正在实现两个程序,分别作为ALU和CPU,CPU将从文件中读取输入作为3个参数(num,operator,num)并将这些参数传递给ALU.exe,然后由ALU.exe计算并返回结果传递了参数,但仍未返回所需的输出
注意:-我本来只发布有问题的部分,但是上次我在这里发布问题时,注释要求输入完整的代码
CPU.exe
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main () {
ifstream obj;
const char *path = "/home/f178082/ALU.exe";
obj.open("sample.txt",ios::app);
string arg1,arg2;
string op;
while(
getline(obj,arg1,',') && getline(obj,op,',') && getline(obj,arg2,'\n')
) {
pid_t pid = fork();
if(pid==0) {
char* args[3];
char a1[arg1.size()+1];
char a2[arg2.size()+1];
char o[op.size()+1];
arg1.copy(a1,arg1.size()+1);
a1[arg1.size()]='\0';
arg2.copy(a2,arg2.size()+1);
a2[arg2.size()]='\0';
op.copy(o,op.size()+1);
o[op.size()]='\0';
args[1]=a1;
args[2]=o;
args[3]=a2;
//args[0]=arg1.c_str();
//args[1]=op.c_str();
//args[2]=arg2.c_str();
execv(path,args);
}
wait();
}
return 0;
}
将参数发送到ALU.exe的段
while(
getline(obj,arg1,',') && getline(obj,op,',') && getline(obj,arg2,'\n')
) {
pid_t pid = fork();
if(pid==0) {
char* args[3];
char a1[arg1.size()+1];
char a2[arg2.size()+1];
char o[op.size()+1];
arg1.copy(a1,arg1.size()+1);
a1[arg1.size()]='\0';
arg2.copy(a2,arg2.size()+1);
a2[arg2.size()]='\0';
op.copy(o,op.size()+1);
o[op.size()]='\0';
args[1]=a1;
args[2]=o;
args[3]=a2;
//args[0]=arg1.c_str();
//args[1]=op.c_str();
//args[2]=arg2.c_str();
execv(path,args);
}
wait();
}
及以下是ALU.cpp的代码
ALU.cpp
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <stdlib.h>
using namespace std;
int Operation(int a,char* op,int b) {
char operand = *op;
if(operand == '+') {
return a+b;
}
if(operand == '-') {
return a-b;
}
if(operand == '/'){
return a/b;
}
if(operand == '*'){
return a*b;
}
else {
cout << "invalid" << endl;
return -999;
}
}
int main (int argc,char* argv[]) {
//if(argc!=3) {
//cout << "Exit" << endl;
//return -9999;
//}
for(int i=1;i<argc;i++) {
cout << "Arg " << i << ": " << argv[i] << endl;
}
int arg1,arg2;
char* op;
arg1= atoi(argv[1]);
arg2= atoi(argv[3]);
op = argv[2];
int result = Operation(arg1,op,arg2);
cout << "result: " << result << endl;
return result;
}
并且示例文件包含此数据
3,+,6
12,-,4
3,*,10
但获得的结果如下
invalid
invalid
result: -999
result: -999
invalid
result: -999
我已经调试了好几天了,但是仍然找不到解决问题的方法。我一直在搜索以前的问题,例如在堆栈溢出时遇到这样的问题,但是仍然没有成功,这就是为什么我在这里问这个问题,如果我的英语流利程度令我难以理解,或者我一直在做一些被stackoverflow社区不赞成的事情,我感到很抱歉,但是我已经尽我所能写一个问题,让每个人都牢记一切在这里告诉我
编辑:
尝试打印出参数会导致错误,所以这可能是类型转换的问题吗?
当我尝试
cout << a1 << o << a2 << endl;
这是我收到的错误
3b��*c�����s
10�����p
*** Error in `12��-c�����s
4c�����p
*** Error in `3b��+c�����s
6c�����p
*** Error in `b���b��xd��-���*c��10���b��xd��S�b���b��xd��-���
*** Error in `c���b��xd��-���-c��4c���b��xd��S�c���b��xd��-���
*** Error in `./a.out': free(): invalid pointer: 0xbfb162b0 ***
./a.out': free(): invalid pointer: 0xbfb162b0 ***
b���b��xd��-���*c��10���b��xd��S�b���b��xd��-���
b���b��xd��-���*c��10���b��xd��S�b���b��xd��-���
*** Error in `*** Error in `./a.out': free(): invalid pointer: 0xbfb162b0 ***
./a.out': free(): invalid pointer: 0xbfb162b0 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb7bf8377]
/lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb7bfe2f7]
/lib/i386-linux-gnu/libc.so.6(+0x6dc31)[0xb7bfec31]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x18)[0xb7dd2d88]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev+0x25)[0xb7e6c985]
./a.out[0x8048f5a]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb7ba9637]
./a.out[0x8048b11]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:01 1052404 /home/f178082/a.out
0804a000-0804b000 r--p 00001000 08:01 1052404 /home/f178082/a.out
0804b000-0804c000 rw-p 00002000 08:01 1052404 /home/f178082/a.out
09621000-09646000 rw-p 00000000 00:00 0 [heap]
b7a00000-b7a21000 rw-p 00000000 00:00 0
b7a21000-b7b00000 ---p 00000000 00:00 0
b7b39000-b7b3c000 rw-p 00000000 00:00 0
b7b3c000-b7b8f000 r-xp 00000000 08:01 393657 /lib/i386-linux-gnu/libm-2.23.so
b7b8f000-b7b90000 r--p 00052000 08:01 393657 /lib/i386-linux-gnu/libm-2.23.so
b7b90000-b7b91000 rw-p 00053000 08:01 393657 /lib/i386-linux-gnu/libm-2.23.so
b7b91000-b7d41000 r-xp 00000000 08:01 393587 /lib/i386-linux-gnu/libc-2.23.so
b7d41000-b7d43000 r--p 001af000 08:01 393587 /lib/i386-linux-gnu/libc-2.23.so
b7d43000-b7d44000 rw-p 001b1000 08:01 393587 /lib/i386-linux-gnu/libc-2.23.so
b7d44000-b7d47000 rw-p 00000000 00:00 0
b7d47000-b7d63000 r-xp 00000000 08:01 393625 /lib/i386-linux-gnu/libgcc_s.so.1
b7d63000-b7d64000 rw-p 0001b000 08:01 393625 /lib/i386-linux-gnu/libgcc_s.so.1
b7d64000-b7ed1000 r-xp 00000000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed1000-b7ed2000 ---p 0016d000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed2000-b7ed7000 r--p 0016d000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed7000-b7ed8000 rw-p 00172000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed8000-b7edb000 rw-p 00000000 00:00 0
b7ef0000-b7ef1000 rw-p 00000000 00:00 0
b7ef1000-b7ef2000 rw-p 00000000 00:00 0
b7ef2000-b7ef5000 r--p 00000000 00:00 0 [vvar]
b7ef5000-b7ef7000 r-xp 00000000 00:00 0 [vdso]
b7ef7000-b7f1a000 r-xp 00000000 08:01 393559 /lib/i386-linux-gnu/ld-2.23.so
b7f1a000-b7f1b000 r--p 00022000 08:01 393559 /lib/i386-linux-gnu/ld-2.23.so
b7f1b000-b7f1c000 rw-p 00023000 08:01 393559 /lib/i386-linux-gnu/ld-2.23.so
bfaf8000-bfb19000 rw-p 00000000 00:00 0 [stack]
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb7bf8377]
/lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb7bfe2f7]
/lib/i386-linux-gnu/libc.so.6(+0x6dc31)[0xb7bfec31]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x18)[0xb7dd2d88]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev+0x25)[0xb7e6c985]
./a.out[0x8048f5a]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb7ba9637]
./a.out[0x8048b11]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:01 1052404 /home/f178082/a.out
0804a000-0804b000 r--p 00001000 08:01 1052404 /home/f178082/a.out
0804b000-0804c000 rw-p 00002000 08:01 1052404 /home/f178082/a.out
09621000-09646000 rw-p 00000000 00:00 0 [heap]
b7a00000-b7a21000 rw-p 00000000 00:00 0
b7a21000-b7b00000 ---p 00000000 00:00 0
b7b39000-b7b3c000 rw-p 00000000 00:00 0
b7b3c000-b7b8f000 r-xp 00000000 08:01 393657 /lib/i386-linux-gnu/libm-2.23.so
b7b8f000-b7b90000 r--p 00052000 08:01 393657 /lib/i386-linux-gnu/libm-2.23.so
b7b90000-b7b91000 rw-p 00053000 08:01 393657 /lib/i386-linux-gnu/libm-2.23.so
b7b91000-b7d41000 r-xp 00000000 08:01 393587 /lib/i386-linux-gnu/libc-2.23.so
b7d41000-b7d43000 r--p 001af000 08:01 393587 /lib/i386-linux-gnu/libc-2.23.so
b7d43000-b7d44000 rw-p 001b1000 08:01 393587 /lib/i386-linux-gnu/libc-2.23.so
b7d44000-b7d47000 rw-p 00000000 00:00 0
b7d47000-b7d63000 r-xp 00000000 08:01 393625 /lib/i386-linux-gnu/libgcc_s.so.1
b7d63000-b7d64000 rw-p 0001b000 08:01 393625 /lib/i386-linux-gnu/libgcc_s.so.1
b7d64000-b7ed1000 r-xp 00000000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed1000-b7ed2000 ---p 0016d000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed2000-b7ed7000 r--p 0016d000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed7000-b7ed8000 rw-p 00172000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed8000-b7edb000 rw-p 00000000 00:00 0
b7ef0000-b7ef1000 rw-p 00000000 00:00 0
b7ef1000-b7ef2000 rw-p 00000000 00:00 0
b7ef2000-b7ef5000 r--p 00000000 00:00 0 [vvar]
b7ef5000-b7ef7000 r-xp 00000000 00:00 0 [vdso]
b7ef7000-b7f1a000 r-xp 00000000 08:01 393559 /lib/i386-linux-gnu/ld-2.23.so
b7f1a000-b7f1b000 r--p 00022000 08:01 393559 /lib/i386-linux-gnu/ld-2.23.so
b7f1b000-b7f1c000 rw-p 00023000 08:01 393559 /lib/i386-linux-gnu/ld-2.23.so
bfaf8000-bfb19000 rw-p 00000000 00:00 0 [stack]
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb7bf8377]
/lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb7bf8377]
/lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb7bfe2f7]
/lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb7bfe2f7]
/lib/i386-linux-gnu/libc.so.6(+0x6dc31)[0xb7bfec31]
(+0x6dc31)[0xb7bfec31]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x18)[0xb7dd2d88]
/usr/lib/i386-linux-gnu/libstdc++.so.6/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x18)[0xb7dd2d88]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev+0x25)[0xb7e6c985]
./a.out[0x8048f5a]
/lib/i386-linux-gnu/libc.so.6(_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev+0x25)[0xb7e6c985]
./a.out[0x8048f5a]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb7ba9637]
./a.out[0x8048b11]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:01 1052404 /home/f178082/a.out
0804a000-0804b000 r--p 00001000 08:01 1052404 /home/f178082/a.out
0804b000-0804c000 rw-p 00002000 08:01 1052404 /home/f178082/a.out
09621000-09646000 rw-p 00000000 00:00 0 [heap]
b7a00000-b7a21000 rw-p 00000000 00:00 0
b7a21000-b7b00000 ---p 00000000 00:00 0
b7b39000-b7b3c000 rw-p 00000000 00:00 0
b7b3c000-b7b8f000 r-xp 00000000 08:01 393657 /lib/i386-linux-gnu/libm-2.23.so
b7b8f000-b7b90000 r--p 00052000 08:01 393657 /lib/i386-linux-gnu/libm-2.23.so
b7b90000-b7b91000 rw-p 00053000 08:01 393657 /lib/i386-linux-gnu/libm-2.23.so
b7b91000-b7d41000 r-xp 00000000 08:01 393587 /lib/i386-linux-gnu/libc-2.23.so
b7d41000-b7d43000 r--p 001af000 08:01 393587 /lib/i386-linux-gnu/libc-2.23.so
b7d43000-b7d44000 rw-p 001b1000 08:01 393587 /lib/i386-linux-gnu/libc-2.23.so
b7d44000-b7d47000 rw-p 00000000 00:00 0
b7d47000-b7d63000 r-xp 00000000 08:01 393625 /lib/i386-linux-gnu/libgcc_s.so.1
b7d63000-b7d64000 rw-p 0001b000 08:01 393625 /lib/i386-linux-gnu/libgcc_s.so.1
b7d64000-b7ed1000 r-xp 00000000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed1000-b7ed2000 ---p 0016d000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed2000-b7ed7000 r--p 0016d000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed7000-b7ed8000 rw-p 00172000 08:01 785415 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.21
b7ed8000-b7edb000 rw-p 00000000 00:00 0
b7ef0000-b7ef1000 rw-p 00000000 00:00 0
b7ef1000-b7ef2000 rw-p 00000000 00:00 0
b7ef2000-b7ef5000 r--p 00000000 00:00 0 [vvar]
b7ef5000-b7ef7000 r-xp 00000000 00:00 0 [vdso]
b7ef7000-b7f1a000 r-xp 00000000 08:01 393559 /lib/i386-linux-gnu/ld-2.23.so
b7f1a000-b7f1b000 r--p 00022000 08:01 393559 /lib/i386-linux-gnu/ld-2.23.so
b7f1b000-b7f1c000 rw-p 00023000 08:01 393559 /lib/i386-linux-gnu/ld-2.23.so
bfaf8000-bfb19000 rw-p 00000000 00:00 0 [stack]
我在这里做什么错了?
答案 0 :(得分:3)
您不应传递3个参数,而应传递四个参数。请记住,第一个参数(argv [0],因为数组基于零)应该是程序名称。而且必须对数组进行NULL终止。
所以您必须这样做:
const char *args[5];
args[0]=path;
args[1]=arg1.c_str();
args[2]=op.c_str();
args[3]=arg2.c_str();
args[4]=NULL;
编辑:
我也忘记了execv()
是越野车。它被声明为int execv(const char *pathname, char *const argv[]);
而不是int execv(const char *pathname, const char *const argv[]);
,因此这是可以使用指针强制转换的少数情况之一:
execv(path, (char **)args);
或者也可以将args[]
放在一起并使用execl()
:
execl(path, path, arg1.c_str(), op.c_str(), arg2.c_str(), NULL);