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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:31:44+00:00 2026-05-27T05:31:44+00:00

I have the following algorithm to implement new in C void *newinc(unsigned int s)

  • 0

I have the following algorithm to implement new in C

void *newinc(unsigned int s)
        {
                // allocate and align storage of size s

                // handle failure via exception

                // return pointer to storage
        }

I have the following implementation:

 void *newinc(unsigned int s)
{
   int *p = (int *)malloc(s * sizeof(int));
    return p;
}

I believe the first and the last step mentioned in the algorithm have been achieved by the program,how do I implement the second line in the algorithm:
// handle failure via exception
I believe C program does not have try/catch blocks to catch exceptions.

  • 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-27T05:31:45+00:00Added an answer on May 27, 2026 at 5:31 am

    Are you sure you are wanting an implementation in C, not C++? The following is all under the assumption that you really want C.

    To start with, C does not know the keyword operator, and you cannot give a function a name with two words. Therefore you will need to give your function a different name, like operator_new. Given that your function obviously only works for ints (there’s no way in your interface to give information about the type, and your example implementation allocates space for s ints), I’d suggest new_ints instead (since C doesn’t have constructors, the distinction between new and operator new is moot). Alternatively you might want to pass on arguments telling about the size of the type you want to allocate. You can even pass on a pointer to a function you want to be called as “constructor” for your objects (using a void* interface).

    Also C has no exceptions. You may somewhat emulate them with setjmp/longjmp, however since C doesn’t know destructors it will not do cleanup for you. Code aware of your hand-made exception handling may however implement cleanup explicitly through a chain of setjmp buffers. However, those setjmp buffers need to be passed on to your function, either using extra arguments, or using a global variable. All in all, the better option in C is to just return NULL, as malloc does. However, if you insist on the exception part, this is how you might do it (untested):

    #include <setjmp.h>
    #include <stdlib.h>
    
    void* operator_new(size_t s,     /* how many bytes to allocate */
                       jmp_buf env)  /* the "exception" information */
    {
      void* block = malloc(s);
      if (!block)
        longjmp(env, 1); /* 1 is an "error code" */
      return block;
    }
    

    The function would then be called as follows:

    int main()
    {
      volatile jmp_buf env; /* I'm actually not sure if this has to be volatile,
                               but longjmp may mess up some non-volatile variables */
      if (!setjmp(env)) /* try */
      {
        int* p = operator_new(10*sizeof int, env);
        /* use p */
        free(p)
      }
      else /* catch(...) */
      {
        /* handle the error */
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two dimensional array: static int[,] arr = new int[5, 5]
i'm new to c++ and have been trying to implement the bellman ford algorithm,
I'm trying to implement Peterson's algorithm in Java, and have created the following for
I have the following (imperative) algorithm that I want to implement in Haskell: Given
I have a SP that has the following algorithm. IF <SomeCondition> BEGIN SELECT *
I have an algorithm that recursively makes change in the following manner: public static
I have to implement an algorithm that generates a timetable for a university. I've
I can't figure out how to handle the following situation: I have a 9*9
I have the following algorithm implemented in Java which uses TCP/IP: -Client request a
I'm trying to implement AES encryption in my application. I have the following code

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.