tags:

views:

5679

answers:

2

Hi,

I have a these structures definitions

typedef struct my_s {
   int x;
   int y;
} my_T;

typedef struct your_s {
    my_T * x;
} your_T;

your_T array[MAX_COL][MAX_ROW];

To initialize the array's pointer to NULL, can I do:

memset (array, 0, sizeof(array))

this does not look right to me,,, appreciate some advise.

Thanks

+3  A: 

easiest is

your_T array[MAX_COL][MAX_ROW] = {{{0}}};
rampion
+1, just to clarify, this works because all members not explicitly initialized will be set to 0 which is a null pointer.
Robert Gamble
hmn, trying this, compiler gave warnings:missing braces around initializernear array[0][0], my warnings are treated as errors.
sorry, forgot third set of braces. Array of array of struct.
rampion
+1  A: 
typedef struct my_s {
   int x;
   int y;
} my_T;

typedef struct your_s {
    my_T * x;
} your_T;

your_T array[MAX_COL][MAX_ROW];

You cannot initialize the pointers of your_T to null using memset, because it is not specified that a null pointer has its bit pattern all consisting of null bits. But you can create your array like this:

your_T array[MAX_COL][MAX_ROW] = {{}};

The elements will be default initialized, which means for a pointer that the pointer will contain a null pointer value. If your array is global, you don't even have to care. That will happen by default then.

Johannes Schaub - litb
Thanks for the information. I know when global, it is defaulted to NULL, but I want to make sure it is NULL-ed since I am running in the embedded space...
right then you are fine with {{}}. the indent is that an implementation could store a value different from 0x0 in the pointer, which when dereferenced by a following read of that memory cell could cause a trap to be generated. thus, the 0x0 bit pattern is not neccassary what is in a null pointer.
Johannes Schaub - litb
+1. Empty brace initialization is illegal in C but, as I just learned, legal in C++, is this a common/useful construct?
Robert Gamble
actually once i thought it's illegal in C++ too (because i read it on some site which talked about C), until some guys in ##c++ showed me where in the standard it allows that in C++ :)
Johannes Schaub - litb
on your +1 point, it seemed it compiles ok with {{ }}..
i think it could have a benefit if you change order of members in a struct, you don't have to change a first initializer. it just default initializes members of the aggregate. i guess they haven't seen any difficulties supporting it, so they allowed it. i dunno about the rationale for it in C.
Johannes Schaub - litb
additionally, i think they allowed for empty initialization lists because initialization for objects in aggregates is copy initialization. so if you initialize the first element, it requires the type to be copyable. while with an empty initializer, the type merely needs to be default constructible.
Johannes Schaub - litb
but then again, with such class types, they would be default initialized anyway and one could omit the empty braces altogether :)
Johannes Schaub - litb
I believe that a null pointer is defined as being 0 (C++03,4.10): _' A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero.'_ This of course does not mean that the pointer will be zero, but that assigning 0 will make the pointer null
David Rodríguez - dribeas
yes, a null pointer constant is every integer constant expression that yields zero (all bits zero). such as ('a'-'a') or simple 0. converting it to a pointer will yield a null pointer value, which is NOT all bits zero anymore necassarily.
Johannes Schaub - litb