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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:29:23+00:00 2026-06-03T18:29:23+00:00

I have been stuck on this one for a while, I’m not an expert

  • 0

I have been stuck on this one for a while, I’m not an expert in C. Basically, I am trying to make a function that “safely” strcats a character to an existing char *.

I am trying to get the “dynamic allocation” method working from this example:

Using strcat in C

I have made a few modifications, I removed the var that’s set by the realloc function (the compiler said that it returned void). I also modified it to only append one character instead of an array of characters. I figured this would change the “realloc” parameters, so instead of passing the length of the addition string, I just passed in “sizeof(char)” (x2 because the original had an extra sizeof char, i think because of the null terminator?)

char *buffer = NULL;

int mystrcat(char addition)
{
   realloc(buffer, strlen(buffer) + sizeof(char)*2);
   if (!buffer)
     return 0;
   strcat(buffer, addition);
   return 1;
}

I call it like this:

if(!safestrcat(str[i+j]))
    printf("Out of Memory");

For some reason, I am seeing this:

Unhandled exception at 0x60f0d540 (msvcr100d.dll) in myProg.exe:
0xC0000005: Access violation reading location 0x00000000.

And the debugger shows me strlen.asm at line 81:

main_loop:
    mov     eax,dword ptr [ecx]     ; read 4 bytes

I’m sorry if this is a newb question, but what is happening? Why is the addition char not being appending to the buffer?

Sorry I should add, that it compiles succesfully.

  • 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-03T18:29:26+00:00Added an answer on June 3, 2026 at 6:29 pm
    • You forgot one argument
    • sizeof(char) is 1 by definition
    • your realloc code is broken
    • strcat doesn’t take a char as its second argument
    • I’d just return the newly created string, like strcat does
    char* mystrcat(char* buffer, char addition) {
        unsigned oldlen = strlen(buffer);
        buffer = realloc(buffer, oldlen + 2);
        if (buffer == NULL)
            return NULL;
    
        buffer[oldlen + 0] = addition;
        buffer[oldlen + 1] = '\0';
        return buffer;
    }
    

    However, pay attention to two things:

    1. You must call mystrcat with a valid, initialised pointer – same as strcat!
    2. In the case of failure, the function returns NULL – in that case, it’s the caller’s responsibility to ensure that the original buffer’s memory is freed. This means that you mustn’t call the function as

      buffer = mystrcat(buffer, 'x');
      

      – This may cause a memory leak.

    So a correct usage would be:

    char* something = "hello";
    char* buffer = malloc(sizeof(something) + 1);
    strcpy(buffer, something);
    
    char* new_buffer = mystrcat(buffer, 'x');
    if (new_buffer == NULL) {
        free(buffer);
        exit(1);
    }
    
    buffer = new_buffer;
    

    Yes, convoluted. This is the price for safe memory operations.

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

Sidebar

Related Questions

I have been stuck on this one for a while - I couldn't figure
I have been stuck on this for a while. i have tried and I
Been kind of stuck on this one for a while now, so any help
I've been stuck on this one for a while and any advice would be
I've been stuck on this for 3 days now. I have two pages that
Hi all been stuck on this for a while now, not too sure where
I have a problem that I've been stuck on for a while and I
I have been stuck on this for days, and was wondering if anyone had
I have been stuck on this and don't really know how to go about
I have been stuck on this silly if statement, whatever i do, I cannot

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.