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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:23:41+00:00 2026-05-24T20:23:41+00:00

int* func() { int* i=new int[1]; //do something return i; } void funcc() {

  • 0
int* func()
{
   int* i=new int[1];
   //do something
   return i; 
}

void funcc()
{
   int* tmp=func();
   //delete allocated memory after use
   delete tmp;
}

should delete working as described in the second function be a correct use ?
I think I didn’t allocate memory to it by new ? new was used in the first, to be sure.

  • 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-24T20:23:42+00:00Added an answer on May 24, 2026 at 8:23 pm

    As others have stated, you should use delete[] to delete arrays, not delete.

    But, if you’re asking whether it’s okay to delete tmp because the memory it points to wasn’t allocated with new, then your premise is incorrect.

    It was allocated with new. The address of the memory that you allocate within the function for i is passed back to be stored in tmp, so it does point to memory that was allocated by new.

    You’re correct that i itself is out of scope at that point but memory allocated by new survives the scope change on exit from the function. And, since you’re storing its location into tmp, you still have access to it.

    Hence the deletion (once you make it an array deletion with []) is quite valid.

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

Sidebar

Related Questions

Say a function takes a void pointer as an argument, like so: int func(void
I have a C++ API prototype void Func(int& size); How can I translate it
Basically I'd like to do something like that: int[3] array_func() { return {1,1,1}; }
I use the following types to create a new function at runtime: typedef int
How do you specify the Func signature for anonymous objects? new Func<DataSet, **IEnumerable<int>>** I
void some_func(int param = get_default_param_value());
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
The below does not compile: Func<int, int> fac = n => (n <= 1)
So odd situation that I ran into today with OrderBy: Func<SomeClass, int> orderByNumber =
Given a Lambda Expression: Define(Expression<Func<T, int>> property) and used like: Define(x => x.Collection.Count) What

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.