PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Missing prototype and resulting coredump
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Missing prototype and resulting coredump

Réponse
 
LinkBack Outils de la discussion
Vieux 18/10/2007, 07h31   #1
Rakesh UV
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Missing prototype and resulting coredump

Hi,
If i am not putting the function prototype of a function
returning a pointer, i get a core dump.Though this will happen less
probably on 32 bit machine

example
int main(int argc , char **argv)
{
char *base = basename(argv[0]) ;
printf("%s",base);
return 0;

}

The compiler assumes that basename is returning an int and thus we
loose 32 bit of the actual address, because pointer is 64 bit and
integer is 32 bit in 64 bit machines

I am working on
Os linux x86_64 2.6.9
Machine Hp
compiler gcc 3.46

Is there any option in GCC to make the default return value as long so
that i can preserve the actual address returned
or
do we have any other way in C to make it right
I know that putting the prototype would solve the problem, but
unfortunately
there are huge number of files


Rakesh UV

  Réponse avec citation
Vieux 18/10/2007, 07h49   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Missing prototype and resulting coredump

Rakesh UV wrote:
> I know that putting the prototype would solve the problem, but
> unfortunately
> there are huge number of files
>

Time to warm up your favorite text editor. Can't you add the
prototype(s) to an appropriate header and the add said header to all the
files with a script?

Porting smelly code from 32 to 64 bit is fraught with problems, lack of
prototypes is one you can easily fix.

--
Ian Collins.
  Réponse avec citation
Vieux 19/10/2007, 02h48   #3
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Missing prototype and resulting coredump

On Thu, 18 Oct 2007 06:31:17 -0000, Rakesh UV <uvrakesh@gmail.com>
wrote in comp.lang.c:

> Hi,
> If i am not putting the function prototype of a function
> returning a pointer, i get a core dump.Though this will happen less
> probably on 32 bit machine
>
> example
> int main(int argc , char **argv)
> {
> char *base = basename(argv[0]) ;


If your compiler does not issue a diagnostic for this code, either it
is badly broken, or you did not tell it to operate as an actual C
compiler. A diagnostic is required.

> printf("%s",base);
> return 0;
>
> }
>
> The compiler assumes that basename is returning an int and thus we
> loose 32 bit of the actual address, because pointer is 64 bit and
> integer is 32 bit in 64 bit machines


And what if 64 bit addresses are returned in a completely different
manner and you are really losing all 64 bits of the address?


> I am working on
> Os linux x86_64 2.6.9
> Machine Hp
> compiler gcc 3.46
>
> Is there any option in GCC to make the default return value as long so


Why? The code has undefined behavior. It is wrong. It is garbage.

> that i can preserve the actual address returned
> or


What if 64 bit long long is returned in a different register than a 64
bit pointer?

> do we have any other way in C to make it right


There is no way in C to make it right. The code has undefined
behavior. If a function returns anything other than an int, and you
do not have a prototype in scope, they there is no way to avoid
undefined behavior. Period. The only way to make it right is to have
a prototype in scope. Period.

> I know that putting the prototype would solve the problem, but
> unfortunately
> there are huge number of files


That's why there are programs that can search and replace in huge
numbers of files.

> Rakesh UV


Don't EVER port code this bad to another platform without fixing it.

And reeducate the programmers who wrote it. If they won't accept
reeducation, fire them.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  Réponse avec citation
Vieux 04/11/2007, 00h50   #4
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Missing prototype and resulting coredump

On Thu, 18 Oct 2007 06:31:17 -0000, Rakesh UV <uvrakesh@gmail.com>
wrote:

>Hi,
> If i am not putting the function prototype of a function
>returning a pointer, i get a core dump.Though this will happen less
>probably on 32 bit machine
>
>example
>int main(int argc , char **argv)
>{
> char *base = basename(argv[0]) ;
> printf("%s",base);
> return 0;
>
>}
>
>The compiler assumes that basename is returning an int and thus we
>loose 32 bit of the actual address, because pointer is 64 bit and
>integer is 32 bit in 64 bit machines
>
>I am working on
>Os linux x86_64 2.6.9
>Machine Hp
>compiler gcc 3.46
>
>Is there any option in GCC to make the default return value as long so
>that i can preserve the actual address returned


Even if there were such an option, your code would still invoke
undefined behavior. This is true anytime you cause the compiler to
make an assumption that is not valid. Why do you assume the compiler
will return a pointer using the same mechanism it uses to return an
integer? What is so onerous about providing a prototype that you are
willing to run this risk?

>or
>do we have any other way in C to make it right
>I know that putting the prototype would solve the problem, but
>unfortunately
>there are huge number of files


That's what headers are for.


Remove del for email
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14095 seconds with 12 queries