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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:06:17+00:00 2026-05-20T16:06:17+00:00

I have implemented my own version of strcat function. It works fine but valgrind

  • 0

I have implemented my own version of strcat function.
It works fine but valgrind complains.

main()
{
    char *src=NULL;
    src=(char *) malloc(sizeof(char)*8);
    strcpy(src,"sandeep");
    xstrcat(src,"pathak");
    printf("******FINAL STR IS : %s ********",src);
    free(src);
    src=NULL; 
}

void  xstrcat(char *src,const char *dest)
{
        int dlen=strlen(dest);
        int slen=strlen(src);
        int j=0;
        int i=0;
        char *temp=(char *) realloc(src,(slen+dlen+1)*sizeof(char));
        for(j=slen;i<dlen;i++,j++)
        {
                temp[j]=dest[i];
        }
        temp[j]='\0';
        printf("%s",temp);
}

VALGRIND ERROR :

==31775== Invalid read of size 4
==31775== Invalid read of size 4
==31775== Invalid read of size 1
==31775== Invalid read of size 1
==31775== 14 bytes in 1 blocks are definitely lost in loss record 1 of 1
==31775==    at 0x1B9053EE: realloc (vg_replace_malloc.c:197)
==31775==    by 0x8048606: xstrcat (in /user/gur27268/Computer_Systems/Socket/DevTest/UTI
==31775==    by 0x804850F: main (in /user/gur27268/Computer_Systems/Socket/DevTest/UTIL/a
==31775==
==31775== LEAK SUMMARY:
==31775==    definitely lost: 14 bytes in 1 blocks.
==31775==    possibly lost:   0 bytes in 0 blocks.
==31775==    still reachable: 0 bytes in 0 blocks.
==31775==         suppressed: 0 bytes in 0 blocks.
==31775== Reachable blocks (those to which a pointer was found) are not shown.**
==31775== To see them, rerun with: --show-reachable=yes

I have freed src, but using realloc tends to this problem…

Any help would be appreciated..

  • 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-20T16:06:18+00:00Added an answer on May 20, 2026 at 4:06 pm
    void xstrcat(char *src,const char *dest) {
    

    You’re passing src by value, and xstrcat is working on and modifying its local copy. Any changes you make to src will not be reflected in the calling function.

    void xstrcat(char **src,const char *dest) {
      // Work with *src
    }
    
    ...
    xstrcat(&src, ...)
    

    This approach lets xstrcat modify main’s src variable.

    To quote a frequently mentioned sample:

    void foo (int x) {
      x = 2;
    }
    void bar (int * x) {
      *x = 2;
    }
    int main() {
      foo(9); // foo cannot modify the literal 9
      int i = 1;
      foo(i); // foo cannot modify the variable i
      bar(&9); // This isn't legal - it can't be
      bar(&i); // bar modifies i
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented Twitter4J in my Android application and it's works fine. But after
I have implemented my own simple version of a navigation window, mainly because navigation
I have an own Tree object implemented in PHP. Say we have a tree
I have implemented Solr search in one of my .net application. Everything working fine
I have an assignment to implement my own version of Collections.fill() and Collections.reverse(). The
I now have a problem on using reduce to implement my own version of
I've implemented my own version of RoleVoter and I've added the following to the
I'd have to implement my own security for the application (windows form). The application
I have a class derived from ItemsControl in which I implement my own selection-algorithm
I have implemented the usual examples of accessing the camera roll on ipad and

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.