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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:35:12+00:00 2026-06-06T22:35:12+00:00

Is the following code acceptable. That is, is this the right way to do

  • 0

Is the following code acceptable. That is, is this the right way to do a malloc?

This is the minimal code that I could get to work for my situation. I think it’s ‘the right way to do it’ but I am super-new to C and don’t have much of a clue overall. I read several related SO posts but none seem to exactly match this situation. Comments?

#include <stdio.h>

// example of calling a function that creates a dynamically sized array and
// returns it to a caller that doesn't know or care about the size of the array

char* return_char_array(){
     // for the sake of the example, we determined the size to be 100
     char *f=malloc(100*sizeof(char));
     // stick something in the first couple of elements for test purposes
     *f=65;
     *(f+1)=66;
     return f;
}

int main(){
    // we want this function to remain ignorant of the size or other workings 
    // of the array, so, no '[]' or 'malloc'
    char *wipc = return_char_array();
    // well i guess it cares a little, because we assume there are at least 2 elements...
    printf("%c,%c\n",*(wipc),*(wipc+1));
    system("PAUSE");
    return 0;
}
  • 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-06T22:35:15+00:00Added an answer on June 6, 2026 at 10:35 pm

    No this is a bad idea and doesn’t make much sense. The code that is allocating the memory needs to be responsible for freeing it, or you will have a great potential for memory leaks.

    Typically you would write algorithms like this:

    // caller.c
    
    #include "stuff.h"  // the code uses the code module "stuff"
    
    void caller (void)
    {
      int* array = malloc(N * sizeof(int));
    
      do_stuff(array, N);
    
      free(array);
    }
    

    _

    // stuff.h
    
    void do_stuff (int* array, int size);
    

    _

    // stuff.c
    
    #include "stuff.h"
    
    void do_stuff (int* array, int size)
    {
      ...
    }
    

    With this program design, “do_stuff()” doesn’t need to care about memory allocation, it just executes its algorithm. Nor does anyone have to worry about memory leaks, because allocation and freeing is done from the same place.

    Also, with this design, do_stuff() will work just as fine with statically allocated memory (simple arrays).

    This design is pretty much the norm in C. The whole Windows API uses this design, as one example.

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

Sidebar

Related Questions

Is the following always acceptable code? It seems to work, but does it consistently
Hi upon compiling this code in Visual studio 2008 i get the following error
Possible Duplicate: Weird “406 not acceptable” error following code generate a 406 Not Acceptable
I have the following code that cannot be compiled: #include <iostream> #include <set> #include
I have the following line of code: sqlcommand.Parameters.Add(@LinkID, SqlDbType.Int, 4).Value = linkID; But, I'm
This may be a little subjective, but I have often found that it can
Don't know if there is a better way to do this, so that is
Is there any good or standard way of doing this? Take the following example:
Finally I managed to make this little site look acceptable in ff/ie/chrome -But when
Following code produces a nested array as a result for keys containing three items:

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.