C- static initialization of structures


 
Thread Tools Search this Thread
Top Forums Programming C- static initialization of structures
# 8  
Old 12-16-2012
Quote:
Originally Posted by garysk
As I pointed out in my reply to @jimmacnamara, there was no overflow. By means of logic that is not obvious to me, the compiler made certain decisions, which resulted in two things:

1. The data aligned, one comma-separated datum per named member of each instance of the structure within the array of structures. The values expected, except for the NULL-initialized char [5], appeared in their expected places in the array and within every element of the array. If they didn't align one to one, that would have been the time to break out the additional curly braces.

2. The "NULL" initializer of the member array was interpreted as a signal to initialize the member array with null bytes. Somehow, no doubt through application of fundamental rules of the language (which is what is not clear to me), the compiler made the decision to convert the *((void *)0) to (char)0, essentially converting a declared pointer into a declared integer.

To my knowledge there is not a simpler explanation of the decision (the need for that decision that I created by expecting the compiler to resolve ambiguous code notwithstanding) than to ascribe it to datatype promotion/demotion. Typically, though, the compiler balks at pointer-integer conversion, and I would have expected something like that here, rather than silently do as it thought I said.

I think there should have been a warning somewhere, or an error, applied to the construct by the compiler. But alas, none was indicated.

I am aware that the compiler "tries" to do what it asked to do, and to try to make things fit, but if it's not clear, I'd rather have errors and warnings out the kazoo than to have it guess as to what is intended. And I know that the compiler is neither intelligent nor animate, and can only obey the rules given; I am at this point really only interested in understanding the rules that came into play here.
I agree with what Jim McNamara has already said. I hope this may provide a little more that will help explain what you're seeing.

Depending on the programming environment you're using, NULL is likely to be defined to be 0, 0L, or (void *)0. (All three of these are used for one or more of the multiple programming environments provided by OS X and all three of these are legitimate values for NULL according to the C Standard depending on the relative sizes of pointers and integers in your programming environment.)

If NULL expands to 0 in your programming environment, there would be absolutely no difference to the compiler between the initializer:
Code:
NULL, '\0', '\0'

and the initializer:
Code:
'\0', '\0', '\0'

If NULL expands to 0 or 0L, no pointer to integer conversion is needed to turn NULL into an integer constant used to initialize the array of characters in your structure.

I agree that if your compilation environment has NULL defined to be (void *)0 it would have been nice for the compiler to produce a warning, but I don't see anything in the C Standard that would require that to happen since the conversion from a NULL pointer to an integer constant expression is a well defined operation with no loss of data involved.
# 9  
Old 12-16-2012
Don, a million thanks for your cogent reply.

'Pon our systems, NULL the pointer is defined as (void *)0. NUL the character is (char)0. This is why I have been saying, as you do below, it would be natural to expect a warning about the type coercion implied by my mistake.

But, as I also said at the thread head, zero is zero is zero. And C is enamored with zeros, for breakfast lunch and dinner, where more strongly typed languages would never allow such digital promiscuity.

Our OS is 32-bit kernel running on a 64-bit architecture; we tell the compiler so by using the -march=1.0 which means PA RISC 1 architecture, a 32 bit system. Our int's long's and pointers are all 32-bit. We are stuck in the 32bit timewarp as untangling all the 32-bit words and coding each properly for 64 bits in a 720k-line package where pointer/integer cross-overs are rampant, as are exchanges between longs and ints. It has been judged a monumental rewrite that is not in the current budget. Fortunately, our package compiles links and runs (same gcc version, whatever that means cross-architectures) on RedHat 64-bit (kernel 2.6.18), so we can presumably limp along indefinitely on an antedeluvian setup, as long as gcc supports us.

There is a loss of bts in the compiler's conversion (bits that are always 0). But coercing a 32-bit pointer to fit into an 8-bit char is potentially a little lossy.

