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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:24:30+00:00 2026-05-26T10:24:30+00:00

When a you declare a struct in a class method, how is the memory

  • 0

When a you declare a struct in a class method, how is the memory for that struct allocated? For example:

void Foo::Bar(void)
{
    struct timeval a;

    a.tv_sec = 1;
    a.tv_usec = 0;

    ...
}

Is a allocated on the stack like any other local data (int, double, etc.), or is a default constructor called and the memory allocated on the heap?

What about if the struct initialized as so: struct timeval a = {0, 0};?

Trying to decide if it is better to make the structure a class member or have it as a method local since the method gets called quite often and needs to be as fast as possible.

  • 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-26T10:24:30+00:00Added an answer on May 26, 2026 at 10:24 am

    Is a allocated on the stack like any other local data (int, double, etc.), or is a default constructor called and the memory allocated on the heap?

    It is allocated on local storage(stack).
    An object will be created on Freestore(heap) only when any of the new versions of the operators(except maybe placement new) are used while creating it.
    Also, your structure object does not persist beyond the scope of the function enclosing it. An Freestore(Heap) object would persist till you call delete on it explicitly.

    Whenever you create an object of structure/class, an appropriate constructor(by matching parameter types) will be called for that structure/class. In your case you do not pass any arguments while creating the object hence the Default constructor will be called.

    What about if the struct initialized as so: struct timeval a = {0, 0};

    It is still allocated as an object with local storage.

    Trying to decide if it is better to make the structure a class member or have it as a method local since the method gets called quite often and needs to be as fast as possible.

    Creating Objects on local storage(stack) is faster than creating them on Freestore(Heap).
    Though,In your case you are not using the heap at all.

    Actual profiling a sample code shall tell you which one(a local structure or a class Member) better suits your performance requirements.

    However,Consider how the structure is related to your class before making it a member. Does it have an has a relationship to your class or it is just an utility structure. You should consider that design aspect too.

    Also, there are several restrictions in what can be accessed inside an local structure or class so do bear that in mind too.

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

Sidebar

Related Questions

Sometimes, C++'s notion of privacy just baffles me :-) class Foo { struct Bar;
I’d like to declare a generic class Simple where a method called Addition would
I can't figure out how to forward declare a windows struct. The definition is
Is there a way to declare a variable as Nullable in c#? struct MyStruct
I'm in the process of reverse-engineering a Windows executable. I found a class that
I have a method that is over 700+ lines long. In the beginning of
I've got a generic method TResult Foo<TSource, TResult>(IEnumerable<TSource> source) and if TResult is declared
This is my class: class SSLIRC : public Bot { public: SSLIRC(void); void sockconnect(char*,
I want the following struct as a class member, but I don't know the
I'm trying to forward declare a class with the _ may _alias attribute, but

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.