C problem which I'm having problems with

From: Paul F. Johnson (paul_at_all-the-johnsons.co.uk)
Date: 12/12/04


Date: Sun, 12 Dec 2004 09:19:05 GMT

Hi,

I'm reworking some old C. The old version was held in a single mammoth
file which made debugging a pain in the backside.

I've farmed all of the globals (mostly enumerations) to variables.h and
fixed the compiler errors so the app now will compile happily again. I
can start the app, but as soon as I do anything, there is a seg fault.

I've traced the problem to some global char arrays.

Here's how things are set up

variables.h contains all the enumerations
arrays.h contains the following prototypes

extern char *metacharacter[];
extern char *commands[];
extern char line[200]; // current line
extern char tokens[100][30]; // tokens in the line

(I'll explain why line and tokens are done that way in a moment!)

arrays.c contains
#include "variables.h"

char *metacharacter[] = { ";", "&", "\n", ">", "<", ">>", "|", "@>s",
"@>c", "@<s", "@<c", " " };
char *commands[] = { "echo", "setenv", "getenv", "cd", " " };
char line[MAX_STRING_LENGTH]; // current line
char tokens[MAX_TOKENS][MAX_TOKEN_LENGTH]; // tokens in the line

Okay, there is an obvious potential problem there in that if I alter any
of the macros in array.c, array.h need to be altered.

Why the separation?

When I originally have these globals in array.h, the code would compile
fine, but on linking there were masses of errors as the global arrays
had already been defined.

The problem though seems to be coming from the a function which is
searching using metacharacter as a search string...

#include <string.h>
#include <stdio.h>
#include "lexical.h"
#include "variables.h"
#include "arrays.h"
#include "scribeserver.h"

/* other code here*/

int findString(char *strs[], char *str)
{
        int i = 0, stLen = strlen(str);
        printf("Inside findString. stLen = %d\n", stLen);
        // return the index of str in the string strs or NOT_FOUND
        while (strcmp (strs[i], "") != 0 && i < stLen)
        {
                printf("i = %d, strs[i] = %c\n", i, strs[i]);
                if (strcmp(strs[i], str) == 0)
                {
                        printf("returning i = %d\n");
                        return i;
                }
                else
                        ++i;
        }
        printf("Returning not found\n");
        return NOT_FOUND;
}

int tokenCode(char *token)
{
        return findString(metacharacter, token);
}

There is something odd going on - the program 100% of the time segfaults
in findString. When I've looked at the characters being searched, it's
obvious why - it looks pretty much undefined! (when the compiler hit the
tokenCode function, it gave a warning that parameter 1 was not the same
between the caller and reciever).

I've come across this problem before now, but can't for the life of me
remember how to fix it. Help!

TTFN

Paul

-- 
http://www.all-the-johnsons.co.uk
Joy!


Relevant Pages

  • Re: Function prefix comments in C files
    ... >> I have been searching for (not in a Google way, ... Years ago when using an Intel compiler for an 80C196KC, ... all function prototype are in one file: ... all globals are in one file: ...
    (comp.arch.embedded)
  • Re: C problem which Im having problems with
    ... >fixed the compiler errors so the app now will compile happily again. ... >When I originally have these globals in array.h, ... >searching using metacharacter as a search string... ... If this function is called with metacharacter as the first argument as ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Malcolms new book
    ... From a compiler writer's point of view, of course, it is important that if) is legal whilst if) is not. ... Similalry from an algorithms point of view we are not too interested in whether a function receives its parameters as arguements or in globals. ... returning void. ... A lot of C functions also have a primary purpose of ...
    (comp.lang.c)
  • Re: Function prefix comments in C files
    ... I know how and do use those methods David described when the ... 3272 on their 80C196 compiler is the document that describes in detail some ... > Where globals are regrettably present, it is reasonable to gather them ... ISRs though ANY and ALL variables modified by ISRs must have the volatile ...
    (comp.arch.embedded)
  • Re: Is UML appropriate for embedded systems ?
    ... >>I haven't used a gnu toolchain for an embedded project yet, ... The compiler can only see 1 file at time. ... I tend to use very very few globals so checking ... test code which was never put into the actual product, ...
    (comp.arch.embedded)

Loading