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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:24:31+00:00 2026-06-09T14:24:31+00:00

Okay, so apparently this works: void foo(size_t s) { int myArray[s]; // … use

  • 0

Okay, so apparently this works:

void foo(size_t s) {
    int myArray[s];
    // ... use myArray...
}

Is this really legal? I mean, it must be, because it compiles (where the C compiler would reject it as non-constant). The first part of my question is: how does this work? I assume it’s allocating it on the stack? Is this different from using alloca()?

Practically, I found some code that does this:

void bar(size_t chunkSize) {
    CFReadStreamRef foo = NULL;
    // ...some stuff to init foo...

    while (stuffToDo) {
        UInt8 buffer[chunkSize];
        // ...read some data from stream into buffer
        //    using CFReadStreamRead()...
    }
}

This works. However, when I move the buffer allocation from inside the loop to the first line of the function (directly before foo is declared), the function… stops working. In the debugger it gets to the first access of local variables and then just… exits. I don’t see any exceptions being thrown, it doesn’t crash, it just program carries on running (in reality the function returns a string and that return value is NULL, which is what the return variable is initialized to). I’m not sure what’s going on. The second part of my questions is, in light of the first part, what the heck is going on?

  • 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-09T14:24:32+00:00Added an answer on June 9, 2026 at 2:24 pm

    it is legal in C99, although dangerous, and yes — it is like alloca.

    because it’s like alloca, you want reasonably sized arrays when allocating on the stack. i am not sure if this is defined if the length is zero, but you could definitely cause a stack overflow if the array is ‘large enough’ to do so.

    as far as what is going on — pulling it out of the loop should make no difference if the sizes are reasonable. i suspect you are seeing undefined behavior because a parameter value is too large (or perhaps 0) — you should validate the chunkSize parameter. the assembly will tell you why pulling it out of the loop makes a difference (assuming everything else in the program is well-formed).

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

Sidebar

Related Questions

Okay this question is very simple: I have a facebook page, and a website.
Okay, I feel a bit foolish for having to ask this but I guess
Okay, so I'm working on a java server for an Apps backend, it must
Okay, this is probably a very basic question; but, I'm just getting back in
Okay...so apparently with the new update in IE8, it starts up with NO add-ons.
Okay, so this is sort of a hack...but it may have to be. I'm
Okay, this just feels plain nasty, but I've been directed to do it, and
Edit: I'm rewriting this question because I apparently wasn't clear. Sometimes the GPS service
Okay a few rules: No, I cannot install Strawberry Perl on this system. I
Okay the title cannot explain the situation correct enough. Now this is it, 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.