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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:50:38+00:00 2026-05-27T17:50:38+00:00

I want to malloc some memory, run some operations on that memory, and then

  • 0

I want to malloc some memory, run some operations on that memory, and then have the result of those operations be persistent. An example might be if I have a 2-d persistent array that I want to do row-wise updates on.

void update(int **persistent, int row, int rowSize) 
{
    int *temp = (int*) malloc(sizeof(int) * rowSize);
    //...do stuff with temp
    persistent[row] = temp
}

What would be the safest way to do this in C without having to deep-copy the contents of temp to persistent[row]? If I do it the way I typed above without freeing temp, would that cause a memory leak even if I correctly free persistent later on?

EDIT: A related question I have is: if I try to free(temp) at the end of the function, I noticed that even though persistent[row] maintains a reference to the data at temp, the memory is released. Is this because of the way malloc/free are designed, so that whenever a free on a pointer is called, the memory is freed without checking for other live references to that memory?

  • 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-27T17:50:38+00:00Added an answer on May 27, 2026 at 5:50 pm

    That would only cause a memory leak if you don’t properly free the old pointer that was in persistent[row], if it wasn’t NULL. As long as you free the old pointer before overwriting it with temp you won’t have any problems as long as you correctly (row by row) free persistent.

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

Sidebar

Related Questions

The following is a sketch of how I might have some form of automated
I have a child process which generates some output of variable length and then
I want to allocate virtually contiguous range memory so that I can use the
Lets say I have an array of structs and I want to delete an
I have a function that takes some bitmap data and returns a UIImage *
I have setup some views with the navigation controller. I want to be able
I want to create an integer pointer p, allocate memory for a 10-element array,
I have created an array of pointers. I have used some of them as
I have a chunk of memory, let us say 'NN'MB. I want a custom
I want to know how malloc and free work. int main() { unsigned char

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.