occasional stackdump created and errors on program run.

I'm having occasional errors when testing my program, I'm not quite sure. When this error occurs creates stackdump then will continue to produce the same error until I delete the stackdump.
only thing I could find on stackdump was: http://www.cplusplus.com/forum/general/4282/
which was completely useless. What is a stackdump?
Unfortunately the error didn't point to any specific line being it was exec, which is usually what I read when looking at errors it said something about char * std:: alloc etc..
I didn't quite get it .... hit the enter key too soon and ended up back in VI, and unfortunately being that I was using VI opening shell, when I quit to go check the error again it had scrolled off the term. I have tried to reproduce the error but it is not comming up at the moment, I assume I will get it again sooner or later I hope so i can try and figure out what is going on. There is only 1 if statement that it seemed to happen on a few times.
This is my code:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "header.h"

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main(int argc,char *argv[])
{
  int row=1;
  string iparam="";
  string oparam="";
  string usage="Usage: -i \"input_file\" -o \"output_file\"";

  // check if args have been supplied
  if (argc<=1)
  {
    cerr << usage << endl;
    exit(1);
  }
  // check switches
  else if (argv[1][0] != '-')
  {
    cerr << "invalid switch: " << argv[1] << endl;
    cerr << usage << endl;
    exit(1);
  }
  // stick in/out elements in string iparam/oparam
  while ((row<argc)&&(argv[row][0]=='-'))
  {
    string sw = argv[row];
    if (sw =="-i")
    {
      row++;
      iparam=argv[row];
    }
    else if (sw=="-o")
    {
      row++;
      oparam=argv[row];
    }
    else 
    {
      cerr << "unknown switch: " << argv[row] << endl;
      cerr << usage << endl;
      exit(1);
    }
    row++;
  }
  // check for duplicate filenames
  if (iparam==oparam)
  {
    cerr << "Duplicates found, use unique files." << endl;
    cerr << usage << endl;
    exit(1);
  }
  // this is the "if" i introduced that started giving me the errors
  // I had the previous code working and tested multiple times
  // before implementing this next conditional.

  // exit on more args than required:--
  if (argv[5][0]!=0)
  {
    cerr << "Too many args." << endl;
    cerr << usage << endl;
    exit(1);
  }

  // start app:
  StringSort ss(iparam,oparam);
  ss.runApp();
}


The bottom if or possibly the creating of StringSort object most likely causes, I don't think it would be the object because at the moment all it is taking iparam and oparam as constructor args and setting them, with a couple cout test statements in constructor, setters, and runApp...

I'll post back if I come across the error again, I just don't want to submit this knowing the code may be flawed, something I may be overlooking...

Thanks for reading so far.
Last edited on
ok got it again i tried this on another computer using cygwin I commented out the include header and lines 70,71 compiled fine then I ran the exe as:

./a -i iinnnn -o ouuttttttt
16 [main] a 3704 _cygtls::handle_exceptions: Error while dumping state<probably corrupted stack>
segmentation fault (core dumped)
debug the core using gdb and you will know where you got the exception.
if its a release binary, compile it with -g option and you can debug the core.
syntax:
gdb binary_name core

try it.
ok I have never used this before but gave it a shot,
running the exe with args ::
1
2
3
4
5
6
7
8
9
10
11
12
13
[New thread 5532.0x1794]
(no debugging symbols found)
(...)
(...)
(...)
[New thread 5532.0x12e0]
(no debugging symbols found)
(...)
(...)
(...)
Program recieved signal SIGSEGV, Segmentation fault.
0x004016a9 in ?? ()   
(gdb)  


now compile using -ggdb flag
now that's useful :D thanks a bunch, so now that gdb has confirmed that what I thought was correct, can I say now change this to if *argc>=5 and retest etc, and it should tell me if successful yea?
1
2
3
Program recieved signal SIGSEGV, seg fault.
0z092131 in main (argc=5, argv=0xde2178) at a2Main.cpp:62
62    if (argv[5][0]!=0)


Also in the manual it doesn't talk much about debugging symbols, is there a better resource to look up how to use debugging symbols in code?
Last edited on
I'm wondering why was the comparison against 0 throwing this seg fault?
I'm pretty sure i tried comparing to != NULL but it told me that it had to be an int which is why I changed to 0... doesn't matter too much anyway argc>5 is essentially the same.
You're doing it wrong. You're not supposed to check argv[i][0]. If you do that, the [0] dereferences the pointer and you get a segmentation fault when i==argc. You're supposed to check argv[i].
line 29: while ((row<argc)&&(argv[row][0]=='-')) is also going to cause this error?
I did read up on pointers before this, but assumed from what I read you can still treat them much like arrays, but I'm not quite sure what you mean....
are you saying that comparing [i][j] to 0 will be comparing against memory address?
Last edited on
If you are accessing element [i][j] of an array, it means you are still dereferencing the 2nd pointer (to get the j-th element), but if that pointer is NULL, then you can't deference it.
ah ok thanks.
Shit, I forgot to click the submit button. Oh, well.
Topic archived. No new replies allowed.