Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > portability problem with a function returning a struct

Reply
Thread Tools

portability problem with a function returning a struct

 
 
Army1987
Guest
Posts: n/a
 
      05-16-2007
Under Linux (gcc) this works OK, but under Windows (lcc-win32)
p.Blue always is zero.
Within the function itself, result.Blue is correct (either 0, 255,
or the floor of the expression assigned to it modulo 256, according
to the sign of n). But when returned to main(), it magically turns
to zero. (The code below is entirely copied and pasted.)
What the deuce is happening? Any ideas? sizeof(struct Pixel) is 3,
if that can matter to anything (but so it is with gcc).

#define DEBUG
#include <stdio.h>
#include <limits.h>
#include <math.h>

struct Pixel {
unsigned char Red;
unsigned char Green;
unsigned char Blue;
} colormap(int n, int max);

#ifdef DEBUG
int main(void)
{
int n, max;
while (scanf("%d%d",&n, &max) > 1) {
struct Pixel p = colormap(n, max);
printf("%d %d %d\n", (int)p.Red, (int)p.Green,
(int)p.Blue);
if (!p.Blue)
puts("p.Blue is *really* zero.");
}
return 0;
}
#else
/*real program*/
#endif

struct Pixel colormap(int n, int max)
{
struct Pixel result;
if (n < 1) { /* black if negative, white if zero */
result.Red = n ? 0 : UCHAR_MAX;
result.Green = n ? 0 : UCHAR_MAX;
result.Blue = n ? 0 : UCHAR_MAX;
} else {
const double pi = 4 * atan(1);
double val = UCHAR_MAX*(1 - log(n)/log(max))
+ log(n)/log(max);
double hue = 2 * pi * log(n) / log(2.0);
result.Red = val * (1 + cos(hue))/2;
result.Green = val * (1 + cos(hue - 2*pi/3))/2;
#ifdef DEBUG
printf("%g\t", val * (1 + cos(hue - 4*pi/3))/2);
#endif
result.Blue = val * (1 + cos(hue - 4*pi/3))/2;
}
#ifdef DEBUG
printf("result.Blue is %d\n", (int)result.Blue);
#endif
return result;
}

Behaviour with gcc:
army1987@army1987-laptop:~$ ./a.out
-1 255
result.Blue is 0
0 0 0
p.Blue is *really* zero.
0 255
result.Blue is 255
255 255 255
1 255
63.75 result.Blue is 63
255 63 63
42 255
35.4608 result.Blue is 35
9 80 35
23 32767
144.707 result.Blue is 144
0 121 144
ok thanks
army1987@army1987-laptop:~$

Behaviour with lcc-win32:
C:\lcc\projects\lcc>test
-1 255
result.Blue is 0
0 0 0
p.Blue is *really* zero.
0 255
result.Blue is 255
255 255 0
p.Blue is *really* zero.
1 255
63.75 result.Blue is 63
255 63 0
p.Blue is *really* zero.
42 255
35.4608 result.Blue is 35
9 80 0
p.Blue is *really* zero.
23 32767
144.707 result.Blue is 144
0 121 0
p.Blue is *really* zero.
go f*** yourself

C:\lcc\projects\lcc>



 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      05-18-2007
Army1987 said:

> Under Linux (gcc) this works OK, but under Windows (lcc-win32)
> p.Blue always is zero.


I can't see any obvious issues with the code. I recommend that you try
to find the simplest possible program that exhibits the same problem,
and re-post it. For example, if you throw out all the trig, and just
set the values directly within colormap(), does the problem remain?

But this does look like a bug in the implementation. No surprise there,
given the maintainer's attitude to conformance and portability.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      05-18-2007
In article <f2k988$ns2$(E-Mail Removed)>, Army1987 <(E-Mail Removed)> wrote:
>Under Linux (gcc) this works OK, but under Windows (lcc-win32)
>p.Blue always is zero.


I'd guess it's a bug in struct return in lcc-win32.

The question is remarkably similar to

http://forums.belution.com/en/cpp/000/052/60s.shtml

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      05-18-2007
"Richard Heathfield" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed)...
> Army1987 said:
>
>> Under Linux (gcc) this works OK, but under Windows (lcc-win32)
>> p.Blue always is zero.

