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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:04:15+00:00 2026-06-11T04:04:15+00:00

Sorry if this is a silly question :-) Background I have legacy code that

  • 0

Sorry if this is a silly question 🙂

Background

I have legacy code that looks like this:

struct {
int field1;
int field2;
int field3;
int field4;
... many many fields
} myStruct;


while (something) {
initialzationFunction(&myStruct);

// ...change fields of myStruct and do stuff.
}

Each iteration of the while loop needs that myStruct will be initialized to something, lets say zero.
initialzationFunction initializes all the fields of myStruct to zero.

The question

Is it good to keep initialzationFunction inside the while loop,
or is it better to call it once before the loop, and let the programmers initialize what they need “by hand” if they happen to change this code.

edit: Unfortunately myStruct is a global variable, so making it an automatic variable isn’t an option, unless I want to pass it as a parameter to tons of legacy functions that use it.

What I think

  • just calling initialzationFunction() will prevent bugs in case someone modifies the code and forgets to later initialize myStruct.
  • It might be more informative to see what specific fields are initialized.
  • If only a few fields are modified later in the while loop, calling initialzationFunction() that inits all of the fields is redundant.

What would you do?

  • 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-11T04:04:16+00:00Added an answer on June 11, 2026 at 4:04 am

    Since the struct fields are changed inside the while loop, it makes sense to initialize it during each iteration for whatever the purpose of processing done in the loop.

    I would say it’s fine to to re-initialize few fields even if there were not modified in the loop. But keeping track of which fields were modified and excluding those fields
    when initializing duing the next iteration would be a hassle which you can do without.

    An alternative would be to use a temporary strucy variable with the initialization values and simply assign it at the start of every iteration.

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

Sidebar

Related Questions

This may seem like a silly question if so sorry =] Ok i have
Sorry if this seems like a silly question - I am an amateur when
Sorry if this sounds like a really silly question. But I Googled the web
I am just begginer so sorry for my silly question. my model looks like
Sorry if this is a silly question, but I currently have the following and
I am sorry if this is a silly question but I have been working
Sorry if this is a silly question but I have been on web design
Sorry for asking silly question.. I have an array with class objects like: Class
I am really sorry to ask this silly question. I have been onto this
if this is a silly question, am sorry about that. I was wondering if

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.