Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3986024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:57:43+00:00 2026-05-20T05:57:43+00:00

Something tells me I’m doing something stupid. I haven’t done any programming in a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:57:44+00:00Added an answer on May 20, 2026 at 5:57 am

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe I should just keep my mouth shut and move forward, but something tells
Is there any option to tell gs to convert strokes to fills? Something similar
This works, but something tells me I really should do it in SQL. Which
Something tells me I might get lynched for asking this. And sorry for the
Name of the option tells something but what Visual Studio/compiler really do and what
Sometimes I read EAGL, openAL ... wikipedia doesn't tell me something about EAGL.
can anyone tell me what is the best way to do something like, from
I'm looking for something to add to my .vimrc that would tell MacVim to
Something terrible as happened with my poor attempt at adding a CountIf forumula to
Something is wrong with my Rails app because when I run bundle install, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.