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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:13:21+00:00 2026-06-08T02:13:21+00:00

Must I free structure memory after using it? I have sample code: struct aa

  • 0

Must I free structure memory after using it? I have sample code:

struct aa 
{
int a;
char * b ;
    aa()
    {
    a=0;
    b= new char[255];
    }
} ;


aa *ss = new aa[3];

void fill()
{
    aa * ssss = new aa;
    aa * sss = new aa;

    sss->a=10;
    ss[0] = *sss;
    cout<<ss[0].a<<"\n";
    ss[1] = *sss;

    cout<<ss[1].a<<"\n";
    cout<<ssss[1].a<<"\n";
}

int _tmain(int argc, _TCHAR* argv[])
{


    fill();
    delete(ss);
}

Must I do delete(ssss) at the end of fill?

Must I delete ss array of structure at the end of main?

Must I create destruct or to structure ss that frees *b memory?

What about classes is it the same logic?

  • 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-08T02:13:23+00:00Added an answer on June 8, 2026 at 2:13 am

    Must I do delete(ssss) at the end of fill?

    Yes, you must delete anything created with new. However, there’s no need to use new here, just make it automatic:

    void fill() {
        aa ssss; // automatically destroyed on exit from the function
    }
    

    Must I delete ss array of structure at the end of main?

    Yes, but it is an array, so must be deleted as an array:

    delete [] ss;
           ^^
    

    But again, there’s no reason for this to be dynamically allocated:

    aa ss[3]; // automatically destroyed on exit from the program
    

    Must I create destruct or to structure ss that frees *b memory?

    If you really want to use a raw pointer to manage the dynamic array, then yes. You will also need to think about a copy constructor and copy-assignment operator (per the Rule of Three) to make the class safe to use. Alternatively, use a smart pointer or container to manage the memory for you:

    struct aa 
    {
        int a;
        std::vector<char> b ;
    
        aa() : a(0), b(255) {}
    } ;
    

    What about classes is it the same logic?

    Yes, the rule is always the same: anything created with new must be destroyed with delete. Managing objects is much easier if you avoid dynamic allocation where possible, and use smart pointers, containers and other RAII classes when you really do need it.

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

Sidebar

Related Questions

My C is quite rusty. Consider the code above: must I free up the
Must be a way looping through this code: private void loadSprites() { this.sprites[0] =
I have a problem with the following language: I must write a context-free grammar:
I have a TCP Client,which puts a packet in a structure using System.Runtime.InteropServices; [StructLayoutAttribute(LayoutKind.Sequential)]
I'm struggling to find why I can't free a memory block. Something must be
I must send a struct between two machines via a udp socket the information
i have following HTML structure: <div class=container> <div class=sidebar></div> <div class=content></div> <div class=subscript></div> </div>
I'm developing an android app for 2.2 ver. My app must hav such structure:
I'm using Visual c++. I'm trying to implement a circular buffer, this CB must
Coming from a C# background, I have only vaguest idea on memory management on

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.