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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:46:46+00:00 2026-06-17T11:46:46+00:00

I came to C from Python. Python has a delightfully simplistic white glove approach

  • 0

I came to C from Python. Python has a delightfully simplistic white glove approach to manipulating strings. The more I use arrays in C, the more I think how convenient it would be to have certain features. Rather than writing loops to do this every time I need to do a particular operation, I have decided to create a library to do this.

So, let’s say I have a library and the call looks something like this:

char* new_array = meatSlicer(old_array, element_start);

I’m passing the pointer to the array I want changed, expecting a pointer return, and indicating what element to slice at.

If meatSlicer (yes, I’m a sucker for bad naming) returns a pointer to an array that is made locally within the slicer, the pointer will be a bad pointer. So, within meatSlicer() I have this:

    ... manipulation before the below ...

    char *heap_the_Array;     /* put it on the heap to pass it back to caller */
    heap_the_Array = malloc((size + 1) * sizeof(char));

    int i;                
    for (i = 0; i <= (size + 1); i++){           /* make it so... again */

            heap_the_Array[i] = newArray[i];     /* newArray is the local */

    }

    return heap_the_Array;                       /* return pointer */

My question is, am I properly returning ownership to the caller function so that it can free() the new array? Is passing a pointer to an array on the heap sufficient for that?

  • 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-17T11:46:48+00:00Added an answer on June 17, 2026 at 11:46 am

    Yes, copying local variables into malloc-ed regions of memory works well. You can replace the loop with a call of memcpy to reduce the code size. Write

    memcpy(heap_the_Array, newArray, size+1);
    

    instead of

    int i;                
    for (i = 0; i <= (size + 1); i++){           /* make it so... again */
            heap_the_Array[i] = newArray[i];     /* newArray is the local */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read a INI file from python, and I came up with
I came from the Spring camp , I don't want to use Spring ,
I came from PHP, Python and Django are new to me. I have a
I have a numpy array which came from a cv2.imread and so has dtype
I came from PHP language(codeigniter), but now I learning ASP.Net MVC :) In PHP
Background I came from several years working in a company where all the database
User can came from web or from devise (Android|iPhone|iPad|iPod) How correctly detect it?
I have a record that came from the follow linq query: using (var context
There's this already populated database which came from another dev. I'm not sure what
I have a form with a hidden Came from Adwords field that will be

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.