 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chris Wallace Guest
|
Posted: Fri Jul 30, 2004 9:36 am Post subject: cout statement stopping a seg fault that occurs with an arra |
|
|
I have encountered an extremely bizarre error that I cannot explain.
I sorta have a workaround but I don't trust it and it certainly makes
no sense. If anyone sees what is going wrong I would greatly
appreciate an explanation.
Here is what I consider the important information from the code:
char* strmHeader;
const char* name = "TraceField: " (name gets this value from a
function passing in the stuff
in quotes)
int namesize = 13; (the number of characters in 'name')
Normally, I will get a seg fault as soon as i hit the following line
of code(according to gdb):
strmHeader = new char[namesize];
If I add this as a debug statement right above the previous line of
code, the seg fault will go away!
cout << " "; (anything printed will do actually)
I personally cannot see anything wrong with this assignment and that
'cout' statement should have no effect on anything other code, yet it
does. What is going on here? I'd really like to not have such a
cheesy hack in my code......
Any help would be greatly appreciated.
thanks in advance
-Chris Wallace
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Jul 30, 2004 3:28 pm Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
In article <dfeb6b26.0407291607.7c97769e (AT) posting (DOT) google.com>, Chris
Wallace <wallac1 (AT) stamps (DOT) stortek.com> writes
| Quote: | I have encountered an extremely bizarre error that I cannot explain.
I sorta have a workaround but I don't trust it and it certainly makes
no sense. If anyone sees what is going wrong I would greatly
appreciate an explanation.
|
You need to post a minimalist function that exhibits this problem.
Without that we would be guessing. Segment faults are often the result
of undefined behaviour, but not necessarily in the immediate code. UB
can appear long after the cause.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Vinayak Raghuvamshi Guest
|
Posted: Sat Jul 31, 2004 3:09 am Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
[email]wallac1 (AT) stamps (DOT) stortek.com[/email] (Chris Wallace) wrote in message news:<dfeb6b26.0407291607.7c97769e (AT) posting (DOT) google.com>...
| Quote: | I have encountered an extremely bizarre error that I cannot explain.
I sorta have a workaround but I don't trust it and it certainly makes
no sense. If anyone sees what is going wrong I would greatly
appreciate an explanation.
Here is what I consider the important information from the code:
char* strmHeader;
const char* name = "TraceField: " (name gets this value from a
function passing in the stuff
in quotes)
int namesize = 13; (the number of characters in 'name')
Normally, I will get a seg fault as soon as i hit the following line
of code(according to gdb):
strmHeader = new char[namesize];
If I add this as a debug statement right above the previous line of
code, the seg fault will go away!
cout << " "; (anything printed will do actually)
I personally cannot see anything wrong with this assignment and that
'cout' statement should have no effect on anything other code, yet it
does. What is going on here? I'd really like to not have such a
cheesy hack in my code......
Any help would be greatly appreciated.
thanks in advance
|
if your are seeing a crash in this statement:
strmHeader = new char[namesize];
then that most probably means that your heap got corrupted by some
operation that was executed before you reached this statement.
why does a cout mask this? I cant say for sure. But it all depends on
which mem location your heap operation is trying to access. You can
try to replace your statement with something like strmHeader = new
char[1]; and see if that crashes too, if not, then that explains part
of the wierdness.
in any case, i think your sample code is over simplified and it would
help to see your actual code that causes teh crash....
-Vinayak
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Guest
|
Posted: Sat Jul 31, 2004 3:40 am Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
On 30 Jul 2004 11:28:32 -0400, Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk>
wrote:
| Quote: | In article <dfeb6b26.0407291607.7c97769e (AT) posting (DOT) google.com>, Chris
Wallace <wallac1 (AT) stamps (DOT) stortek.com> writes
I have encountered an extremely bizarre error that I cannot explain.
I sorta have a workaround but I don't trust it and it certainly makes
no sense. If anyone sees what is going wrong I would greatly
appreciate an explanation.
You need to post a minimalist function that exhibits this problem.
Without that we would be guessing. Segment faults are often the result
of undefined behaviour, but not necessarily in the immediate code. UB
can appear long after the cause.
|
my doughnuts say he didn't exit(0) at the end of main(). of course, this also
requires the inclusion of cstdlib...
peace,
cj
--
===============================================================================
Christopher Jon Miller Drink and dance and laugh and lie
Parallel Systems Engineer Love, the reeling midnight through
For tomorrow we shall die!
(But, alas, we never do.)
-- Dorothy Parker, "The Flaw in Paganism"
===============================================================================
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marcelo Pinto Guest
|
Posted: Sat Jul 31, 2004 3:46 am Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
[email]wallac1 (AT) stamps (DOT) stortek.com[/email] (Chris Wallace) wrote in message news:<dfeb6b26.0407291607.7c97769e (AT) posting (DOT) google.com>...
| Quote: | I have encountered an extremely bizarre error that I cannot explain.
I sorta have a workaround but I don't trust it and it certainly makes
no sense. If anyone sees what is going wrong I would greatly
appreciate an explanation.
Here is what I consider the important information from the code:
char* strmHeader;
const char* name = "TraceField: " (name gets this value from a
function passing in the stuff
in quotes)
int namesize = 13; (the number of characters in 'name')
Normally, I will get a seg fault as soon as i hit the following line
of code(according to gdb):
strmHeader = new char[namesize];
If I add this as a debug statement right above the previous line of
code, the seg fault will go away!
cout << " "; (anything printed will do actually)
I personally cannot see anything wrong with this assignment and that
'cout' statement should have no effect on anything other code, yet it
does. What is going on here? I'd really like to not have such a
cheesy hack in my code......
Any help would be greatly appreciated.
thanks in advance
-Chris Wallace
|
I tried the following code and it didn't behave wildly:
int main()
{
char* strmHeader;
const char* name = "TraceField: ";
int namesize = 13;
strmHeader = new char[namesize];
delete[] strmHeader;
return 0;
}
</code>
There is somenthing from your code that is missing in your message
that is probably causing the problem.
Every time I faced wilds problems, they were caused by memory
violations. These are the most dificult (IMO) bugs to catch.
Could you post more code from your real problem, specially the code of
the function that is returning to name?
Best regards,
Marcelo Pinto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Chris Wallace Guest
|
Posted: Wed Aug 04, 2004 12:20 pm Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
Sorry if the code was oversimiplified, but I felt that the post would
turn into a huge mess of code if I posted all the details.
Coincidentally, I too tried making the "problematic" code at using a
different compiler and it didn't have a problem, so I think the
problem is some interaction or bug with the compiler (trying to go
from gcc 2.8.1 to gcc 3.3). Incase the code is some how non-standard
and that is what is causing the problem, I'll post the complete
functions where the bail out occurs.
This is the function where the seg fault occurs. I know there is some
code in there that is not needed, but it is the result of me trying to
fix this problem. The function still exibits the same behavior no
matter how I change it up. I also tried putting in the constant '13'
to make the new char array, but it also resulted in a seg fault.
template <class T>
void List<T>::setStreamHeader(const char* name) {
int count = 0;
// Clear out old name
delete [] strmHeader;
// Copy input string if not NULL
if (name != NULL)
{
if (name == "TraceField: ")
{
count = 13;
cout << " "; //seg fault if not before the statement below
strmHeader = new char[count];
}
else
{
strmHeader = new char[strlen(name)+1];
}
strcpy(strmHeader, name);
}
else {
strmHeader = NULL;
}
}
This is the function that calls setStreamHeader and causes the
segfault. Note that setStreamHeader gets called in a couple of other
places and no seg faults occur then. this function is the only one
that passes the arguement "TraceField: " to name.
TraceField::TraceField(){
// Define TraceField stream labels
tdgs.setStreamHeader("TraceField: ");
}
Thanks for again
-Chris
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Aug 05, 2004 3:19 am Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
In article <dfeb6b26.0408030751.5d5ecd8 (AT) posting (DOT) google.com>, Chris
Wallace <wallac1 (AT) stamps (DOT) stortek.com> writes
| Quote: | Sorry if the code was oversimiplified, but I felt that the post would
turn into a huge mess of code if I posted all the details.
Coincidentally, I too tried making the "problematic" code at using a
different compiler and it didn't have a problem, so I think the
problem is some interaction or bug with the compiler (trying to go
from gcc 2.8.1 to gcc 3.3).
|
However this kind of symptom is absolutely classic undefined behaviour.
And one problem with that is that undefined behaviour can manifest long
after it has been triggered.
| Quote: | Incase the code is some how non-standard
and that is what is causing the problem, I'll post the complete
functions where the bail out occurs.
This is the function where the seg fault occurs. I know there is some
code in there that is not needed, but it is the result of me trying to
fix this problem. The function still exibits the same behavior no
matter how I change it up. I also tried putting in the constant '13'
to make the new char array, but it also resulted in a seg fault.
|
So does it always trigger if the string length is 13?
| Quote: |
template <class T
void List
int count = 0;
// Clear out old name
delete [] strmHeader;
// Copy input string if not NULL
if (name != NULL)
{
if (name == "TraceField: ")
|
Well the above is at least part of the problem because C++ does not
support operator== for arrays. At best it is comparing the address in
name with the address of the literal. You need to use strcmp().
| Quote: | {
count = 13;
cout << " "; //seg fault if not before the statement below
strmHeader = new char[count];
}
else
{
strmHeader = new char[strlen(name)+1];
}
strcpy(strmHeader, name);
}
else {
strmHeader = NULL;
}
}
This is the function that calls setStreamHeader and causes the
segfault. Note that setStreamHeader gets called in a couple of other
places and no seg faults occur then.
|
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Chris Wallace Guest
|
Posted: Fri Aug 06, 2004 10:19 am Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: | In article <dfeb6b26.0408030751.5d5ecd8 (AT) posting (DOT) google.com>, Chris
Wallace <wallac1 (AT) stamps (DOT) stortek.com> writes
Sorry if the code was oversimiplified, but I felt that the post would
turn into a huge mess of code if I posted all the details.
Coincidentally, I too tried making the "problematic" code at using a
different compiler and it didn't have a problem, so I think the
problem is some interaction or bug with the compiler (trying to go
from gcc 2.8.1 to gcc 3.3).
However this kind of symptom is absolutely classic undefined behaviour.
And one problem with that is that undefined behaviour can manifest long
after it has been triggered.
Incase the code is some how non-standard
and that is what is causing the problem, I'll post the complete
functions where the bail out occurs.
This is the function where the seg fault occurs. I know there is some
code in there that is not needed, but it is the result of me trying to
fix this problem. The function still exibits the same behavior no
matter how I change it up. I also tried putting in the constant '13'
to make the new char array, but it also resulted in a seg fault.
So does it always trigger if the string length is 13?
template <class T
void List
int count = 0;
// Clear out old name
delete [] strmHeader;
// Copy input string if not NULL
if (name != NULL)
{
if (name == "TraceField: ")
Well the above is at least part of the problem because C++ does not
support operator== for arrays. At best it is comparing the address in
name with the address of the literal. You need to use strcmp().
{
count = 13;
cout << " "; //seg fault if not before the statement below
strmHeader = new char[count];
}
else
{
strmHeader = new char[strlen(name)+1];
}
strcpy(strmHeader, name);
}
else {
strmHeader = NULL;
}
}
This is the function that calls setStreamHeader and causes the
segfault. Note that setStreamHeader gets called in a couple of other
places and no seg faults occur then.
|
Heh, I guess I got lucky with that name == TraceField statment, its
the only thing with a 'T' so it works. But since that was code that
I added trying to solve this and determine the location of the
problem, I have since removed it and gone back to how it was
originally since the behavior was exactly the same(keeping the cout
for the time being though). It atleast looks cleaner.
here is how it looked originally + the cout statement
if (name != NULL)
{
cout << " ";
strmHeader = new char[strlen(name)+1]; //seg fault here
strcpy(strmHeader, name);
}
else
{
strmHeader = NULL;
}
Granted I do not fully understand how everything can interact with
each at runtime, but it seemed like that new char statement where the
seg fault occurs is entirely independent of everything else in the
code so that is why I did not suspect something else earlier was at
fault, especially since this code used to run fine on the older
compiler.
| Quote: | So does it always trigger if the string length is 13?
yes, to the best of my knowledge, I have tried creating that char |
array a bunch of ways (char[strlen(name)]], char[count], and
char[13])...all do it. I have yet to try it using a different string
of equal length though.
Thanks for all the help.
-Chris
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Aug 06, 2004 2:47 pm Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
In article <dfeb6b26.0408050707.620438f4 (AT) posting (DOT) google.com>, Chris
Wallace <wallac1 (AT) stamps (DOT) stortek.com> writes
| Quote: | So does it always trigger if the string length is 13?
yes, to the best of my knowledge, I have tried creating that char
array a bunch of ways (char[strlen(name)]], char[count], and
char[13])...all do it. I have yet to try it using a different string
of equal length though.
|
I would be much more interested to know if use of 14, or 12 with a
shorter string also failed. If it works for all values less than 13 and
fails for all values greater than 13 that might be helpful. Nonetheless
I feel certain that your problem is earlier.
However are you using an implementation that throws an exception when
memory allocation fails? If not try checking that strmHeader is non-Null
after the call to new.
Another remote possibility is that the value passed in for name is not
null terminated.
Whatever, I am certain that all cout << " "; is doing is obscuring some
form of UB. While I am thinking of possible diagnostics, try including:
cout << name << 'n';
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Mon Aug 09, 2004 9:30 am Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
[email]wallac1 (AT) stamps (DOT) stortek.com[/email] (Chris Wallace) wrote:
| Quote: | yes, to the best of my knowledge, I have tried creating that char
array a bunch of ways (char[strlen(name)]], char[count], and
char[13])...all do it. I have yet to try it using a different string
of equal length though.
|
You can't do the first two of those in standard C++, if your compiler
allows it then it is a non-standard extension. The thing inside the
[] must be a constant expression.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Fri Aug 13, 2004 1:53 pm Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
[email]wallac1 (AT) stamps (DOT) stortek.com[/email] (Chris Wallace) wrote
| Quote: | Chris Wallace <wallac1 (AT) stamps (DOT) stortek.com> writes
Sorry if the code was oversimiplified, but I felt that the post would
turn into a huge mess of code if I posted all the details.
I'm sure that's true... you need to cut your program down far enough, but |
not too far. The smallest program that exhibits the problem.
Sometimes, just the act of creating such a program helps YOU to see where
the problem is, so you don't have to post it after all.
| Quote: | Coincidentally, I too tried making the "problematic" code at using a
different compiler and it didn't have a problem, so I think the
problem is some interaction or bug with the compiler (trying to go
from gcc 2.8.1 to gcc 3.3).
|
You are dealing with Undefined Behavior (UB). The problem might exhibit
itself in different ways with different compilers; you might not even
notice the problems sometimes; but it's still a problem.
Blaming the compiler should be your absolute LAST recourse. Chances are
it was a problem somewhere in your own source code, where you're doing
something you shouldn't do.
| Quote: | Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote
However this kind of symptom is absolutely classic undefined behaviour.
And one problem with that is that undefined behaviour can manifest long
after it has been triggered.
|
Right. Here's an example:
char * name; // Uninitialized
cout << "What is your name: ";
cin >> name; // Reads to some RANDOM location!
cout << "Hello, " << name << "!" << endl;
Without the comments, the problem in the above code can sometimes be
difficult to spot -- but it's still there. Unfortunately, if the
random uninitialized contents of pointer name happens to be an address
of some real memory, it's very likely that cin>>name will simply erase
that memory, writing the name there instead. If that memory isn't
used in the next few statements, then you might not notice the problem
until seconds or even minutes later -- when a completely different
part of your program (say, function List<T>::setStreamHeader) is running.
One possible (but by no means conclusive) problem is that you wrote
into memory that is used to allocate memory from the heap. This can
be as simple as overwriting a heap buffer:
char *name = new char[5];
strcpy(name, "Hello"); // Oops! The null terminator overwrites something
In my experience, the vast majority of UB is caused by misuse of pointers.
You can't know for sure that your problem was caused by this, but that's
the first thing to look for.
| Quote: | template <class T
void List
int count = 0;
// Clear out old name
delete [] strmHeader;
// Copy input string if not NULL
if (name != NULL)
{
if (name == "TraceField: ")
Well the above is at least part of the problem because C++ does not
support operator== for arrays. At best it is comparing the address in
name with the address of the literal.
|
At worst, too. Perhaps your compiler rolls string literals together;
otherwise, the expressin in the if() statement should always be FALSE.
| Quote: | You need to use strcmp().
|
Like this:
if (strcmp(name, "TraceField: ")==0)
| Quote: | {
count = 13;
cout << " "; //seg fault if not before the statement below
strmHeader = new char[count];
}
else
{
strmHeader = new char[strlen(name)+1];
}
strcpy(strmHeader, name);
}
else {
strmHeader = NULL;
}
}
|
This probably isn't where the error is.
| Quote: | Heh, I guess I got lucky with that name == TraceField statment, its
the only thing with a 'T' so it works.
|
Some would call this BAD luck -- it's bad if your program keeps working
until the day of the big demo...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Fri Aug 13, 2004 1:55 pm Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
| Quote: | wallac1 (AT) stamps (DOT) stortek.com (Chris Wallace) wrote:
yes, to the best of my knowledge, I have tried creating that char
array a bunch of ways (char[strlen(name)]], char[count], and
char[13])...all do it. I have yet to try it using a different string
of equal length though.
|
[email]oldwolf (AT) inspire (DOT) net.nz[/email] (Old Wolf) wrote
| Quote: | You can't do the first two of those in standard C++, if your compiler
allows it then it is a non-standard extension. The thing inside the
[] must be a constant expression.
|
I'm pretty sure that Chris Wallace meant that he used each of those
as part of an operator new, i.e
strmHeader = new char[strlen(name)]; // Minor typo fixed
strmHeader = new char[count];
strmHeader = new char[13];
In each case he was allocating 13 characters.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Fri Aug 13, 2004 1:56 pm Post subject: Re: cout statement stopping a seg fault that occurs with an |
|
|
| Quote: | Chris Wallace <wallac1 (AT) stamps (DOT) stortek.com> writes
I have encountered an extremely bizarre error that I cannot explain.
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote:
You need to post a minimalist function that exhibits this problem.
Without that we would be guessing. Segment faults are often the result
of undefined behaviour, but not necessarily in the immediate code. UB
can appear long after the cause.
|
[email]cj (AT) bird (DOT) hoax.qwest.net[/email] () wrote
| Quote: | my doughnuts say he didn't exit(0) at the end of main().
|
Your doughnuts might be very wise. But even assuming that they are right,
how could this be related to the problem?
1. You aren't required to call exit at the end of main.
2. A return 0 statement in main has the same effect.
3. If you don't call exit or return 0, it's supposed to have
the same effect as return 0.
4. Even if failing to return 0 from main was an error, how could this
cause a problem in an operator new expression that runs LONG before
the end of main is reached?
| Quote: | of course, this also requires the inclusion of cstdlib...
|
I think maybe your doughnuts should explain further.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|