dimanche 15 décembre 2019

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

NOTE:-I was going to post the segment with problems only but last time I posted a question here comments were asking for entire code

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;
}

The segment where arguments are sent to 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();
}

and below is the code for 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;

}

and the sample file contains this data

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

but the results obtained are as follows

 invalid
invalid
result: -999
result: -999
invalid
result: -999

I've been debugging for days and still can't find any solution to my problem I've searched for previous questions with issues like this on stack overflow but still no success this is why I'm asking this question here,I'm sorry if there's an issue with my fluency in English which makes it hard to understand or I've been doing something which is frowned upon by the stackoverflow community but I've tried my best to write a question keeping everything in mind everyone tells me here

EDIT:

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

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]

What am I doing wrong here?

Aucun commentaire:

Enregistrer un commentaire