Quote:
Originally Posted by Don Cragun
I agree with what Jim McNamara has already said. I hope this may provide a little more that will help explain what you're seeing.

Depending on the programming environment you're using, NULL is likely to be defined to be 0, 0L, or (void *)0. (All three of these are used for one or more of the multiple programming environments provided by OS X and all three of these are legitimate values for NULL according to the C Standard depending on the relative sizes of pointers and integers in your programming environment.)

If NULL expands to 0 in your programming environment, there would be absolutely no difference to the compiler between the initializer:
Code:
NULL, '\0', '\0'

and the initializer:
Code:
'\0', '\0', '\0'

If NULL expands to 0 or 0L, no pointer to integer conversion is needed to turn NULL into an integer constant used to initialize the array of characters in your structure.

I agree that if your compilation environment has NULL defined to be (void *)0 it would have been nice for the compiler to produce a warning, but I don't see anything in the C Standard that would require that to happen since the conversion from a NULL pointer to an integer constant expression is a well defined operation with no loss of data involved.
# 10  
Old 12-17-2012
Seems to me a major point has been missed.

Given an array, the bare array without subscript evaluates to the address of the first element of the array.

Initializing the contents of an array to NULL or NUL or the square root of two won't change the address of the first element.

Thus, in this code,
Code:
   struct my_struct_type {
         char tags_sn[5];
         char group_c;
         char is_err_c;
     };

     struct my_struct_type stuff[] = {
         {"abcd", 'A', 'E'},
         {"efgh", 'B', 'E'},
         {"ijkl", 'C', 'E'},
         { {0x0}, '\0', '\0'}     };

the value of stuff[ 3 ].tags_sn is not NULL even though the value of the contents of the array might be the equivalent of NULL.
# 11  
Old 12-17-2012
Thank you, Achenle, for reiterating this point. This point was made in the original post. Obviously, the constant address that is symbolized by the name of the member cannot be changed. The original questions were "why no objection from the compiler" and "where did the NULL go". The best answers seem to be that the NULL was interpreted at its most primitive, a zero, and used to fill the member array, and because this was a slam-dunk move on gcc's part, there was no need for it to object.
# 12  
Old 12-17-2012
Quote:
Originally Posted by garysk
Thank you, Achenle, for reiterating this point. This point was made in the original post. Obviously, the constant address that is symbolized by the name of the member cannot be changed. The original questions were "why no objection from the compiler" and "where did the NULL go". The best answers seem to be that the NULL was interpreted at its most primitive, a zero, and used to fill the member array, and because this was a slam-dunk move on gcc's part, there was no need for it to object.
You sure didn't seem to know it, else you never would have posted this:

Quote:
In a loop that uses tags_sn == NULL as its terminating condition, it fails to terminate and instead of course gets a segmentation violation and dumps core.
# 13  
Old 12-17-2012
Quote:
Originally Posted by achenle
You sure didn't seem to know it, else you never would have posted this:
Quote:
Oiginally posted by garysk. In a loop that uses tags_sn == NULL as its terminating condition, it fails to terminate and instead of course gets a segmentation violation and dumps core.
At the time that I posted it, I did know that assigning to an array was bad, but I didn't think/know/relying-on-memory that I was doing so. In subsequent investigation (while waiting for helpful replies) I discovered to my surprise that tags_sn was not a pointer but an array. Should I have looked up the declaration before posting? Yes.

Was the post "necessary"? To me it was because I didn't see my error at that time, and wondered then if I was violating some constraint somewhere. Before the first replies came, I had found my error, but I was still concerned that the compiler had said nothing, and wondering what it HAD done with the code. The subsequent exchanges addressed these two questions.

I find that the first post (such as the original post here) I submit to any forum I join (which is NOT many) is usually brain-damaged in some way, but the others on the forum are usually graciously helpful nonetheless.
# 14  
Old 12-17-2012
Quote:
Originally Posted by garysk
Don, a million thanks for your cogent reply.

