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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:27:22+00:00 2026-05-18T10:27:22+00:00

I have a function which takes two current class level member variables and sets

  • 0

I have a function which takes two current class level member variables and sets them into a timeval structure, and returns the timeval obj (by value).

I am seeing an issue when setting a class level member timeval object vs creating a new timeval object at each get() call.

Inside the class

protected:
int time[2];
timeval tv;

// work done on setting the time array

timeval getTimeval()
{
    tv.tv_sec = (time_t)time[0];
    tv.tv_usec = time[1];
    return tv;
}

This will not return the correct timeval values. The tv.tv_sec will get overwritten but the tv_usec remains constant. However, it will return the correct values when I create the timeval object inside the get call.

timeval getTimeval()
{
    timeval t;
    t.tv_sec = (time_t)time[0];
    t.tv_usec = time[1];
    return t;
}

Is there any reason setting the timeval objects on a member variable should differ from creating a new object and setting its values?

  • 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-18T10:27:23+00:00Added an answer on May 18, 2026 at 10:27 am
    • Any chance you’ve corrupted something somewhere else (undefined behaviour)?
    • Are you using threads? If so, the first approach could have two threads both working on the class member ‘tv’ at the same time, whereas the second approach has each thread working on its own local timeval instance.

    That said, there’s really no reason to have the timeval as a class member here; you aren’t optimizing anything as is (to avoid constructing a separate instance, you would have to return the class member by reference, rather than by value) so you’re just wasting space inside each instance of the class. (Returning the timeval by value isn’t especially costly anyway; it’s a small, POD struct, being stack-allocated.)

    But why are the values starting out in the int array in the first place? Why not just have a timeval data member and work with it directly (and return it by const reference in the accessor)?

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

Sidebar

Related Questions

I have a function which parses one string into two strings. In C# I
Say I have a C function which takes a variable number of arguments: How
So: I have the following function, adapted from a formula found online, which takes
I have a function which searches an STL container then returns the iterator when
I have an function which decodes the encoded base64 data in binary data but
I have a function which gets a key from the user and generates a
i have a function which retrieves values from a webservice , then loops through
Imagine I have an function which goes through one million/billion strings and checks smth
This is a Windows Forms application. I have a function which captures some mouse
i have a c function which returns a long double . i'd like to

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.