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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:57:50+00:00 2026-05-27T22:57:50+00:00

gcc 4.6.2 c89 I am just wondering what could cause more memory issues. For

  • 0

gcc 4.6.2 c89

I am just wondering what could cause more memory issues. For example, if you allocate a structure array on stack or if you was to allocate it dynamically on the heap.

Normally, I follow a simple rule. If the structure is only to be used in that function, then I allocate on the stack. However, if I need to refer to it somewhere else by keeping it in memory I would allocate dynamically.

The reason I asked this question as some of my memory was getting corrupted and I was told that I should allocate dynamically rather than on the stack for structure arrays. As stack memory has more chance of getting corrupted. However, if memory that is allocated dynamcially gets corrupted you can easily free it.

Does the above make sense to anyone?

Many thanks for any suggestions,

  • 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-27T22:57:50+00:00Added an answer on May 27, 2026 at 10:57 pm

    I wouldn’t say that any one way of allocating a structure has “more chance of corruption”. Whatever is causing the corruption could happen just as easily however it’s allocated.

    I’d say you’d be better off fixing the source of the corruption: You can use gdb to put a breakpoint on write to the corrupted variable with watch <varname>. Alternatively, you can also put a breakpoint when you detect corruption, and then use reverse debugging to find where the corruption occurred.


    Edit: There seems to be some confusion about the meaning of stack and static allocation:

    void foo() {
        int a[10]; // stack
        static int b[10]; // static
    
        int *c = malloc(sizeof(int) * 10); // dynamic on the heap
    }
    

    Stack variables are valid only during the lifetime of the function – once this function has returned, you can’t expect the data at the location of a to still be valid. Sometimes these are called local or automatic variables.

    Static variables inside a function are valid also outside the function – the data is kept around between function invocations. This means if you do this:

    void foo() {
        static int a = 0;
        a++;
        printf("%d\n",a);
    }
    

    The number printed will increase by one on each invocation of foo(). Generally, functions don’t have many variables declared as static, because static variables remember their last value from the last time the function was called – which is usually not the behaviour you want. It’s possible this is what was meant by the person who told you that static variables have “more chance of being corrupted”.

    Heap variables are valid from when they’re created to when they’re free()d. This is also called dynamic allocation and is what you want if you’re planning to return data to another part of the program (or if you want to specify the length of an array at runtime in C89).


    Aside: static is confusing in that when applied to a function name or variable in global scope:

    static void foo() { ... }
    static int x;
    

    it means “this function or variable is only visible inside this translation unit”. I’m not sure why the static keyword has these two different meanings.

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

Sidebar

Related Questions

gcc (GCC) 4.6.3 c89 Hello, I am just wondering if this is the best
gcc 4.4.4 C89 I am just wondering what most C programmers do when they
gcc 4.4.4 c89 I am just wondering is it worth passing a const into
gcc 4.4.4 c89 I was just experimenting with a int array. And something just
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 c89 Just wondering is there a better way to do
gcc 4.4.1 I am just wondering which standard is better and more portable? I
gcc 4.4.4 c89 I am just wondering is there any standard that should be
gcc 4.4.3 c89 pthreads I use valgrind for checking memory errors. I am just
gcc 4.6.2 c89 I am just wondering if this is a good way to
gcc c89 I am just looking to link against the below shared library. I

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.