473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Troubling Alignment (I think) issue.

Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I'm having. Hopefully I can explain this best
through code below. ACSign and MyACSign functions below get compiled
(gcc) into one library, where as the code that executes these
functions gets compiled into a seperate library. Calls to MyACSign
work fine, calls to the straight ACSign do not and I believe it is due
to alignment issues, but I can't pinpoint how. This <i>should</ihave
been very elementary. Any help is of course greatly appreciated,

- Kevin
Function Code:
************************************************** ******************
double ACSign ( double a) {
if (a 0.0)
return(1.0);
else if (a < 0.0)
return(-1.0);
}

double * MyACSign (double * a) {
if (*a 0.0)
*a = 1.0;
else if (*a < 0.0)
*a = -1.0;
return a;
}

Execution Code:
************************************************** ******************
double test = 0 ;
double test1 = 0;
test = 0.707107;
test1 = test * -1;

printf("Locations: %u, %u\r\n", &test, &test1);

printf("0Double: %f, %f", test, test1);
printf("1Double: %f, %f", ACSign(test), ACSign(test1));
printf("2Double: %i, %i", (char)ACSign(test), (char)ACSign(test1));

printf("3Double: %f, %f", 0.707107, -0.707107);
printf("4Double: %f, %f", ACSign(0.707107), ACSign(-0.707107));
printf("5Double: %i, %i", (char)ACSign(0.707107),
(char)ACSign(-0.707107));

printf("6Double: %f, %f", test, test1);
printf("7Double: %f, %f", *(double*)MyACSign(&test),
*(double*)MyACSign(&test1));
printf("8Double: %i, %i", (char)*(double*)MyACSign(&test),
(char)*(double*)MyACSign(&test1));
************************************************** ******************
The output I get is:
Locations: 4263558272, 4263558264
0Double: 0.707107, -0.707107
1Double: 0.707107, -0.707107
2Double: -98, -98
3Double: 0.707107, -0.707107
4Double: 0.707107, 0.000000
5Double: -98, -98
6Double: 0.707107, -0.707107
7Double: 1.000000, -1.000000
8Double: 1, -1

Nov 6 '07 #1
12 1263
Kevin <ke********@gmail.comwrites:
Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I'm having.
I doubt your problems have anything to do with alignment.
Rather, I suspect that you are not properly declaring the
functions in a header file, e.g.
double ACSign(double);
double *MyACSign(double *);
The prototypes (the bits in parentheses) are optional but they
help the compiler to check the calls.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Nov 6 '07 #2
On Wednesday 07 Nov 2007 1:57 am Kevin <ke********@gmail.comwrote in
article <11**********************@i38g2000prf.googlegroups .com>:
Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I'm having. Hopefully I can explain this best
through code below. ACSign and MyACSign functions below get compiled
(gcc) into one library, where as the code that executes these
functions gets compiled into a seperate library. Calls to MyACSign
work fine, calls to the straight ACSign do not and I believe it is due
to alignment issues, but I can't pinpoint how. This <i>should</ihave
been very elementary. Any help is of course greatly appreciated,

- Kevin
Function Code:
************************************************** ******************
double ACSign ( double a) {
if (a 0.0)
return(1.0);
Both the parenthesis and the decimal point are unnecessary.
else if (a < 0.0)
return(-1.0);
Similarly here.
}

double * MyACSign (double * a) {
if (*a 0.0)
*a = 1.0;
else if (*a < 0.0)
*a = -1.0;
return a;
}

Execution Code:
************************************************** ******************
double test = 0 ;
double test1 = 0;
test = 0.707107;
test1 = test * -1;

printf("Locations: %u, %u\r\n", &test, &test1);
No. To print pointers use the '%p' specifier and cast the corresponding
argument to void *. Also the '\r' is not only not necessary but may
cause problems. The C Standard library translates a '\n' to whatever
the system uses as end-of-line.
printf("0Double: %f, %f", test, test1);
printf("1Double: %f, %f", ACSign(test), ACSign(test1));
printf("2Double: %i, %i", (char)ACSign(test), (char)ACSign(test1));
What's the point in casting a double value to a char and then passing
them inccorrectly to printf after telling it to look for an int? Use
the '%c' format to print a char.
printf("3Double: %f, %f", 0.707107, -0.707107);
printf("4Double: %f, %f", ACSign(0.707107), ACSign(-0.707107));
printf("5Double: %i, %i", (char)ACSign(0.707107),
Same problems again. Besides what are you expecting to see after a lossy
cast?
(char)ACSign(-0.707107));

