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

  • Home
  • SEARCH
  • 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 8399069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:12:06+00:00 2026-06-09T21:12:06+00:00

int main() { cout<<Enter n; cin >> n; int * a = new int[n];

  • 0
int main()
{

 cout<<"Enter n";

 cin >> n;

 int * a = new int[n];

 a = foo(n);

 free(a);

 return -1;

}

int * foo(int n)
{
 int * a = new int [n];

 if(!a) // what should I return here if allocation of a fails 

 {}

 free(a);


} 

In the code above I am trying to catch the return value of an function from main, the return type of the function is a pointer . However I am allocating memory dynamically . So , what should I return if my memory allocation fails … any special symbol like NULL .

P.S – Its a very basic question and could not formalize my question to any succinct form for to search over Google.

Edit: Thanks all of you guys . I have solved my problem .

  • 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-09T21:12:07+00:00Added an answer on June 9, 2026 at 9:12 pm

    It is a custom to return NULL in case of allocation failure from functions which allocate their own memory and return pointer to it. See for example strdup(). Note that operator new throws std::bad_alloc if it fails to allocate memory, so you may need to catch this if you want to return NULL or alternatively, you can let std::bad_alloc propagate out of the function.

    Note however, that it is not always wise to return such pointers since it raises the issues of ownership and increases the likelihood of memory leaks.

    You may find that sticking to the RAII idiom makes your code easier to reason about and less error prone. One consequence of RAII idiom is that allocation and deallocation are done by the same code unit.

    In your particular situation you allocate the array in main() so you may pass the pointer to it to foo() and deallocate memory also in main().

    Also, if you use new to allocate, you should use a proper version of delete to deallocate (here delete[] since you allocated an array). You use free() to deallocate memory allocated with malloc() and friends.

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

Sidebar

Related Questions

int main() { char* NamePointer = new char; std::cout << Enter the file you
#include <iostream> namespace { int a=1; } int a=2,b=3; int main(void) { std::cout<<::a<<::b; return
I have the following code: int main(int argc, char** argv) { onelog a; std::cout
struct car { string name; int year; }; int main() { int noOfCars; cout<<enter
#include <iostream> using namespace std; int main() { cout << !!!Hello World!!! << endl;
I created a program called test: #include<stdlib.h> #include<iostream> int main() { std::cout<<system(..\\add\\debug\\add.exe 4 8);
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
int main() { int b[2][3]={{1,2},{3,4,5}}; cout << b[0][2] << endl; } And the result
void myterminate () { cout << "terminate handler called"; } int main (void) {
#include <iostream> using namespace std; int main() { int a, b, c, max; cout<<a=;

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.