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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:24:28+00:00 2026-06-17T03:24:28+00:00

Possible Duplicate: Definition of global variables using a non constant initializer I have this

  • 0

Possible Duplicate:
Definition of global variables using a non constant initializer

I have this code:

#include <stdio.h>
#include <stdlib.h>


int foo (int num, int i)
{
    static int* array = malloc(sizeof(int));  // ERROR HERE!!!
    printf("%d", array[i]);
    return 0;
}



int main(int argc, char *argv[])
{
    int i;
    for (i = 0; i < 2; i++) {
    foo(i, i);
    }

    return 0;
}

I save the code as a c source file, I can’t work? the error prompt:

gcc -O2 -Wall test.c -lm -o test
test.c:4:1: error: initializer element is not constant

Compilation exited abnormally with code 1 at Sat Jan 05 21:33:56

However, I save it as a C++ source file, It works OK. Why? is there anybody can explain it to me?

  • 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-17T03:24:29+00:00Added an answer on June 17, 2026 at 3:24 am

    C and C++ standards treat initialization of objects with static storage duration differently. C++ allows both static initialization (i.e. initialization with a constant) and dynamic initialization (i.e. initialization with non-constant expression), while C allows only static initialization – i.e. with constant expressions.

    The relevant portion of the C++ standard is 6.7.4:

    The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) is performed before any other initialization takes place. A local object of POD type (3.9) with static storage duration initialized with constant-expressions is initialized before its block is first entered. […] Otherwise such an object is initialized the first time control passes through its declaration; such an object is considered initialized upon the completion of its initialization. (emphasis added)

    C++ need additional “bookkeeping” in order to run the dynamic portion of your initializer (i.e. the call of malloc) only once. There is no similar “dynamic” provision in the C standard:

    All objects with static storage duration shall be initialized (set to their initial values) before program startup.
    All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

    In the absence of concurrency, you can rewrite the code for use with C like this:

    int foo (int num, int i) {
        static int* array = NULL;
        if (!array) array = malloc(sizeof(int)); // No error
        printf("%d", array[i]);
        return 0;
    }
    

    Now your code is responsible for the “bookkeeping”: it checks array for NULL before performing the allocation.

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

Sidebar

Related Questions

Possible Duplicate: What is the correct way of using extern for global variables ?
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Possible Duplicate: Pure virtual functions may not have an inline definition. Why? Just as
Possible Duplicate: Throw an error in a MySQL trigger I have a table definition
Possible Duplicate: Pure virtual functions may not have an inline definition. Why? I've come
Possible Duplicate: What makes code legacy? What is the definition of legacy code?
Possible Duplicate: What does 'unsigned temp:3' means I don't understand this struct definition. It
Possible Duplicate: In MATLAB, can I have a script and a function definition in
Possible Duplicate: What does 'unsigned temp:3' means A struct 's definition goes like this,
Possible Duplicate: Are global variables in PHP considered bad practice? If so, why? global

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.