printf("6Double: %f, %f", test, test1);
printf("7Double: %f, %f", *(double*)MyACSign(&test),
No. Use *(MyACSign(&test)).
*(double*)MyACSign(&test1));
Similarly.
printf("8Double: %i, %i", (char)*(double*)MyACSign(&test),
(char)*(double*)MyACSign(&test1));
Again this mysterious cast to a char value.
************************************************** ******************
The output I get is:
Locations: 4263558272, 4263558264
0Double: 0.707107, -0.707107
1Double: 0.707107, -0.707107
2Double: -98, -98
3Double: 0.707107, -0.707107
4Double: 0.707107, 0.000000
5Double: -98, -98
6Double: 0.707107, -0.707107
7Double: 1.000000, -1.000000
8Double: 1, -1
As you have invoked undefined behaviour in several places you could get
any kind of output. I suggest you fix the obvious errors and recompile
and rerun.

Nov 6 '07 #3
Bingo, I stumbled upon the same answer at the same time - thanks!. I
missed the implied declaration warning in the compiling code and my
partners missed this one function in the header.

Thanks again!

- Kevin
On Nov 6, 3:37 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
Kevin <kevinwo...@gmail.comwrites:
Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I'm having.

I doubt your problems have anything to do with alignment.
Rather, I suspect that you are not properly declaring the
functions in a header file, e.g.
double ACSign(double);
double *MyACSign(double *);
The prototypes (the bits in parentheses) are optional but they
help the compiler to check the calls.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6}*,*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

Nov 6 '07 #4
In <11**********************@i38g2000prf.googlegroups .comKevin <ke********@gmail.comwrites:
Calls to MyACSign work fine, calls to the straight ACSign do not
You're more likely to get useful responses if you describe *exactly* what
is wrong, rather than just saying "it doesn't work".
The output I get is:
Locations: 4263558272, 4263558264
0Double: 0.707107, -0.707107
1Double: 0.707107, -0.707107
2Double: -98, -98
3Double: 0.707107, -0.707107
4Double: 0.707107, 0.000000
5Double: -98, -98
6Double: 0.707107, -0.707107
7Double: 1.000000, -1.000000
8Double: 1, -1
Which of these do you think are wrong, and what were you expecting instead?

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Nov 6 '07 #5
Kevin wrote On 11/06/07 15:27,:
Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I'm having. Hopefully I can explain this best
through code below. ACSign and MyACSign functions below get compiled
(gcc) into one library, where as the code that executes these
functions gets compiled into a seperate library. Calls to MyACSign
work fine, calls to the straight ACSign do not and I believe it is due
to alignment issues, but I can't pinpoint how. This <i>should</ihave
been very elementary. Any help is of course greatly appreciated,
My bet is that the calling code has no declarations
for the called functions. This will cause a C99 compiler
to emit a diagnostic, but causes older compilers to assume
that the undeclared function returns an `int' value. Since
the two functions actually return `double' and `double*',
the behavior is undefined. In one case you get garbage:
the returned `double' is getting treated as an `int', or
the function's value is getting plucked from an integer
register instead of from the floating-point register where
the function put it, or something of that sort. In the
other case, it looks like you're lucking out: the `double*'
value seems to be returned in the same way an `int' would
have been, and the conversion from `int' to `double*' is
apparently not damaging it -- but that's not something you
can count on.

double ACSign ( double a) {
if (a 0.0)
return(1.0);
else if (a < 0.0)
return(-1.0);
... and if a == 0.0 exactly ...?
}
--
Er*********@sun.com

Nov 6 '07 #6
santosh <sa*********@gmail.comwrites:
On Wednesday 07 Nov 2007 1:57 am Kevin <ke********@gmail.comwrote in
article <11**********************@i38g2000prf.googlegroups .com>:
[...]
>double ACSign ( double a) {
if (a 0.0)
return(1.0);

Both the parenthesis and the decimal point are unnecessary.
> else if (a < 0.0)
return(-1.0);

Similarly here.
>}
[...]

Yes, the parentheses are unnecessary, and as a matter of style
I strongly prefer *not* to use extra parentheses. So:
return 1.0;
rather than
return(1.0);
The latter looks too much like a function call, and it isn't one.
(But it's perfectly legal, and K&R1 used that style.)

As for the decimal point, presumably you aren't suggesting
return 10;
rather than
return 1.0;
8-)}

I prefer to use floating-point literals for floating-point numbers.
Yes, you can use
return 1;
but ``1'' is of type int, and is implicitly converted to double. The
compiler will almost certainly generate exactly the same code as for
return 1.0;
but I find the more explicit form clearer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 7 '07 #7
Keith Thompson said:
Keith Thompson (The_Other_Keith) [...]
<http://www.ghoti.net/~kst>
On that page, you write:

"Versions of this page are available at
<http://www.users.cts.com/king/k/kst/and..."

but the cts link is dead.

No skin, etc, but I thought you might like to know! :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 7 '07 #8
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:
>Keith Thompson (The_Other_Keith) [...]
<http://www.ghoti.net/~kst>

On that page, you write:

