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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:17:12+00:00 2026-06-05T18:17:12+00:00

When writing C++ code I’ve learned that using the stack to store memory is

  • 0

When writing C++ code I’ve learned that using the stack to store memory is a good idea.

But recently I ran into a problem:

I had an experiment that had code that looked like this:

void fun(const unsigned int N) {
    float data_1[N*N];
    float data_2[N*N];

    /* Do magic */
}

The code exploted with a seqmentation fault at random, and I had no idea why.

It turned out that problem was that I was trying to store things that were to big on my stack, is there a way of detecting this? Or at least detecting that it has gone wrong?

  • 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-05T18:17:13+00:00Added an answer on June 5, 2026 at 6:17 pm
    float data_1[N*N];
    float data_2[N*N];
    

    These are variable length arrays (VLA), as N is not a constant expression. The const-ness in the parameter only ensures that N is read-only. It doesn’t tell the compiler that N is constant expression.

    VLAs are allowed in C99 only; in other version of C, and all versions of C++ they’re not allowed. However, some compilers provides VLA as compiler-extension feature. If you’re compiling with GCC, then try using -pedantic option, it will tell you it is not allowed.

    Now why your program gives segfault, probably because of stack-overflow due to large value of N * N:


    Consider using std::vector as:

    #include <vector> 
    
    void fun(const unsigned int N) 
    {
      std::vector<float> data_1(N*N);
      std::vector<float> data_2(N*N);
    
      //your code
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing code that read customized config section from app.config, but I get
When writing code in C/C++ using MPI, if memory is allocated before MPI_Init is
While writing code in a file that would comprise of PHP, HTML, CSS &
I'm writing code that uses sched_setaffinity, which requires kernel 2.5.8 or later. I've been
I am writing code that will build an oriented bounding box (obb) tree for
I've been writing code that processes certain fields of an object by modifying their
I am writing code that will populate the Margin , Padding and BorderThickness properties
I'm writing code where I retrieve XML from a web api, then parse that
While writing code, I often run into the problem of addressing trivial dependancies. For
I am writing code that involved a for loop that makes calculations at each

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.