Re: Why segfault and no NULL match in for loop?



On Wed, 02 May 2007 20:14:34 -0400, CBFalconer <cbfalconer@xxxxxxxxx>
wrote:

somebody wrote:

There are two files below named search.c and search.h. In the for
loop in search.c, the for loop never exits, even if
mystruct[i].field1 has no match. Instead of exiting the for loop
it keeps going until it segfaults. This seems to be related to the
strcmp with the NULL value. There are 2 comments below that
indicate the segfaults. I guess the question is, when there is no
match, how to I detect that and return without a segfault?

... snip to content of search.h ...

struct _mystruct mystruct[] =
{
"AAA", "EEE", "R", "DF", 25,
"BBB", "FFF", "R", "DF", 25,
"CCC", "GGG", "R", "DF", 99,
"DDD", "HHH", "R", "DF", 13,
};

NEVER define an actual object in a .h file. Doing so will cause it
to be multiply defined, and result in link or run time errors. Add
the word "extrn" to the definition, and delete the initialization.
Put the above structure in ONLY one of the files in which you
#include "search.h".

In addition to the correction noted by Keith ("extrn" should be
"extern"), it should be noted that defining an actual object in a .h
file does not necessarily mean that it will be multiply defined and
result in a link or run time error.

If the header file is included by a single translation unit, there
will be no link or run time errors. You only run into trouble when
multiple translation units include said header file and *think* they
are accessing the same data (something I have witnessed in practice).

Why would you ever want to define an actual object in a .h file? I can
think of only one instance, which is if the .h file is shared between
two or more programs (and included by a single translation unit in
each, of course). I've used this technique, but only sparingly. Think
of gcc and gcov using the same objects with file scope internal
linkage, as an example (I'm not saying they do this, just that it is
an acceptable option, IMHO, especially if there are a lot of the same
objects used in both).

(My actual usage of the above is almost the same code running as
programs/processes on two dissimilar processors, which was one of the
requirements necessary to achieve a certain level of system safety
assurance).

NEVER define an actual object in a .h file, unless you really know
what you're doing.

A .h file is NOT a place for headers, it is a place to define the
data you want to export from a .c file.

Saying a ".h file is NOT a place for headers" is like saying a "house
is not a place for a home". I think you meant "objects" instead of
"headers".

Best regards
--
jay
.



Relevant Pages

  • Re: Office 2003 Installation Problem
    ... Requests for assistance by email can not and will not be acknowledged. ... to install" followed by a box saying "please wait while Windows configures MS ... then goes through the same "install/configure" loop. ...
    (microsoft.public.office.setup)
  • Re: Looping to find status
    ... Are you saying that you have 7 fields in your table, each field holding a 1, ... This might be handled in a query, using IIF() statements (untested ... a loop, but not sure how I could loop through them. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Performance: Iterating a vector in a loop..
    ... > But he was saying its slow because .beginand .endare called every ... > loop. ... > I know that the function is called every iteration, ... > any speed hits when doing that, or, if it would be better to re-write the ...
    (microsoft.public.vc.language)
  • Re: OT: Stuff that just works
    ... > Try saying "it just works" when you have ... > ya trainers tied with too big a loop in the laces and it hooks round the ...
    (uk.comp.sys.mac)