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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:22:25+00:00 2026-05-16T20:22:25+00:00

When using C++, if there is a class: class MyClass { char memory1bye; int

  • 0

When using C++,
if there is a class:

class MyClass
{
    char memory1bye;
    int memory4bytes;
    int another4bytes;
};

this class uses total of 9 bytes at memory… so if i do something like:

MyClass *t1;

This will give me a usable address for the Class, but will it allocate the 9 bytes? And will it call default constructor?
Or do I need to malloc those 9 bytes to the class?
If then I called something like:

t1 = (MyClass *)new MyClass;

will it be considered memory leak? In other words, what happens to the old address?

  • 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-16T20:22:26+00:00Added an answer on May 16, 2026 at 8:22 pm
    1. As many people have said, the size of MyClass is implementation-dependent. In this case, because the class has no methods, you’ve basically got a struct so it is possible to make some reasonable guesses as to size. On a normal modern 32-bit machine without any unusual compiler flags, the size of the structure will be 12 bytes; this follows from the fact that fields are aligned to 4-byte boundaries by default on current architectures.

      On a 64-bit machine it could be even larger, but I’d be a bit surprised if it was bigger than 24 bytes (i.e., 8-byte alignment for each field). I don’t think anything uses anything larger than 8-byte alignment for fields unless explicitly told to, and there’s not much point in using larger alignment values for fields as the memory allocation functions themselves usually have 8-byte alignment. (NB: Don’t count on that being true for your machine!)

      The only way to actually know the size of anything is to use sizeof(MyClass). You hardly ever need to use that in C++, as the new operator knows about that for you and allocates the amount of space needed. And as previously noted, remember that sizes of anything (other than char) are not portable, even if they don’t actually vary gratuitously.

    2. Doing MyClass *t1; doesn’t allocate anything. It just gives you a place to store the address of an object (specifically, a MyClass instance). By default, that space points off into la-la land if the variable is in any local scope or in a class or structure definition. If you’re not about to put an address in the variable, it’s probably a good idea to explicitly initialize it to NULL so that it at least points to a definite not-an-object.

    3. Your t1 = (MyClass *)new MyClass; contains an unnecessary cast, since new returns a pointer to an object of that type anyway. Doing t1 = new MyClass; is enough.

    4. If t1 was previously pointing to an object and was the only variable pointing to it, you’ll have a memory leak (assuming you’ve not used a garbage collector library; most C++ programs are written without using them). If something else is pointing to the object, then that had better be assuming the responsibility for cleaning it up. If the address isn’t pointing to anything in particular, or it was pointing to NULL, then nothing is leaked.

      Remember, addresses don’t leak; objects leak.

      You can mitigate memory leaks by creating the objects on the stack (with straight MyClass t1;) and passing them around by reference rather than address; the object will then be deleted automatically when it passes out of scope. The main disadvantage of this comes when you have an object whose lifetime can’t be coupled nicely to a particular scope. That’s when you use pointers (or smart pointers, which hide most of the details at a cost of some restrictions). Really complex code is just better with garbage collection, though that has its own trade-offs (notably including being much more likely to increase memory consumption; this is the core reason why Java is known for being more memory hungry than C++).

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

Sidebar

Related Questions

I have a class that creates and uses a System.Threading.Timer, something like this: using
I am using this WebCrawler class http://phpcrawl.cuab.de . There is a method named setTmpFile()
I have some code looking like this: using System.Threading; public sealed class MyClass :
Using this example from http://docs.python.org/tutorial/classes.html#class-objects : class MyClass: A simple example class i =
I have a class like this class MyClass { int Identifier; int Context; int
The problem I'm using fitnesse for running some automated tests. There is a class
Is there a way to list methods in a Java class using the JNI
Is there a way to mark an auto-generated class as ExcludeFromCodeCoverage. I am using
I'm using a class that forwards events in C#. I was wondering if there's
I am using the .NET XmlSerializer class to deserialize GPX files. There are two

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.