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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:04:51+00:00 2026-06-11T11:04:51+00:00

I am having an issue with dynamic arrays and malloc . I am fairly

  • 0

I am having an issue with dynamic arrays and malloc. I am fairly new to C so please excuse (and advise on) any rookie mistakes.

The problem is I create an array (input_string in this case) and pass it to func2. Then in func2 I do a test, print out the first element of input_string.

This works as expected in the first printout before the malloc, but after the malloc it doesn’t print anything. This seems weird to me since in between the to printf statements I do nothing to the input_string.

I’m assuming that I am dealing with these arrays incorrectly, but I’m unsure.

Here is a snippet of the code in question:

Updated

... // includes not in snippet

/* CONSTANTS */
#define LINE_LEN 80

/* Function declarations */
char* func1(void);
char* func2(int tl, char* input_string);

int main(void) {
    char* input_string;
    int tab_length;
    char* output_string;

    input_string = func1();
    output_string = func2(tl, input_string);

    return 0;
}

char* func1(void) {
    char cur_char;
    char* input_ptr;
    char input_string[LINE_LEN];
    while ((cur_char = getchar()) != '\n' && chars_read < 80) {
        // iterate and create the array here
    }
    input_ptr = &input_string[0]; /* set pointer to address of 0th index */
    return input_ptr;
}

char* func2(int tl, char* input_string) {
    int n = 0, output_idx = 0;
    char* output_ptr;
    printf("\nBefore malloc: %c ", *(input_string));
    output_ptr = malloc(tab_length * chars_read+1);
    if (output_ptr == NULL) {
            printf("Failed to allocate memory for output_ptr.\nExiting");
            exit(1);
    }
    printf("\nAfter malloc: %c ", *(input_string));
    ...
    return output_ptr;
}

P.s.: Any undeclared variables have been declared outside of this snippet of code.

Update

Thanks for all the replies and advice. It is very much 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-06-11T11:04:53+00:00Added an answer on June 11, 2026 at 11:04 am

    func1 Returns a pointer to a temporary string. You did not allocate it. This will produce undefined behaviour.

    Instead you should do this:

    char* func1(void) {
        char cur_char;
        char* input_ptr = (char*)malloc(LINE_LEN * sizeof(char));
        while ((cur_char = getchar()) != '\n' && chars_read < 80) {
            // iterate and create the array here
        }
        return input_ptr;
    }
    

    Interestingly, you did use malloc inside func2.

    When you are finished with it, you need to call free to release the memory.

    int main(void) {
        char* input_string;
        int tab_length;
        char* output_string;
    
        input_string = func1();
        output_string = func2(tl, input_string);
    
        free(input_string);
        free(output_string);    
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having an issue with adding dynamic content through an interval. The content
I'm having an issue understanding some compiler-errors, regarding 2D arrays (ArrayList containing an ArrayList)
I am having an issue with a dynamic Ovi / Nokia map whereby I
I'm new to Obj-C/Cocoa programming, and I'm having an issue trying to dynamically add
I'm relatively new to C++, and having an issue passing a string. I have
Google checkout is having an issue with dynamic merchant shipping callback calculation. When customer
I'm coding a Wordpress theme and I'm having an issue displaying 5 dynamic post
I'm using the BeautyTips jQuery extension, but I'm not having any luck passing dynamic
I am having an issue with Dynamic Binding in JSF page I am using
I am having issues passing a dynamic parameter to a JavaScript function using innerHTML.

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.