'Pon our systems, NULL the pointer is defined as (void *)0. NUL the character is (char)0. This is why I have been saying, as you do below, it would be natural to expect a warning about the type coercion implied by my mistake.
Ah, but void is special this way, intended to avoid that fuss. You can pass any kind of pointer into a function that takes a void * without objection, for instance.

Last edited by Corona688; 12-17-2012 at 06:54 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error occurred during initialization of VM

Hi , I was invoking a sh file using the nohup command. But while invoking, I received a below error. Error occurred during initialization of VM Unable to load native library: /u01/libjava.so: cannot open shared object file: No such file or directory . Could you please help out. Regards,... (2 Replies)
Discussion started by: Kamal1108
2 Replies

2. Shell Programming and Scripting

Variable initialization

Hallo Team, I have a simple request and i would appreciate your help. I would like to use two dates in my script lets: A=$(date +"%d %B %Y") echo $A 23 June 2014 That's awesome now we cooking. Now i want B to be on the previous month eg: echo $B Should give me 23 May 2014 I would... (9 Replies)
Discussion started by: kekanap
9 Replies

3. Shell Programming and Scripting

Initialization error

new to shell scripting. below line is showing error in script. ${parameter:=word} in the o/p first it shows the below error. word: not found. and then in next line print "word" ---------------- p2: word: not found. word --------------------------- OS is AIX and shell is... (12 Replies)
Discussion started by: scriptor
12 Replies

4. Programming

Even the Static cURL Library Isn't Static

I'm writing a program which uses curl to be run on Linux PCs which will be used by a number of different users. I cannot make the users all install curl on their individual machines, so I have tried to link curl in statically, rather than using libcurl.so. I downloaded the source and created a... (8 Replies)
Discussion started by: BrandonShw
8 Replies

5. Programming

Class Pointer initialization C++

Hello everyone, I have a question, that are the following ways of pointer intialization same ? ClassA *point; point = 0; point = new ClassA; Thanks a load in advance!! Regards, (10 Replies)
Discussion started by: mind@work
10 Replies

6. Shell Programming and Scripting

little confusion about variable initialization.

Whenever i execute the below scriptlet with out proper file name it deletes /tmp directory . I guess this is because value of variable a didnt get initialized and there for rm -rf /tmp/ get executed and entire /tmp directory get deleted. How would i avoid any empty variables to be used in... (9 Replies)
Discussion started by: pinga123
9 Replies

7. IP Networking

I need HELP to Set up Coyote Linux router with 1 static IP & 64 internal static IP

hello, i need help on setting my coyote linux, i've working on this for last 5 days, can't get it to work. I've been posting this message to coyote forum, and other linux forum, but haven't get any answer yet. Hope someone here can help me...... please see my attached picture first. ... (0 Replies)
Discussion started by: dlwoaud
0 Replies

8. Programming

Char initialization

Hi All, char a="\0"; a) a contains \0 a contains garbage value b) a contains \ a contains 0 a contains garbage value Pls, let me know correct result is a or b. I guess a. Thanks, Naga:cool: (2 Replies)
Discussion started by: Nagapandi
2 Replies

9. UNIX for Dummies Questions & Answers

Shell initialization files

As you know, when a user logs in, the shell reads the initialization files in an order something like below... ################### Bourne Shell /etc/profile > $HOME/.profile Bash Shell /etc/profile > $HOME/.bash_profile > $HOME/.bash_login > $HOME/.profile > $HOME/.bashrc C Shell... (3 Replies)
Discussion started by: SeanWuzHere
3 Replies

10. Programming

Struct Initialization

Hi We are using a code generator for initializing structures with the #define macro. Compiling it with the GCC 2.8.1 (with -ansi) it OK. But when we are using the SUN C 5.0 compiler it screams. Following is a code sample: #include <stdlib.h> #include <stdio.h> typedef struct TEST3 {... (4 Replies)
Discussion started by: amatsaka
4 Replies
Login or Register to Ask a Question