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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:53:50+00:00 2026-06-17T05:53:50+00:00

I am mainly a Java person recently working on some projects involving C so

  • 0

I am mainly a Java person recently working on some projects involving C so please bear with me if it’s a basic C question.

So inside my main I have a while loop and I declare a variable each iteration.

int main()
{
  int done = 0;
  while(!done)
  {
    char input[1024];
    scanf("%s", input);
    //parse the input string
    ...
  }
} 

Now since the input variable will change every time depending on what the user wants I have to use a “new” variable each time. However, I think the above declaration will be causing memory leak ultimately(or will it?). I would like to know if gcc takes care of garbage collection.

Is there any better approach without allocating and freeing after every iteration?

  • 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-17T05:53:51+00:00Added an answer on June 17, 2026 at 5:53 am

    I think the above declaration will be causing memory leak ultimately(or will it?).

    No, it wouldn’t: input is an automatic (AKA “Stack”) variable, it will get “deallocated” as soon as it goes out of scope (i.e. after the closing brace).

    Is there any better approach without allocating and freeing after every iteration?

    There is no actual allocation or deallocation going on: the space in the automatic memory (AKA “on the stack”) is allocated by some compile-time bookkeeping around the stack pointer. The access of automatic variables is a very fast operation heavily assisted by hardware, so there is no loss of efficiency there.

    Dynamic memory allocation (Java-style) is done with malloc/calloc/realloc in C. These are not garbage collected – you need to explicitly free every pointer that you allocated.

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

Sidebar

Related Questions

I am a novice programmer who mainly uses Java. Recently I have become interested
Mainly this is a question about how to get a similar behaviour as java
This question is mainly about protecting the content inside my iOS app. I intend
I have worked for 5 years mainly in java desktop applications accessing Oracle databases
I tagged this question both Java and Scala because while I mainly develop on
We're working mainly in Java, using both Eclipse and Intellij in the team. We
I have been coding in Java(Mainly) and .Net for a while. What I found
I am fairly new to web programming, I have mainly used java to create
I'm working on a mainly Java based project, that also has a couple of
I've been learning programming for some months now, mainly with java and C#. They

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.