>
> I can't see any obvious issues with the code. I recommend that you try
> to find the simplest possible program that exhibits the same problem,
> and re-post it. For example, if you throw out all the trig, and just
> set the values directly within colormap(), does the problem remain?

Yes, as I showed in the original post:
struct Pixel colormap(int n, int max)
{
struct Pixel result;
if (n < 1) { /* black if negative, white if zero */
result.Red = n ? 0 : UCHAR_MAX;
result.Green = n ? 0 : UCHAR_MAX;
result.Blue = n ? 0 : UCHAR_MAX;
} else {
[snip]
#ifdef DEBUG
printf("result.Blue is %d\n", (int)result.Blue);
#endif
return result;
}

Behaviour with lcc-win32:
C:\lcc\projects\lcc>test
[snip]
0 255
result.Blue is 255
255 255 0
p.Blue is *really* zero.

> But this does look like a bug in the implementation. No surprise there,
> given the maintainer's attitude to conformance and portability.

I guess that, as soon as I manage to have a working Internet
connection on Linux, I will add the following to every program I'll
ever write:
struct Pixel { unsigned char Red, Green, Blue; } ****(void)
{
struct Pixel ****_;
****.Red = ****.Green = ****.Blue = 1;
return ****;
}
int main(void) {
if (!****().Blue) system("format c: /AUTOTEST");


 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      05-18-2007
On 16 May, 17:26, "Army1987" <(E-Mail Removed)> wrote:

> struct Pixel colormap(int n, int max)
> {
> struct Pixel result;

....
> return result;
>
> }


What happens to the storage allocated for result at the end of the
colormap function?

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      05-18-2007
"Army1987" <(E-Mail Removed)> ha scritto nel messaggio
news:f2kal1$pmq$(E-Mail Removed)...
> struct Pixel { unsigned char Red, Green, Blue; } ****(void)
> {
> struct Pixel ****_;
> ****.Red = ****.Green = ****.Blue = 1;

Yeah, I meant ****_.Red = etc...


 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      05-18-2007

<(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) ups.com...
> On 16 May, 17:26, "Army1987" <(E-Mail Removed)> wrote:
>
>> struct Pixel colormap(int n, int max)
>> {
>> struct Pixel result;

> ...
>> return result;
>>
>> }

>
> What happens to the storage allocated for result at the end of the
> colormap function?


The same which happens with any non-array automatic variable, I
guess. At least, that is what is supposed to happen.


 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      05-18-2007
On 18 May, 14:45, (E-Mail Removed) wrote:
> On 16 May, 17:26, "Army1987" <(E-Mail Removed)> wrote:
>
>
>
> > struct Pixel colormap(int n, int max)
> > {
> > struct Pixel result;

> ...
> > return result;

>
> > }

>
> What happens to the storage allocated for result at the end of the
> colormap function?


OOPS - very bad. Forget I said it, though it's probably related in
some way. As RT suggests it's a bug with lcc-win32's method of
returning structs, and I'd guess that some compiler "magic" tries to
make the struct inside colormap() accessible to the caller.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-18-2007
Army1987 said:

> "Richard Heathfield" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed)...
>
>> But this does look like a bug in the implementation. No surprise
>> there, given the maintainer's attitude to conformance and
>> portability.

> I guess that, as soon as I manage to have a working Internet
> connection on Linux, I will add the following to every program I'll
> ever write:
> struct Pixel { unsigned char Red, Green, Blue; } ****(void)


Welcome to the bozo bin. See you in 30 days.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-19-2007
Army1987 wrote:

> while (scanf("%d%d",&n, &max) > 1) {


> 0 0 0


Are you entering three values
when your scanf call is only looking for two?

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Returning a Struct from a Function alex.j.k2@gmail.com C++ 12 05-20-2008 08:55 AM
struct initializer efficiency and portability Laurent Deniau C Programming 1 08-28-2007 03:22 PM
Access from function in struct (pointer to function) to other items in the struct (advanced) Ole C Programming 4 10-26-2004 09:43 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments