Since I'm researcher, I found one little interestingbug in gcc 3.x and 4.x tool
chains. It could be seenfrom simple piece of code I pasted bellow. SunStudio
C,and gcc 2.95 compiles it fine.
This information I give to community so anyone thathas some code affected with
this unusual behaviorknow what is going on. This error has its roots fromearly
90's C programming style if anyone wants to knowlittle history regarding this
issue.
Regards,Uros Nedic
==========================================================
#include <stdio.h>#include <stdlib.h>#include <errno.h>
#define PC struct pctypedef unsigned short word;
PC { /* typical linked list structure */ word op; PC
*code;};
PC *pcptr; /* global structures */PC *pcptr_copy;
#ifdef __STDC__int get_data(char *prompt)#elseint get_data(prompt)
char *prompt;#endif{ char buf[40]; int result, *result_ptr;
result_ptr = &result; do { fputs(prompt, stdout);
fgets(buf, sizeof(buf), stdin); sscanf(buf, "%i", result_ptr); }
while (result != 1 && result != 2); return(result);}
#ifdef __STDC__PC *fnc_A(void)#elsePC *fnc_A()#endif{ if ((pcptr =
pcptr_copy = (PC *)malloc(sizeof(PC))) == NULL) { perror("malloc(3)
failed"); exit(1); } pcptr->code = NULL; printf("fnc_A:
\tpcptr = %p\n", (void *)pcptr); printf(" \tpcptr->code =
%p\n", (void *)pcptr->code); return(pcptr);}
#ifdef __STDC__int main(void)#elseint main()#endif{ int init_choice, branch;
branch = get_data("Take the suspect branch [1] or stay`safe' [2]? ");
init_choice = get_data("Initialize `pcptr' with NULL [1] or malloc[2]? ");
puts("");
if (init_choice == 1) pcptr = NULL; else { if ((pcptr =
(PC *)malloc(sizeof(PC))) == NULL) { perror("malloc(3) failed");
exit(1); } } printf("main: [init] pcptr = %p\n\n",
(void *)pcptr);
printf("Taking branch %i.\n", branch); if (branch == 1) /*
* This branch works fine with the gcc-2.95 and SUN C compilers. * If
compiled with gcc {3,4}.X, however, the results are: * * >
SEGFAULT if `pcptr' is initialized to NULL * or * >
Corrupted value of `pcptr->code' if `pcptr' * is initialized to a
non-NULL address. * * The newer compilers appear to incorrectly
handle the case * where the assignment target is a global pointer
reference * (pcptr->code) and the value being assigned is returned by
* a function which can modify the global pointer itself. */
pcptr->code = fnc_A(); else { /* * All compilers can handle
this branch correctly * regardless of how `pcptr' is initialized.
*/ PC *tmp_ptr; tmp_ptr = fnc_A(); pcptr->code =
tmp_ptr; } printf("main: \tpcptr = %p", (void *)pcptr);
printf("\t(%p <- should be this)\n", (void *)pcptr_copy); printf("
\tpcptr->code = %p", (void *)pcptr->code); printf("\t(%p <- should be
this)\n", (void *)pcptr_copy); return(0);}
---------------------------------------------"Man is the lowest-cost,
150-pound, nonlinear, all-purpose computer system which can be mass-produced
by unskilled labor."-NASA in 1965
_________________________________________________________________
Windows Live Hotmail: Your friends can get your Facebook updates, right from
HotmailĀ®.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_4:092009
_______________________________________________
indiana-discuss mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/indiana-discuss