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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:12:13+00:00 2026-05-27T07:12:13+00:00

I am trying to create a function which should return all arguments as a

  • 0

I am trying to create a function which should return all arguments as a concatenated char array. Unfortunately, I get an ‘invalid pointer’ error while using the function. I’m new to C so maybe I’m wrong in using realloc in this way.

char* concat(int argc, ...) {
    char* result;

    va_list args;
    va_start(args, argc);

    int i;
    for (i = 0; i < argc; i++) {
        char* s = va_arg(args, char*);
        int length = (result) ? strlen(result) : 1;

        char* tmp = (char*)realloc(result, sizeof(char) * (strlen(s) + length - 1));
        if (tmp == NULL) {
            throw_error("Realloc failed in `concat`.");
        }

        result = tmp;
        memcpy(&(result[length-1]), s, strlen(s));

        printf("result: %s\n", s);
    }

    va_end(args);
    return result;
}

The error message if it can help:

*** glibc detected *** ./pascc: realloc(): invalid pointer: 0x00000000006060c8 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x76bb6)[0x7f9953be8bb6]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0x338)[0x7f9953beed58]
./pascc[0x401368]
./pascc[0x400f85]
./pascc[0x4019bb]
./pascc[0x403c49]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xff)[0x7f9953b90eff]
./pascc[0x400e29]
======= Memory map: ========
00400000-00406000 r-xp 00000000 08:05 2885504 

To avoid memory leak, I read in an other thread that I shouldn’t realloc something itself like this:

result = (char*)realloc(result, sizeof(char) * (strlen(s) + length - 1));

So I’m trying to find the good way to do this. Some 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-27T07:12:14+00:00Added an answer on May 27, 2026 at 7:12 am

    You are reallocing an uninitialized pointer.

    char* result;
    

    To use realloc you must either use a NULL pointer or a pointer that points to memory that was assigned by malloc.

    char* result = NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a function which takes an array as an argument, adds
I am trying to create a function which will give me alphabet position when
I am currently trying to create a function which will allow me to pass
I'm trying to create a simple function which returns me a date with a
I'm trying to create a function in C# which will allow me to, when
I'm trying to create a function that would dynamically allocate an array, sets the
I wanted to create a function which would return the path of the current
I'm trying to create a function that searches up a WebControl's parent-child relationship (basically
I'm trying to create a function to create a new basewith another struct as
I am trying to create a function that will block access to some of

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.