"Versions of this page are available at
<http://www.users.cts.com/king/k/kst/and..."

but the cts link is dead.

No skin, etc, but I thought you might like to know! :-)
Yeah, my cts.com account died years ago, and I haven't updated my
ghoti.net home page in even longer. I suppose I should either update
it or drop it from my sig.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 7 '07 #9
Keith Thompson said:

<snip>
Yeah, my cts.com account died years ago, and I haven't updated my
ghoti.net home page in even longer. I suppose I should either update
it or drop it from my sig.
I'd leave it until tomorrow if I were you.

Oh, you did. :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 7 '07 #10
On 6 Nov, 20:46, santosh <santosh....@gmail.comwrote:
On Wednesday 07 Nov 2007 1:57 am Kevin <kevinwo...@gmail.comwrote in
article <1194380852.818883.183...@i38g2000prf.googlegroups .com>:
<snip>
printf("Locations: %u, %u\r\n", &test, &test1);

No. To print pointers use the '%p' specifier and cast the corresponding
argument to void *. Also the '\r' is not only not necessary but may
cause problems. The C Standard library translates a '\n' to whatever
the system uses as end-of-line.

very rarely explicit \r is needed when the system output doesn't
match the required output. I've even had to generate \r\r\n!
I suppose the output should have been binary rather than text.
I can't rememeber if it was.

<snip>

--
Nick Keighley

Nov 7 '07 #11
On Wednesday 07 Nov 2007 2:13 pm Nick Keighley
<ni******************@hotmail.comwrote in article
<11**********************@57g2000hsv.googlegroups. com>:
On 6 Nov, 20:46, santosh <santosh....@gmail.comwrote:
>On Wednesday 07 Nov 2007 1:57 am Kevin <kevinwo...@gmail.comwrote
in article <1194380852.818883.183...@i38g2000prf.googlegroups .com>:

<snip>
printf("Locations: %u, %u\r\n", &test, &test1);

No. To print pointers use the '%p' specifier and cast the
corresponding argument to void *. Also the '\r' is not only not
necessary but may cause problems. The C Standard library translates a
'\n' to whatever the system uses as end-of-line.


very rarely explicit \r is needed when the system output doesn't
match the required output. I've even had to generate \r\r\n!
I suppose the output should have been binary rather than text.
I can't rememeber if it was.

<snip>
Is such an implementation non-conforming as per the Standard?

Nov 7 '07 #12
"santosh" <sa*********@gmail.coma écrit dans le message de news:
fg**********@registered.motzarella.org...
On Wednesday 07 Nov 2007 1:57 am Kevin <ke********@gmail.comwrote in
article <11**********************@i38g2000prf.googlegroups .com>:
>Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I'm having. Hopefully I can explain this best
through code below. ACSign and MyACSign functions below get compiled
(gcc) into one library, where as the code that executes these
functions gets compiled into a seperate library. Calls to MyACSign
work fine, calls to the straight ACSign do not and I believe it is due
to alignment issues, but I can't pinpoint how. This <i>should</ihave
been very elementary. Any help is of course greatly appreciated,

- Kevin
Function Code:
************************************************* *******************
double ACSign ( double a) {
if (a 0.0)
return(1.0);

Both the parenthesis and the decimal point are unnecessary.
> else if (a < 0.0)
return(-1.0);

Similarly here.
>}
I'm amazed you noticed the extra parentheses and decimal points, and fail to
point at the much more major problem in ACSign: it does not return anything
if a is 0 ;-)
To properly account for negative zero, it should probably return a.

double ACSign(double a) {
return a < 0 ? -1 : a 0 ? 1 : a;
}

If we don't care about negative zeroes, ACSign should probably return an int
and can be made even more obscure:

int ACSign(double a) { return (a 0) - (a < 0); }

Any half decent compiler would have complained about the missing return if
configured properly. Indeed it is a shame that most of them don't complain
for such obvious bugs when invoked without extra flags.

--
Chqrlie.
Nov 8 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
7
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround...
5
by: Sandeep | last post by:
struct x { int a; char b; int c; char d; }; typedef struct x X;
8
by: Cardman | last post by:
I am hopeful that someone can quickly solve my image alignment issue when things are just not going right and where my attempts to solve this alignment issue have all failed. First of all take a...
3
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the...
16
by: pandionx | last post by:
Hi, This may be a silly question, but I would appreciate some insight into this. I know the compiler will align a class' members. However, it is unclear to me whether the compiler will align...
15
by: Matthew | last post by:
Hi, I'm mainly a coder, PHP at the moment, but from time to time need to design and use some css. I've a css text alignment issue. Mostly to align text neatly in the past I've used tables....
31
by: Chris Thomasson | last post by:
How many C compilers provide extensions which allow for a standard implementation of the following hack? ____________________________________________________________________ #include <stdio.h> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.