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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:12:46+00:00 2026-06-12T07:12:46+00:00

How can I change the following code such that I can use new and

  • 0

How can I change the following code such that I can use new and delete instead of malloc and free?

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

class myclass{
  private:
    float **m_R;
  public:
    myclass();
    ~myclass();
    float **getR(void);
    void setR(void);
};

myclass::myclass()
{
  // first allocate rows (for pointers)
  m_R = (float**)malloc(3*sizeof(float));
  // next allocate columns for float values
  for(int i=0;i<3;i++)
    *(m_R+i) = (float*)malloc(3*sizeof(float));
}

myclass::~myclass()
{
  // first free memory allocated by columns
  for(int i = 0; i < 3; i++)
  {
    free(m_R[i]);
  }
  // next free memory allocated by rows
  free(m_R);
}

void myclass::setR(void)
{
  for(int i=0;i<3;i++)
  {
    for(int j=0;j<3;j++)
    {
      m_R[i][j] = 10*i+j;
      //cout << m_R[i][j] << ", ";
    }
    //cout << "\n";
  }
}

float **myclass::getR(void)
{
  return m_R;
}

int main () {

  myclass obj;
  obj.setR();

  for(int i=0;i<3;i++)
  {
    for(int j=0;j<3;j++)
    {
      printf("%02d, ",(int)obj.getR()[i][j]);
    }
    cout << "\n";
  }

  return 0;
}

Edit1: Please note that I have to use a function (not written by me) which takes float** as argument and I have no choice (such as vector) than using float**.

Edit2: The function is from Proximity Query Package (PQP) written as following:

int 
PQP_Distance(PQP_DistanceResult *result, 
             PQP_REAL R1[3][3], PQP_REAL T1[3], PQP_Model *o1,
             PQP_REAL R2[3][3], PQP_REAL T2[3], PQP_Model *o2,
             PQP_REAL rel_err, PQP_REAL abs_err,
             int qsize = 2);
  • 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-12T07:12:47+00:00Added an answer on June 12, 2026 at 7:12 am
    • T* a = (T*)malloc(sizeof(T)) becomes new T.
    • T* b = (T*)malloc(N * sizeof(T)) becomes new T[N].
    • free(a) becomes delete a.
    • free(b) becomes delete[] b.

    So you get:

    myclass::myclass()
    {
      // first allocate rows (for pointers)
      m_R = new float*[3];
      // next allocate columns for float values
      for(int i=0;i<3;i++)
        *(m_R+i) = new float[3];
    }
    
    myclass::~myclass()
    {
      // first free memory allocated by columns
      for(int i = 0; i < 3; i++)
      {
        delete[] m_R[i];
      }
      // next free memory allocated by rows
      delete [] m_R;
    }
    

    Note that this is actually not very optimal. If you want 3×3 matrices, you’re better off allocating 9 floats in one block.

    Also note that your C is incorrect.

    m_R = (float**)malloc(3*sizeof(float));
    

    should be

    m_R = (float**)malloc(3*sizeof(float*));
    

    Your code probably “works” because you’re compiling for 32-bit, where float and float* are 4 bytes. On a 64 bit build, float* is 8 bytes.


    Honestly, though, since your dimensions are fixed and small, you should store everything in the object itself:

    class myclass{
      private:
        float m_R[3][3];
      public:
        myclass() {}
        ~myclass() {}
        void setR(void);
        float* operator[](unsigned i) { return m_R[i]; }
        const float* operator[](unsigned i) const { return m_R[i]; }
    };
    
    void myclass::setR(void)
    {
      for(int i=0;i<3;i++)
      {
        for(int j=0;j<3;j++)
        {
          (*this)[i][j] = 10*i+j;
          //cout << (*this)[i][j] << ", ";
        }
        //cout << "\n";
      }
    }
    
    int main () {
    
      myclass obj;
      obj.setR();
    
      for(int i=0;i<3;i++)
      {
        for(int j=0;j<3;j++)
        {
          printf("%02d, ",(int)(obj[i][j]));
        }
        cout << "\n";
      }
    
      return 0;
    }
    

    This is how I usually do it, so I get the readability of [][] with the efficiency of in-object storage.

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

Sidebar

Related Questions

I have the following html structure (which is generated and I can't change): <table
i have the following xml. legacy: xml (can't change because it is externalize). <bean
I understand that I can change a sql table using the follow sp: EXEC
In the following code, you see that pickledList is being used by the thread
I wish to enforce the use of reviews for any code that gets pushed
I can change SQL at runtime. Can I do the same with LINQ ?
I can change the irb prompt mode with irb --prompt prompt-mode I can see
How i can change the color with gradient??? I have this now,but is different
Where I can change compiler options for C# project in VisualStudio 2008 (without command
I know I can change the selectionStyle of a UITableViewCell to make it highlight

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.