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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:41:28+00:00 2026-05-15T21:41:28+00:00

The closest thread to my question is here . I am trying to compile

  • 0

The closest thread to my question is here. I am trying to compile the following code with gcc:

#include <malloc.h>

class A
{
public:
  A(){};  
  ~A(){};
};//class A

int main()
{
  A* obj = (A*) malloc( sizeof(A) );
  if(obj==0) return 1 ;
  obj->A::A(); /*error: invalid use of 'class A' */
  obj->A::~A();
  free(obj);
  return 0;  
};//

From the command line I compile the code with:

$ g++ -o main main.cpp  
main.cpp: In function 'int main()':  
main.cpp:22: error: invalid use of 'class A'

Can you please point me in the right direction?

  • 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-15T21:41:29+00:00Added an answer on May 15, 2026 at 9:41 pm

    You can’t call a constructor on an object; a constructor can only be called in the creation of an object so by definition the object can’t exist yet.

    The way to do this is with placement new. There’s no need to cast your malloc return. It should be void * as it doesn’t return a pointer to an A; only a pointer to raw memory in which you plan to construct an A.

    E.g.

    void* mem = malloc( sizeof(A) );
    
    A* obj = new (mem) A();
    obj->~A();
    
    free(mem);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jQuery: $(this).closest('div.mcoup').find('div.delcoup').slideToggle(400) .siblings().children('div.delcoup').slideUp(400); What I'm trying to do is get
I'm having a devil of a time understanding references. Consider the following code: class
I am trying to get a reference to the main boost::thread of execution, so
parentId=$(this).closest('tr').find('td:nth(6)')[0].textContent; I am trying to get ID from flexigrid column. It works fine in
I'm trying to sort a list of CLLocationDistance values by closest distance (ascending order).
This is more a generic question than a specific one. I'm trying to have
I am trying to execute a small piece of code on every single frame
I'm trying to clean up resources in my application before it shuts down, following
Starting a new question from this thread, Can anyone answer why a ComboBox with
This is slightly related to the question asked here yet the answer does not

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.