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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:41:27+00:00 2026-06-11T11:41:27+00:00

I am trying to append a character to a string… that works fine unfortunately

  • 0

I am trying to append a character to a string… that works fine unfortunately I can’t free the mem of the string afterwards which causes that the string gets longer and longer…. as it reads a file every linie will be added to the string which obviously shouldn’t happen

char* append_char(char* string, char character)
{
        int length = strlen(string);
        string[length] = character;
        string[length+1] = '\0';
        return string;
}

I allocated mem for string like

char *read_string = (char *)malloc(sizeof(char)*500);

call the function append_char(read_string,buffer[0]); and free it after the whole string is build free(read_string);

I presume that once I call the append_char() , the mem allocation is going to be changed, which cause that I can’t get hold of it.

Edited:
here is the function which uses the append_char()

char *read_log_file_row(char *result,int t)
{
filepath ="//home/,,,,,/mmm.txt";
int max = sizeof(char)*2;
char buffer[max];
char *return_fgets;

char *read_string = malloc(sizeof(char)*500);

file_pointer = fopen(filepath,"r");

if(file_pointer == NULL)
{
    printf("Didn't work....");
    return NULL;
}   

int i = 0;

while(i<=t)
{
  while(return_fgets = (fgets(buffer, max, file_pointer)))
  {
    if(buffer[0] == '\n') 
    {
       ++i;
       break;   
    }   

        if(i==t)
    {
      append_char(read_string,buffer[0]);
     }      
   }

   if(return_fgets == NULL)
   {
      free(read_string);
      return NULL;                              
/*              return "\0";*/
        }
       if(buffer[0] != '\n') 
        append_char(read_string,buffer[0]);

   }    
   fclose(file_pointer);
    strcpy(result,read_string); 
    free(read_string);
   return result;
}
  • 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-11T11:41:28+00:00Added an answer on June 11, 2026 at 11:41 am
    1. Dont cast the return value of malloc() in C.
    2. Make sure you initialize read_string to an empty string before you try to append to it, by setting read_string[0] = '\0';.
    3. Make sure you track the current length, so you don’t try to build a string that won’t fit in the buffer. 500 chars allocated means max string length is 499 characters.

    Not sure what you expect should happen when you do free(read_string). It sounds (from your comment to @Steve Jessop’s answer) that you do something like this:

    char *read_string = malloc(500);
    read_string[0] = '\0';  /* Let's assume you do this. */
    append_char(read_string, 'a'); /* Or whatever, many of these. */
    free(read_string);
    print("%c\n", *read_string); /* This invokes UNDEFINED BEHAVIOR. */
    

    This might print an a, but that proves nothing since by doing this (accessing memory that has been free():d) your program is invoking undefined behavior, which means that anything could happen. You cannot draw conclusions from this, since the “test” is not valid. You can’t free memory and then access it. If you do it, and get some “reasonable”/”correct” result, you still cannot say that the free():ing “didn’t work”.

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

Sidebar

Related Questions

I'm trying to append a string of svg elements to the dom. This is
I have a TextView that I am trying to make display a 2d character-based
i am currently trying to assign each character that i get from a file
I am trying to author an extension method for a string that takes one
I'm trying to ensure that all characters which are not numeric, or alphabetic are
I need to append some hexadecimal characters to my string. I'm trying this: content
I am trying to parse the string contents of an XML file that contains
I am trying append some XML retrieved via a dojo.XHRGet to a dijit.layout.ContentPane .
I am trying to append some text to excel cell. For example if the
I am trying to append a li element to a list with a link

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.