C problem which I'm having problems with
From: Paul F. Johnson (paul_at_all-the-johnsons.co.uk)
Date: 12/12/04
- Next message: B. v Ingen Schenau: "Re: Examples Heap Sort - problem"
- Previous message: Ulrich Eckhardt: "Re: Examples Heap Sort - problem"
- Next in thread: B. v Ingen Schenau: "Re: C problem which I'm having problems with"
- Reply: B. v Ingen Schenau: "Re: C problem which I'm having problems with"
- Reply: Barry Schwarz: "Re: C problem which I'm having problems with"
- Reply: Zach: "Re: C problem which I'm having problems with"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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!
- Next message: B. v Ingen Schenau: "Re: Examples Heap Sort - problem"
- Previous message: Ulrich Eckhardt: "Re: Examples Heap Sort - problem"
- Next in thread: B. v Ingen Schenau: "Re: C problem which I'm having problems with"
- Reply: B. v Ingen Schenau: "Re: C problem which I'm having problems with"
- Reply: Barry Schwarz: "Re: C problem which I'm having problems with"
- Reply: Zach: "Re: C problem which I'm having problems with"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|