For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2008 > Bug help in AIX VisualAge C









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Bug help in AIX VisualAge C
fatfish

2008-03-17, 4:41 am

Hi, I met "Core" in my program. Help needed to review my source code.
Detail environment:
OS: AIX 5.3.0.0 (64bit)
Complier: VisualAge C 6

-----example.c-----------
#include <stdio.h>

struct Example {
int length;
int width;
};
typedef struct Example *EXAMPLE;

int
main(int argc, char **argv)
{
EXAMPLE exp;

exp = (EXAMPLE) malloc(sizeof(struct Example));

printf("step 1\n");
exp->length = 10;
printf("step 2\n");

exit(0);
}
-----------------------------

when I compile the code with: "cc -q32 example.c -o example", I got
the 32-bit mode object "example", and it runs normally with the
output:
step 1
step 2
but, with the 64-bit compile mode: "cc -q64 example.c -o example", the
"example" will "core" at "exp->length = 10;", the output:
step 1
Segmentation fault(coredump)
I took some time to debug, but got nothing. Thank you for your help.
malc@pulsesoft.com

2008-03-17, 7:22 pm

fatfish <renyufei83@gmail.com> writes:

> Hi, I met "Core" in my program. Help needed to review my source code.
> Detail environment:
> OS: AIX 5.3.0.0 (64bit)
> Complier: VisualAge C 6
>
> -----example.c-----------
> #include <stdio.h>
>
> struct Example {
> int length;
> int width;
> };
> typedef struct Example *EXAMPLE;
>
> int
> main(int argc, char **argv)
> {
> EXAMPLE exp;
>
> exp = (EXAMPLE) malloc(sizeof(struct Example));
>
> printf("step 1\n");
> exp->length = 10;
> printf("step 2\n");
>
> exit(0);
> }
> -----------------------------
>
> when I compile the code with: "cc -q32 example.c -o example", I got
> the 32-bit mode object "example", and it runs normally with the
> output:
> step 1
> step 2
> but, with the 64-bit compile mode: "cc -q64 example.c -o example", the
> "example" will "core" at "exp->length = 10;", the output:
> step 1
> Segmentation fault(coredump)
> I took some time to debug, but got nothing. Thank you for your help.


A guess: there's no prototype for malloc in the scope (#include
<stdlib.h> is missing) hence it's taken to mean `int malloc ();' and
in 64 bit mode where (most likely) `sizeof (int) < sizeof (ptr)' the
result (an int after promotion to pointer) is bogus. It's a good
idea to compile things with warnings enabled.

--
mailto:av1474@comtv.ru
fatfish

2008-03-17, 10:14 pm

Thank you, the program runs ok with <stdlib.h> included.
But why it's taken to mean `int malloc ();' instead of 'void
*malloc();' ?

On Mar 18, 3:55=A0am, m...@pulsesoft.com wrote:
> fatfish <renyufe...@gmail.com> writes:
>
>
>
>
>
>
>
>
> A guess: there's no prototype for malloc in the scope (#include
> <stdlib.h> is missing) hence it's taken to mean `int malloc ();' and
> in 64 bit mode where (most likely) `sizeof (int) < sizeof (ptr)' the
> result (an int after promotion to pointer) is bogus. It's a good
> idea to compile things with warnings enabled.
>
> --
> mailto:av1...@comtv.ru- Hide quoted text -
>
> - Show quoted text -


malc@pulsesoft.com

2008-03-18, 7:20 pm

fatfish <renyufei83@gmail.com> writes:

> Thank you, the program runs ok with <stdlib.h> included.
> But why it's taken to mean `int malloc ();' instead of 'void
> *malloc();' ?


Mainly because 3.3.2.2 of C90 standard says so.

[..snip..]

--
mailto:av1474@comtv.ru
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com