Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Segmentation Fault On Number Increment

Status
Not open for further replies.

OzWolf

Programmer
May 22, 2002
52
0
0
AU
Hey folks,

I'll be the first to admit that the last time I did any serious C programming was over a decade ago at uni. Up until today, my work with C has been basic maintenance and minor code changes.

Anyway, I'm running into a segmentation fault that just has me reviving my days in the Navy with cursing.

Here's the function causing the problem:

Code:
int dir_parts(char *dir_name){
    int i;
    int j;
    char c[2];

    j = 0;

    for (i = 0; i < strlen(dir_name); i++){
        strcpy(c, getsub(dir_name, i, i));
        if (strcmp(c, "/") == 0){
            j++;
        }
    }

    if (strcmp(c, "/") != 0){
        j++;
    }

    return(j);
}

The bit in particular is the last j++ statement. It's driving me spare. If I just put a printf in there with the strcmp outcome, the ENTIRE program runs fine. If I put anything except the j++ line in, the program runs fine (though the function returns a number one less than the intended amount).

After figuring out my char memory problems, this one has me wanting a stiff drink.

Any help greatly appreciated.
 
> getsub(dir_name, i, i)
You seem to be copying increasingly long strings into an array of only 2 characters (thus trashing something else).

An alternative is using strchr() to find each /, then resume the search from the next char in the string.
This has the advantage of no copying.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
What for strcmp or strchr? Keep it simple:
Code:
for (i = j = 0; fir_name[i]; ++i)
    if (dir_name[i] == '/')
       ++j;
 
Thanks for the tips guys. I changed the function to:

Code:
int dir_parts(char *dir_name){
    int i;
    int j;
    char c[2];

    for (i = j = 0; dir_name[i]; ++i)
        if (dir_name[i] == '/')
            ++j;

    if (dir_name[i] != '/'){
        ++j;
    }

    return(j);
}

But I still get a core dump on the code:

Code:
if (dir_name[i-1] != '/'){
    ++j;
}

Both my original code and the modified one above worked fine until I put the extra j increment in the if-statement. I'm assuming because I can put a successful printf("%c\n", dir_name[i-1]) into the if-statement and output the last character of dir_name, the problem is with the increment of j, not of anything to do with the string, which is what is bugging the hell out of me.
 
You place this statement in the function body:
Code:
    if (dir_name[i] != '/'){
        ++j;
Why?
dir_name == '\0' after loop (see loop condition).
If no slashes in the dir_name
then j == 0 - and you have wrong memory access
in dir_name[-1]...
 
I've fixed the issue. Obviously the ++j was the last straw in a memory issue elsewhere.

After a re-coding of the program, the last function I wrote out works without a hitch.

And that if statement was supposed to be:

Code:
if (dir_name[i-1] != '/'){
    ++j;
}

Primary reason for the last bit is if the user-entered directory this is helping create bread-crumbs for is missing a / from the end (ie. /archive/files instead or /archive/files/).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top