Exec arguments not providing expected results [Linux/C++]

I'm implementing two programs which will act as ALU and CPU,The CPU will read inputs from a file as 3 arguments (num,operator,num) and pass those arguments to ALU.exe which will then compute and return the result the arguments are passed but still not returning the desired output


The code for CPU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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;
}


The segment where arguments are sent to ALU.exe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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();
}



and below is the code for ALU.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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;

}



and the sample file contains this data

1
2
3
3,+,6
12,-,4
3,*,10


but the results obtained are as follows

1
2
3
4
5
6
invalid
invalid
result: -999
result: -999
invalid
result: -999


Trying to print out the arguments results in an error so this is a problem with type casting then maybe?

when I try to

 
cout << a1 << o << a2 << endl;


This is the error I receive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
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


NOTE:-The error was shortened due to shortage of space in the "content" field

What am I doing wrong here?
1
2
3
4
5
6
7
8
9
10
11
12
13
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;

Your args is too small, so you're running off the end.

Also, your args[0] is left pointing at garbage. By convention, it points to path.

The last args[n] should be NULL.

So perhaps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char* args[5];
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[0]=path;
args[1]=a1;
args[2]=o;
args[3]=a2;
args[4]=NULL;

Last edited on
Is char* args supposed to be
 
const char* args[5]?

Because I'm getting an error
The constness of the execv functions is messed up compared to the constness of the execl functions.

Remove the const from args, and try
char path[] = "/home/f178082/ALU.exe";
The final arg in the list you pass to exec... must be null.
Why not use execl() instead?
1
2
3
if(pid==0) {
   execl(path, arg1.c_str(), arg2.c_str(), arg3.c_str(), NULL);
}
Topic archived. No new replies allowed.