Reply
Thread Tools
Posts: 3,328 | Thanked: 4,476 times | Joined on May 2011 @ Poland
#1
Hi
In my program ****tion main() is

Code:
int main() 
{
    clear;
    cout << "MarBattery v.1.0" << endl << "This software is distributed under GNU GPL v3 terms. Use it on your own risk." << endl << endl; // left one line :)
cout << "getinfo";
    getinfo(batraw);
cout << "after";
    return 0;
}

While running the program I get

Code:
MarBattery v.1.0
This software is distributed under GNU GPL v3 terms. Use it on your own risk.

Segmentation fault
getinfo()

Code:
unsigned char getinfo(battery_state_raw batvar_raw, bool closefile = true)
{
cout << "opening";
    if (datadump.is_open() == false) datadump.open(datadumppath.c_str(), ios::in);
cout << 's';
    system( datadumpcmd.c_str() ); //this stupid function need `const char*`
cout << "ed_r";
    extractdata_raw();
cout << "ed";
    extractdata(); //get only needed ones
    bat.showdata(); //show it
    if ( closefile == true ) datadump.close(); //if needed
}
extractdata_raw()

Code:
#define trashit() getline(datadump, *trash) // I couldn't make a function and don't wanna write it over and over ;)
#define getit(a) getline(datadump, batraw.a)
void extractdata_raw()
{
cout << "extractdata_raw";
    string* trash = new string; // próbowałem też string* trash = NULL;
    trashit(); trashit(); //lines 1, 2 
    getit(capacity_bars); //line 3
    getit(capacity_bars_full); //line 4
    //....
    trashit(); trashit(); trashit(); //line 16, 17, 18
    getit(voltage_now); //line 17
    getit(voltage_design); // line 18
    //T(h)rash it all!! :D
    delete [] trash;
cout << "deleting";
}

extractdata()

Code:
#define rawatof(data) bat.data=atof(tmpraw.data.c_str()) // I'm too lazy to write it so many times ;D
#define extractme(info) tmpraw.info=batraw.info
#define br batraw
#define vn voltage_now
#define vd voltage_design
void extractdata()
{
    battery_state_raw tmpraw;
    extractme(capacity_bars)[34];
    rawatof(capacity_bars);
//....
    extractme(voltage_now)[29] + br.vn[30] + br.vn[31] + br.vn[32];
    rawatof(voltage_now);
    extractme(voltage_design)[28] + br.vd[29] + br.vd[30] + br.vd[31];
    rawatof(voltage_design);
}

class battery_state

Code:
class battery_state
{
public:
    friend void extractdata();
    inline bool isPresent();
    battery_state() : voltage_now(0), voltage_design(0), percentage(0), capacity_full(0), capacity_now(0) ,capacity_bars(0), capacity_bars_full(0)  {} // zero-it, the stupid compiler doesn't allow me to make it auto-variable
    unsigned char showdata();
private:
//dane składowe

in battery_state_raw

Code:
public:
    friend void extractdata_raw();
    friend void extractdata();
the rest are battery_state data but as strings

what can i do?
thanks for all
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#2
First of all: stop being lazy. Remove the #define lines and write the code. Copy paste is cheap. Especially if you need to debug your code.

If you want to see what all your macros are expanded to, use the -E flag to your compiler, assuming you're using g++, and inspect the result.

As to the debugging: if you think that you need to use new to get a string instance, rewrite your code after figuring out what the call to getline() actually does.

Regarding asking for help: include how you compile it, including all warnings that you get. I'm very certain that you get quite a few...

And don't make friends if you don't have to. Having naked functions as friends indicates some other problem.
 

The Following User Says Thank You to Joorin For This Useful Post:
Posts: 3,328 | Thanked: 4,476 times | Joined on May 2011 @ Poland
#3
It was some problem with "new-created" variable. I made it normail var and it's ok
 
Reply


 
Forum Jump


All times are GMT. The time now is 13:46.