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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:54:25+00:00 2026-05-28T19:54:25+00:00

here i want to run this program and take one character pointer like char

  • 0

here i want to run this program and take one character pointer like char *finalstring.

but how can i allocate memory to this finalstring.

i cant declare like 512 bytes and 1024 bytes etc. because here output not fix its depend on Bluetooth address.

So any one can tell me i want to store these all output data which i prints here in one character pointer. i want to concat every while loop execution and want to declare dynamic memory.

code :

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

void main() {

    char *sdpCommand = "sdptool browse 04:18:0F:B1:48:B5";

    FILE *fp;
    char* finalstring = malloc(512);
    char result[512];
    fp = popen(sdpCommand, "r");
    if (fp == NULL) {
        printf("Failed to run command\n");
        return;
    }

    if (finalstring) {
        *finalstring = 0;

        /* Read the output a line at a time - output it. */
        while (fgets(result, sizeof(result) - 1, fp) != NULL) {
            char* newfinalstring = realloc(finalstring, strlen(result) + 1);
            if (newfinalstring) {
                finalstring = newfinalstring;
                strcat(finalstring, result);
            }
            printf("Output    :::  %s", result);
        }
        free(finalstring);
    }
    printf("\nfinal Output    :::  %s", finalstring);

    free(finalstring);
    /* close */
    pclose(fp);
    sleep(1);

}

output :

   Output    :::  Browsing 04:18:0F:B1:48:B5 ...
Output    :::  Service RecHandle: 0x10000
*** glibc detected *** ./sdptool: realloc(): invalid next size: 0x00011008 ***
======= Backtrace: =========
/lib/libc.so.6[0x400a2f3c]
/lib/libc.so.6[0x400a72ac]
/lib/libc.so.6(realloc+0x108)[0x400a84ec]
./sdptool[0x86d4]
/lib/libc.so.6(__libc_start_main+0x120)[0x4004efd4]
======= Memory map: ========
00008000-00009000 r-xp 00000000 1f:04 16647      /opt/WL1271_demo_01/bluetooth_scripts/sdptool
00010000-00011000 rwxp 00000000 1f:04 16647      /opt/WL1271_demo_01/bluetooth_scripts/sdptool
00011000-00032000 rwxp 00000000 00:00 0          [heap]
40000000-4001d000 r-xp 00000000 1f:04 747        /lib/ld-2.8.so
4001d000-40021000 rwxp 00000000 00:00 0 
40024000-40025000 r-xp 0001c000 1f:04 747        /lib/ld-2.8.so
40025000-40026000 rwxp 0001d000 1f:04 747        /lib/ld-2.8.so
40026000-40032000 r-xp 00000000 1f:04 762        /lib/libgcc_s.so.1
40032000-40039000 ---p 0000c000 1f:04 762        /lib/libgcc_s.so.1
40039000-4003a000 rwxp 0000b000 1f:04 762        /lib/libgcc_s.so.1
4003a000-40157000 r-xp 00000000 1f:04 753        /lib/libc-2.8.so
40157000-4015e000 ---p 0011d000 1f:04 753        /lib/libc-2.8.so
4015e000-40160000 r-xp 0011c000 1f:04 753        /lib/libc-2.8.so
40160000-40161000 rwxp 0011e000 1f:04 753        /lib/libc-2.8.so
40161000-40164000 rwxp 00000000 00:00 0 
40200000-40221000 rwxp 00000000 00:00 0 
40221000-40300000 ---p 00000000 00:00 0 
bec5c000-bec71000 rw-p 00000000 00:00 0          [stack]
Aborted
  • 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-28T19:54:26+00:00Added an answer on May 28, 2026 at 7:54 pm

    You have to keep track of how long the string is currently, and how much is allocated. Then when adding more text, if the new length will be more than what’s allocated you allocate more.

    Here’s some code you can use:

    /* How much memory to allocate each time */
    #define BUFFER_CHUNK_LENGTH  128
    
    struct buffer_data
    {
        char  *buffer;   /* The actual buffer */
        size_t length;   /* Length of data in buffer */
        size_t alloc;    /* Bytes allocated for the buffer */
    };
    
    struct buffer_data *create_buffer()
    {
        /* Use "calloc" to clear all members */
        return calloc(1, sizeof(struct buffer_data));
    }
    
    void add_to_buffer(struct buffer_data *buffer, char *text)
    {
        /* Use "+1" for the string termincator character */
        if ((buffer->length + strlen(text) + 1) > buffer->alloc)
        {
            /* Is this the first allocation? */
            int is_first = (buffer->buffer == NULL);
    
            /* Not enough memory in buffer, reallocate to increase */
            buffer->buffer = realloc(buffer->buffer, buffer->alloc + BUFFER_CHUNK_LENGTH);
            buffer->alloc += BUFFER_CHUNK_LENGTH;
    
            if (is_first)
                *buffer->buffer = '\0';  /* Make sure string is terminated */
    
            /* Try to add again */
            add_to_buffer(buffer, text);
        }
        else
        {
            strcat(buffer->buffer, text);
            buffer->length += strlen(text);
        }
    }
    
    int main()
    {
        /* You stuff here... */
    
        struct buffer_data *buffer = create_buffer();
    
        while (fgets(result, sizeof(result), fp) != NULL)
        {
            add_to_buffer(buffer, result);
        }
    
        printf("Buffered data: %s", buffer->buffer);
    
        /* More of your stuff here */
    }
    

    PS.
    Don’t forget to clean up after you, by freeing both the buffer data and the buffer structure.

    Edit

    This code doesn’t contain any error checking or handling! Use of a debugger is highly recommended!

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

Sidebar

Related Questions

I'm not entirely sure if I can do what I want here, but I
I'm probably going to take some heat for this question. But I'd like to
At the moment all I want this program to do is run without any
I've decided to reopen this because I want to here more peoples views on
Here's what I want the resulting class declaration to look like: public sealed partial
here i want to solve stack overflow issue in this code. here in this
I run this sample here: https://github.com/jagregory/fluent-nhibernate/blob/master/src/Examples.FirstProject/Program.cs All C# Properties of type String are mapped
I have a program that uses clipboard but I want to restore the clipboard
I am very new to programming. I want to be able to run this
In reference to this question, as you can see I managed to run 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.