0

Got a problem which to me make no sense. So here goes:

I have a function that counts how many times a word appears in a file, thus this function return a integer (int). So on another function it uses the "counter". Now for some reason it decided to start launching a stack smashing detected error. I had been testing it for 2 weeks the whole program and it worked to perfection. Now I get that error, which really makes no sense. What in the world is going on? And the error is right there, after the function has counter and it return, it launches the stack smashing detected error.

Edit: I keep searching, and yes i get a stack smashing detected error when returning a int function. Any ideas? If i take that code out, it does not crash. Really i have no idea

Any suggestion?

Thanks...

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Alessandroempire
  • 1,640
  • 4
  • 31
  • 54

1 Answers1

2

May I suggest compiling your program with debugging information and running it under Valgrind? See also this related question.

If you need it, I have posted some hints on using Valgrind in an older answer of mine.

Community
  • 1
  • 1
thkala
  • 84,049
  • 23
  • 157
  • 201
  • i am open to any suggestions. I will give it a try. Still it worries me to get this error while returning a int function... – Alessandroempire Jun 21 '12 at 17:54
  • @Alessandroempire: it's quite probable that the actual error lies elsewhere. Give Valgrind a try - it generally knows what your program is doing better than you do... – thkala Jun 21 '12 at 17:56
  • yeah i though the same, but is it too much of a coincidence that when i take away that function it works? I'll let you know if i find anything with valgrind – Alessandroempire Jun 21 '12 at 18:00
  • @Alessandroempire: Removing or even just rearranging code changes the memory layout of the program. It could easily conceal or reveal an existing bug... – thkala Jun 21 '12 at 18:01
  • found the error. In the function to count how many times a word appears on a file, first i use fopen to then use fscan( file_decriptor, "%s", buffer). Where buffer is a char [20]. Now when i make buffer larger i dont get the problem. Why? for some reason i cant understand, hes reading a word that has more than 20 char. So if you make it 50 it does not throw the error. Ill give it a better look, but sound about right why i got the stack overflow. thoughts? – Alessandroempire Jun 21 '12 at 18:16
  • 1. C "strings" need one additional byte for the terminating '\0'. Your `buffer[20]` can only fit words of 19 characters. 2. Maybe one of the words is longer than 19 characters. 3. The error might still be elsewhere. Resizing arrays once again changes the memory layout... – thkala Jun 21 '12 at 18:21
  • that makes life much easier! ill re run the program with valgrind in a few. Need to finish testing some new features. thanks for all the help! – Alessandroempire Jun 21 '12 at 19:44
  • I am using valgrind to find my error. i used valgrind --log-file=valgrind.log --leak-check=full --track-origins=yes --show-reachable=yes ./program it return a log file and i am reading it, but i am not understanding it much. think you can give me a hand with that? i posted the logfile on another question. – Alessandroempire Jun 22 '12 at 17:42