简体   繁体   中英

Why is this integer decrementing by itself?

Something tells me I'm doing something stupid. I haven't done any programming in a long time, and felt a bit rusty while writing this code. I'm sure I'll get back into the coding zen soon enough.

In the meantime, I'm have trouble with this code (specifically the tab1->history_position integer):

/* 
 * Created on February 17, 2011, 1:25 AM
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>


typedef struct dir_instance
{
    char path[PATH_MAX];
    char *history[PATH_MAX/2];
    int history_size;
    int history_position;

};


struct dir_instance *dir_new_instance(char *path)
{
    struct dir_instance inst;
    inst.history_position=0;
    inst.history_size=0;

    inst.history[0]=malloc(strlen(path));
    strcpy(inst.history[0], path);

    return &inst;
}

void dir_add_history(struct dir_instance *inst, char *dir)
{
    inst->history[inst->history_position+1]=malloc(strlen(dir)+1);
    strcpy(inst->history[inst->history_position+1], dir);
}



void dir_goto(struct dir_instance *inst, char *dir)
{
    dir_add_history(inst, dir);
    inst->history_position++;
    inst->history_size++;
}

void dir_go_back(struct dir_instance *inst)
{
    if(inst->history_position>0)inst->history_position--;
}

void dir_go_forward(struct dir_instance *inst)
{
    if(inst->history[inst->history_position+1]!=NULL)inst->history_position++;
}



int main(int argc, char **argv) {

    struct dir_instance *tab1=dir_new_instance("/");
    dir_goto(tab1, "/home");

    printf("the current directory is: %s\n",tab1->history[tab1->history_position]);

    printf("the previous directory is: %s\n",tab1->history[tab1->history_position]);

    return (EXIT_SUCCESS);
}

I'm not sure what funny business is going on here, but like I said, I do suspect a stupid mistake. What seems to be happening, is that the integer tab1->history_position is decremented from 1 to 0 on line 65. No idea why. Please inform me.

The dir_instance that you're creating is allocated on the stack. That means it's invalid once dir_new_instance returns. Allocate it using malloc instead:

struct dir_instance *dir_new_instance(char *path)
{
    struct dir_instance* inst = (struct dir_instance*) malloc(sizeof(dir_instance));
    inst->history_position=0;
    inst->history_size=0;

    inst->history[0]=malloc(strlen(path + 1));
    strcpy(inst->history[0], path);

    return inst;
}

EDIT : Note the change to add 1 to the length of the string returned by strlen . You need that to allow for the terminating null character. (Many implementations have a strdup function which returns a malloc'd copy of the string, eliminating this error, but strdup is non-standard.)

this here is wrong:

inst.history[0]=malloc(strlen(path));
strcpy(inst.history[0], path);

you should allocate strlen(path)+1 to allow for the \\0 to fit in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM