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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:09:23+00:00 2026-05-26T17:09:23+00:00

I have an exercise where I need to write a wrapper function for strcat.

  • 0

I have an exercise where I need to write a wrapper function for strcat. After it prints the string length (for debugging) it seg faults and I am not quite sure why. Any help would be greatly appreciated.

EDIT: I didn’t specify that the wrapper function is supposed to guarantee that it never goes outside of the bounds of memory allocated for destination. This is why I allocated only enough memory for “string” in destination and reallocating more in the Strcat() wrapper.

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

    char* Strcat(char* destination, const char* source);

    int main(void)
    {
        (void)printf("This strcat cannot fail!\n");
        char* destination = (char*)malloc(sizeof(char)*7);
        destination = "string";
        (void)printf("%s\n", destination);
        Strcat(destination, " concatination");
        (void)printf("%s\n", destination);
        (void)printf("It works!\n");

        free(destination);

        return 0;
    }

    char* Strcat(char* destination, const char* source)
    {
        (void)printf("%d\n", (strlen(destination)+strlen(source))*sizeof(char));
        if((sizeof((strlen(destination)+strlen(source)))+1) > sizeof(destination))
            destination = (char*)realloc(destination, sizeof((strlen(destination)+strlen(source)))+1);
        return strcat(destination, source);
    }
  • 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-26T17:09:24+00:00Added an answer on May 26, 2026 at 5:09 pm

    This line:

    destination = "string";
    

    overwrites the pointer returned from malloc(3) so you lose that memory forever. You probably meant to copy that string, so use strcpy(3) or something.

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

Sidebar

Related Questions

I have a situation where I need to write some unit tests for some
I have an exercise to do where I need to code in C, commands
I need help with this exercise where I have to translate properties from English
I as a learning exercise of Objective C I need to write a method
i have to crawl last.fm for users (university exercise). I'm new to python and
I am writing a huffman implementation in Python as a learning exercise. I have
I'm not very familiar with Clojure/Lisp macros. I would like to write apply-recur macro
I need to write an app that that will iterate over our database, and
This is indeed a sort of exercise I have to complete but a little
I have a couple Java classes which have blackbox test cases that just exercise

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.