|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|