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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:04:54+00:00 2026-05-29T06:04:54+00:00

I was trying to implement a simple function that can concatenate any number of

  • 0

I was trying to implement a simple function that can concatenate any number of strings passed to it. My call to realloc fails. Is it something to do with the fact that the string arguments that i pass to the function are stored in data segment where as realloc looks to allocate memory from the heap ? This is just an idea i have. I’m a beginner so please excuse if it seems stupid. How can i make this function run ?

 //Program to implement a function that can concatenate any number of argumnets 

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

char *mstrcat(char *first, ...);
int main(int argc, int **argv){
    char *s;
    s=mstrcat("I ","Love ","Stack","Overflow");
    printf("%s\n",s);
}
char *mstrcat(char *first, ...){
       char *s=first,*p;
       int len=0;  // stores the length of the string as it grows
       len=strlen(s);
       va_list aptr; // creates a pointer to the unnamed argument list
       va_start(aptr,first); // initialise aptr to the first unnamed argument
       if(aptr==NULL){
        return s;
      }
      while((p=va_arg(aptr,char *))!=NULL){ // till there are no more arguments to process
         len+=strlen(p);
         if((s=(char *)realloc(s,len+1))!=NULL){
            strcat(s,p);
         }
         else{
            printf("Failed to concatenate\n");    
            return first; 
         }
      }
      return s;
}
  • 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-29T06:04:55+00:00Added an answer on May 29, 2026 at 6:04 am

    Your code has an Undefined Behavior. The standard mandates that the pointer being passed to realloc should exactly match the pointer which was allocated dynamic memory using a memory management function. Memory management functions specified by the standard are:
    aligned_alloc, calloc, malloc, and realloc.

    The pointer you are passing to realloc() was not returned by any of these and hence the Undefined Behavior.

    Reference:
    c99 standard: 7.22.3.5 The realloc function

    Synopsis: #1

    #include <stdlib.h>
    void *realloc(void *ptr, size_t size);
    

    #3

    If ptr is a null pointer, the realloc function behaves like the malloc function for the
    specified size. Otherwise, if ptr does not match a pointer earlier returned by a memory
    management function, or if the space has been deallocated by a call to the free or
    realloc function, the behavior is undefined
    . If memory for the new object cannot be
    allocated, the old object is not deallocated and its value is unchanged.

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

Sidebar

Related Questions

I'm trying to implement a simple horizontal navigation menu that just shows a single
I am trying to implement a function that will insert a new entry in
I'm trying to create a simple program or function that will take the users
I am trying to implement a simple request to Wikipedia's API using AJAX (XMLHttpRequest).
I am trying to implement a simple authorization strategy for my Wicket application. I
I am now trying to implement a simple HTML webpage scraper using Java.Now I
I've been trying to implement a simple component-based game object architecture using Objective-C, much
I'm new to Flex SDK and trying to implement a simple project using Doug
My first try of MVC. Am trying to implement a simple example. Inspiration from
I'm still learning about Objective-C memory management. I'm trying to implement several simple classes

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.