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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:59:01+00:00 2026-06-02T16:59:01+00:00

I am trying to do some practice for memory allocation. I have the below

  • 0

I am trying to do some practice for memory allocation.

I have the below code which is working but have two questions.

Where do I have to use delete [ ] to free the memory after allocating?

Why is the output for this code at function when using show() function is CDcar?.

#include <cstdlib>
#include <new>
#include <iostream>
#include <cstring>
using namespace std;

class automobile {

    private:

        char (*function)[30];
        char *type;
        double speed;

    public:

        automobile ( );
        automobile (double , char *);
        void speed_up (double);
        void speed_down(double);
        const char * get_function ( ) const;
        void show ( );

};

automobile::automobile ( ) {

    speed = 0;
    function = new char [1][30];
    strcpy(function[1], "CD player with MP3");

    type = new char [4];
    strcpy(type, "car");

}

automobile::automobile(double spd, char * fn ) {

    int sz;

}

void automobile::show ( ) {

    cout << "This is a " << type << " and it has the following functions: " << function[1] << ", and its speed is " << speed << " km/h\n"; 

}

int main ( ) {

    automobile car;

    car.show ( );

    return 0;
}

this is the output:

This is a car and it has the following functions: CDcar, and its speed is 0 km/h

I thought the output shoud be this:

This is a car and it has the following functions: CD player with MP3, and its speed is 0 km/h

Please advise

  • 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-02T16:59:02+00:00Added an answer on June 2, 2026 at 4:59 pm

    Where do I have to use delete [ ] to free the memory after allocating?

    Ideally nowhere. new and delete are features of C++ that are not suitable for most code. They are error-prone and too low-level. They’re only useful for basic building blocks.

    The code shown could benefit from basic building blocks like std::string, std::vector.


    The code shown also invokes undefined behaviour at least in one place:

    function = new char [1][30];
    strcpy(function[1], "CD player with MP3");
    

    Arrays are 0-based, so function[1] is an out-of-bounds access.

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

Sidebar

Related Questions

Trying to solve some queries from sql zoo to practice sql skills I have
I'm trying to apply some DDD-like patterns to the code I write to use
I am trying to practice programming and I have decide to place some effect
Recently I was trying some practice programs in python and I came across this
I'm using Eclipse with C++ plugins on my macbook, trying some practice projects to
I'm trying to adopt some best practices to keep my python code efficient. I've
I am trying some way to optimize following sql statement: exe_sql DELETE FROM tblEvent_type
I have been trying some programs in the C Language and come across to
I'm doing some programming practice right now by trying to sort a 2D array
For practice, I'm trying to do some stuff in Python. I've decided to make

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.