Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to initialize array elements to NULL??

RustumRustum Posts: 20Member
Hi,

Can someone tell me how to safely initialize the following:

int arr[100];

Rgds,

Rustum

Comments

  • LundinLundin Posts: 3,711Member
    : Hi,
    :
    : Can someone tell me how to safely initialize the following:
    :
    : int arr[100];
    :
    : Rgds,
    :
    : Rustum
    :


    Quick answer:
    int arr[100] = {0};


    Detailed answer:

    First note that in C, NULL may or may not be the same thing as 0 (zero).
    You want to set the elements to zero, not to NULL. NULL is for pointers.

    Second, it depends on where the array is allocated. If it is a global or static variable it will be initialized to zero by default. If it is a local variable you can write = {0} as mentioned above. That means that the first element will be set to zero and the rest of the elements will be treated as if they were static variables, which means they will also be set to zero by default.

    If you write programs that will run in a tough environment, you don't want to rely on the initialized values at all, but rather set them in runtime before the variable is used. That is in a for-loop, which is the slowest but safest way to do it.
  • RustumRustum Posts: 20Member
    : : Hi,
    : :
    : : Can someone tell me how to safely initialize the following:
    : :
    : : int arr[100];
    : :
    : : Rgds,
    : :
    : : Rustum
    : :
    :
    :
    : Quick answer:
    : int arr[100] = {0};
    :
    :
    : Detailed answer:
    :
    : First note that in C, NULL may or may not be the same thing as 0 (zero).
    : You want to set the elements to zero, not to NULL. NULL is for pointers.
    :
    : Second, it depends on where the array is allocated. If it is a global or static variable it will be initialized to zero by default. If it is a local variable you can write = {0} as mentioned above. That means that the first element will be set to zero and the rest of the elements will be treated as if they were static variables, which means they will also be set to zero by default.
    :
    : If you write programs that will run in a tough environment, you don't want to rely on the initialized values at all, but rather set them in runtime before the variable is used. That is in a for-loop, which is the slowest but safest way to do it.
    :
    Thanks for the interesting response. You've pre-empted some of my other questions. I forgot to mention that I'm coding in C but I presume your answer would hold true.
  • MT2002MT2002 Posts: 1,444Member
    : : : Hi,
    : : :
    : : : Can someone tell me how to safely initialize the following:
    : : :
    : : : int arr[100];
    : : :
    : : : Rgds,
    : : :
    : : : Rustum
    : : :
    : :
    : :
    : : Quick answer:
    : : int arr[100] = {0};
    : :
    : :
    : : Detailed answer:
    : :
    : : First note that in C, NULL may or may not be the same thing as 0 (zero).
    : : You want to set the elements to zero, not to NULL. NULL is for pointers.
    : :
    : : Second, it depends on where the array is allocated. If it is a global or static variable it will be initialized to zero by default. If it is a local variable you can write = {0} as mentioned above. That means that the first element will be set to zero and the rest of the elements will be treated as if they were static variables, which means they will also be set to zero by default.
    : :
    : : If you write programs that will run in a tough environment, you don't want to rely on the initialized values at all, but rather set them in runtime before the variable is used. That is in a for-loop, which is the slowest but safest way to do it.
    : :
    : Thanks for the interesting response. You've pre-empted some of my other questions. I forgot to mention that I'm coding in C but I presume your answer would hold true.
    :

    [code]
    int arr[100] = {0};[/code]
    [blue]
    I thought this was not ANSI standard (That is, some compilers
    might only initialize the first element to 0).

    A safe method is a simple memset...[/blue][code]
    int arr[100];

    memset ( arr, 0, 100*sizeof (int) );
    [/code]
    [blue]
    In C, NULL is useally defined as (void*)0[/blue]
  • bluj91bluj91 Posts: 133Member
    : : : : Hi,
    : : : :
    : : : : Can someone tell me how to safely initialize the following:
    : : : :
    : : : : int arr[100];
    : : : :
    : : : : Rgds,
    : : : :
    : : : : Rustum
    : : : :
    : : :
    : : :
    : : : Quick answer:
    : : : int arr[100] = {0};
    : : :
    : : :
    : : : Detailed answer:
    : : :
    : : : First note that in C, NULL may or may not be the same thing as 0 (zero).
    : : : You want to set the elements to zero, not to NULL. NULL is for pointers.
    : : :
    : : : Second, it depends on where the array is allocated. If it is a global or static variable it will be initialized to zero by default. If it is a local variable you can write = {0} as mentioned above. That means that the first element will be set to zero and the rest of the elements will be treated as if they were static variables, which means they will also be set to zero by default.
    : : :
    : : : If you write programs that will run in a tough environment, you don't want to rely on the initialized values at all, but rather set them in runtime before the variable is used. That is in a for-loop, which is the slowest but safest way to do it.
    : : :
    : : Thanks for the interesting response. You've pre-empted some of my other questions. I forgot to mention that I'm coding in C but I presume your answer would hold true.
    : :
    :
    : [code]
    : int arr[100] = {0};[/code]
    : [blue]
    : I thought this was not ANSI standard (That is, some compilers
    : might only initialize the first element to 0).
    :
    : A safe method is a simple memset...[/blue][code]
    : int arr[100];
    :
    : memset ( arr, 0, 100*sizeof (int) );
    : [/code]
    : [blue]
    : In C, NULL is useally defined as (void*)0[/blue]
    :

    memset is not standard.

  • IDKIDK Posts: 1,784Member
    : : [code]
    : : int arr[100] = {0};[/code]
    : : [blue]
    : : I thought this was not ANSI standard (That is, some compilers
    : : might only initialize the first element to 0).
    : :
    : : A safe method is a simple memset...[/blue][code]
    : : int arr[100];
    : :
    : : memset ( arr, 0, 100*sizeof (int) );
    : : [/code]
    : : [blue]
    : : In C, NULL is useally defined as (void*)0[/blue]
    : :
    :
    : memset is not standard.
    :
    :

    memset is really the for loop method, but highly optimised (or atlesast should be...).
  • MT2002MT2002 Posts: 1,444Member
    : : : [code]
    : : : int arr[100] = {0};[/code]
    : : : [blue]
    : : : I thought this was not ANSI standard (That is, some compilers
    : : : might only initialize the first element to 0).
    : : :
    : : : A safe method is a simple memset...[/blue][code]
    : : : int arr[100];
    : : :
    : : : memset ( arr, 0, 100*sizeof (int) );
    : : : [/code]
    : : : [blue]
    : : : In C, NULL is useally defined as (void*)0[/blue]
    : : :
    : :
    : : memset is not standard.
    : :
    : :
    :
    : memset is really the for loop method, but highly optimised (or atlesast should be...).
    :
    [blue]
    How is memset nonstandard? Can you please elabrate, or give a
    refrence to this? (Im just curious coinsidering its part of std
    namespace and said to be 'ANSI compatable').[/blue]
  • LundinLundin Posts: 3,711Member
    int arr[100] = {0};

    This will set all elements to zero. From ANSI/ISO 9899:

    "6.7.8 Initialization"

    /--/

    "10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

    - if it has pointer type, it is initialized to a null pointer;
    - if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    - if it is an aggregate, every member is initialized (recursively) according to these rules;"

    /--/

    19 The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; [b]all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration[/b]."

    By just setting setting the value of the first subobject, ie array[0], the rest of them will be handled as if they were static variables, just as I mentioned in my first post.

    And further from ANSI C:

    "7.21.6.1 The memset function"
    ...

  • MT2002MT2002 Posts: 1,444Member
    : int arr[100] = {0};
    :
    : This will set all elements to zero. From ANSI/ISO 9899:
    :
    : "6.7.8 Initialization"
    :
    : /--/
    :
    : "10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
    :
    : - if it has pointer type, it is initialized to a null pointer;
    : - if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    : - if it is an aggregate, every member is initialized (recursively) according to these rules;"
    :
    : /--/
    :
    : 19 The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; [b]all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration[/b]."
    :
    : By just setting setting the value of the first subobject, ie array[0], the rest of them will be handled as if they were static variables, just as I mentioned in my first post.
    :
    : And further from ANSI C:
    :
    : "7.21.6.1 The memset function"
    : ...
    :
    [blue]
    Ah ... so memset [b]is[/b] ANSI compatable..Thought so.
    (Big relief for my current project.)

    Thanks for the information reguarding static array initialization.
    Im assuming a)Most 32-bit compilers support this standard,
    and b)This initialization is done at compile time. Please correct
    me if I am wrong.

    Thanks for the info;

    ~mt2002[/blue]
  • LundinLundin Posts: 3,711Member
    : : int arr[100] = {0};
    : :
    : : This will set all elements to zero. From ANSI/ISO 9899:
    : :
    : : "6.7.8 Initialization"
    : :
    : : /--/
    : :
    : : "10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
    : :
    : : - if it has pointer type, it is initialized to a null pointer;
    : : - if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    : : - if it is an aggregate, every member is initialized (recursively) according to these rules;"
    : :
    : : /--/
    : :
    : : 19 The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; [b]all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration[/b]."
    : :
    : : By just setting setting the value of the first subobject, ie array[0], the rest of them will be handled as if they were static variables, just as I mentioned in my first post.
    : :
    : : And further from ANSI C:
    : :
    : : "7.21.6.1 The memset function"
    : : ...
    : :
    : [blue]
    : Ah ... so memset [b]is[/b] ANSI compatable..Thought so.
    : (Big relief for my current project.)
    :
    : Thanks for the information reguarding static array initialization.
    : Im assuming a)Most 32-bit compilers support this standard,
    : and b)This initialization is done at compile time. Please correct
    : me if I am wrong.
    :
    : Thanks for the info;
    :
    : ~mt2002[/blue]
    :


    Yes, all PC compilers I know of supports this part of ANSI C.
    On RAM-based systems (PC), it is likely done at compile time, but I suppose it might be compiler-specific. On ROM-based systems (microcontrollers etc), all initialization is done in runtime.
  • RustumRustum Posts: 20Member
    : : : int arr[100] = {0};
    : : :
    : : : This will set all elements to zero. From ANSI/ISO 9899:
    : : :
    : : : "6.7.8 Initialization"
    : : :
    : : : /--/
    : : :
    : : : "10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
    : : :
    : : : - if it has pointer type, it is initialized to a null pointer;
    : : : - if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    : : : - if it is an aggregate, every member is initialized (recursively) according to these rules;"
    : : :
    : : : /--/
    : : :
    : : : 19 The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; [b]all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration[/b]."
    : : :
    : : : By just setting setting the value of the first subobject, ie array[0], the rest of them will be handled as if they were static variables, just as I mentioned in my first post.
    : : :
    : : : And further from ANSI C:
    : : :
    : : : "7.21.6.1 The memset function"
    : : : ...
    : : :
    : : [blue]
    : : Ah ... so memset [b]is[/b] ANSI compatable..Thought so.
    : : (Big relief for my current project.)
    : :
    : : Thanks for the information reguarding static array initialization.
    : : Im assuming a)Most 32-bit compilers support this standard,
    : : and b)This initialization is done at compile time. Please correct
    : : me if I am wrong.
    : :
    : : Thanks for the info;
    : :
    : : ~mt2002[/blue]
    : :
    :
    :
    : Yes, all PC compilers I know of supports this part of ANSI C.
    : On RAM-based systems (PC), it is likely done at compile time, but I suppose it might be compiler-specific. On ROM-based systems (microcontrollers etc), all initialization is done in runtime.
    :
    I've fixed it now. Thanks Lundin, MT2002, Bluj91 and IDK :))
Sign In or Register to comment.