Thread: can someone tell me why this creates a segmentation fault?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    can someone tell me why this creates a segmentation fault?

    Code:
    include <stdio.h>
    
    void getSignMantissaExponent(float number, int* sign, unsigned int* mantissa, int* exponent){
      int* second = (int *) &number;
      *sign = (*second) & 0x80000000; /* note one ampersand for bitwise-and */
      *sign = *sign >> 31;
    
      if (*sign == 0)
        *sign = 1;
      else
        *sign = -1;
    
      *exponent = (*second) & 0x7F800000;
      *exponent = *exponent >> 23;
      *exponent = *exponent - 127;
    
      *mantissa = (*second) & 0x7FFFFF;
      *mantissa = *mantissa | 0x800000;
    
      printf("Sign bit is %d\n", *sign);
      printf("Exponent is %d\n", *exponent);
      printf("Mantissa is %x\n", *mantissa);
    }
    
    
    int main()
    {
      float first = -10;
      int* sign;
      int* exponent;
      unsigned int* mantissa;
    
      getSignMantissaExponent(first, sign, mantissa, exponent);
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    sign, mantissa and exponent aren't initialized

    Code:
    int* sign;
      int* exponent;
      unsigned int* mantissa;
    
      getSignMantissaExponent(first, sign, mantissa, exponent);

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I want them to be initialized after the getSignMantissaExponent runs

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so how would I do this?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by -EquinoX- View Post
    so how would I do this?
    Don't declare them as pointers. Pass their addresses.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    well this is a requirement in my assignment that I have to pass it in a pointers

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -EquinoX- View Post
    well this is a requirement in my assignment that I have to pass it in a pointers
    What we mean is that you don't actually have any space to put the mantissa once you compute it. You have a pointer, but you don't own the memory address it points to. So declare variables sign, exponent, and mantissa, and pass &sign, &exponent, and &mantissa to your function.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yeah that makes more sense.. thank you!

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    When do you make second a pointer to an int when it is given the addess of a float?

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh never mind I fixed it
    Last edited by -EquinoX-; 03-04-2008 at 07:15 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So what are you trying to do? Your code does not find the correct exponent or mantissa.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    do you mean it does not because I substract it with 127 on the exponent and on the mantissa I added the hidden bit 1 by using the or?? because that's what I want to do

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmm. Yes, if you don't manipulate the mantissa & exponent, it's right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM