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 8038445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:14:18+00:00 2026-06-05T03:14:18+00:00

I have this function: char * folderFromPath(char *path) { printf(\nentered folderFromPath\n); char *token[80]; int

  • 0

I have this function:

char * folderFromPath(char *path)
{
  printf("\nentered folderFromPath\n");

  char *token[80];
  int i = 0;  
  const int STR_LEN = 128;
  char str[STR_LEN];
  char *folder;
  folder = malloc(sizeof(path));
  strcpy(folder,"/");

  if (strlen(path) > STR_LEN)
  {
    printf("Warning: strlen(path) > STR_LEN, (%d > %d) in function folderFromPath\n", strlen(path), STR_LEN);
  }
  else
  {
      printf("path: %s\n", path);

    strcpy(str,path);

    token[0] = strtok(str, "/");

    while (token[i]!= NULL)
    {
      i++;
      token[i] = strtok (NULL, "/");
      printf("token[i]: %s, i: %d\n", token[i], i);
    }

    if (folder != NULL)
    {
        int j = 0;
        while (j < (i-1))
        {
              strcat(folder,token[j]);
              strcat(folder,"/");
            j++;
        }

        printf("folder: %s\n", folder);

    }

  } /* else if (strlen(path) < STR_LEN) */

  return folder;

}

In it you can see that I have dynamically allocated memory that is pointed to by folder. You can also see that folder is returned to the calling function. I saw in this post where it was suggested to free the pointer after it is used in the calling function. So that is what I have done. Here is the calling function:

void open_activated(GtkWidget *widget, GtkWindow *parent)
{
  GtkSourceLanguage *lang;
  GtkSourceLanguageManager *lm;
  GtkWidget *dialog;
  GtkWidget *tablabel;
  GtkTextBuffer *tbuffer;
  int openTabs = 0;
  char *folder1;
  const gchar *folder2;
  int page = 0;
  char *path;

  page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
  path  = paths[notebookPages[page]];
  folder1 = folderFromPath(path);
  folder2 = folder1;

  dialog = gtk_file_chooser_dialog_new("Open File", parent, GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL);
  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), folder2);
  free(folder1); 

  tbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[openedPages]));

  gtk_source_buffer_begin_not_undoable_action(GTK_SOURCE_BUFFER(tbuffer));

  if(gtk_dialog_run(GTK_DIALOG(dialog))== GTK_RESPONSE_ACCEPT)
  {

    ...

  } /* if(gtk_dialog_run(GTK_DIALOG(dialog))== GTK_RESPONSE_ACCEPT) */

  gtk_widget_destroy(dialog);
  changeLabelColor("black");
  gtk_text_buffer_set_modified (gtk_text_view_get_buffer((GTK_TEXT_VIEW(txtinput[openedPages]))), FALSE);

  gtk_source_buffer_end_not_undoable_action(GTK_SOURCE_BUFFER(tbuffer));
  write_config_files();

  verifyPaths();  

}

When I try to open a file, the application aborts and produces this statement:

*** glibc detected *** ./ledit: free(): invalid next size (fast): 0x082a80c8 ***

So my question is what does this error mean and what should I do differently to properly free the pointer? Thanks.

  • 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-06-05T03:14:19+00:00Added an answer on June 5, 2026 at 3:14 am

    The main problem is probably this.

    char * folderFromPath(char *path)
    ...
    folder = malloc(sizeof(path));
    

    You are allocating sizeof(path), which is the same as sizeof(char*), since path is a char*. You’re only allocating enough bytes to hold a single pointer, rather than the whole path which is probably what you intended.

    Try instead:

    folder = malloc(strlen(path)+1);
    

    There may well be other problems; I haven’t looked very closely. It does look as if you are correctly freeing the return folder after passing it to gtk_file_chooser_set_current_folder(), although I don’t know why you are assigning it to folder2 as well. If you are expecting that assignment to copy the string (does the GTK function expect to take ownership of the path?) then you will be disappointed; you will have to take a separate copy of the string using strncpy() or something similar.

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

Sidebar

Related Questions

I have this function in my program const char* Graph::toChar() { std::string str; const
if I have this function: printAll(const char *message, ...) { va_list argptr = NULL;
I have this function: char* ReadBlock(fstream& stream, int size) { char* memblock; memblock =
I have a dll which contains this function: int __stdcall PrnText(char *printtext); In Windows
I have this function that converts an integer to a std::string: std::string intToStr(const int
I have this function: void receive_message(int sock, char buffer[]) { int test = recv(sock,
I made this function: char** parse_cmd(const char* cmdline) { int i; int j =
i have this function char* copy(char* pString,...){ char *vToate; int vLen; va_list vAp; va_start(vAp,pString);
I have this function: int setIncludes(char *includes[]); I don't know how many values includes
Assume this C function: void do_something(const char* str) It stores the string somewhere for

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.