returned Address is out of bounds

Discussion in 'C' started by asilter, Jul 31, 2007.

  1. asilter

    asilter New Member

    Joined:
    Jul 31, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I'm trying to assign variables where vpSec0 points.

    Code:
    void * vpSec0 = NULL;
    CreateHVFESection0(vpSec0);
    
    CreateHVFESection0 function is below.

    Code:
    void CreateHVFESection0(void * vpSec0)
    {
            int hSec0;      
            size_t * nbytes = (size_t *) malloc(sizeof(size_t));
            hSec0 = bitio_o_open(); 
            
            /* 'B','U','F','R' */
            bitio_o_append(hSec0,66,8);
            bitio_o_append(hSec0,85,8);
            bitio_o_append(hSec0,70,8);
            bitio_o_append(hSec0,82,8);
     
            /* Total length of BUFR message in bytes */
            bitio_o_append(hSec0,0,24);
            
            /* BUFR Edition Number = 4 */
            bitio_o_append(hSec0,4,8);
            vpSec0 = bitio_o_close(hSec0, nbytes);
     
            free(nbytes);
            nbytes = NULL;
           
     
    }
    
    CreateHVFESection0 uses some other functions. But I think the problem is here:
    Code:
    vpSec0 = bitio_o_close(hSec0, nbytes);
    because i run the program with gdb debugger. Before coming here, vpSec0 is 0x0, means NULL. Everything is normal to here.

    Code:
    void *bitio_o_close (handle, nbytes)
    
    int handle;
    size_t *nbytes;
    
    /* This function closes a output-bitstream identified by HANDLE and returns
       a pointer to the memory-area holding the bit-stream.
    
       parameters:
       HANDLE:  Bit-stream-handle
       NBYTES:  number of bytes in the bitstream.
    
       The funcion returns a pointer to the memory-area holding the bit-stream or
       NULL if an invalid handle was specified. The memory area must be freed by
       the calling function.
    */
    
    {
    
      if (!bios[handle].used) return NULL;
    
    /******* Fill up the last byte with 0-bits */
    
      while (bios[handle].nbits % 8 != 0) bitio_o_append (handle, 0, 1);
    
      *nbytes = (size_t) ((bios[handle].nbits - 1) / 8 + 1);
      bios[handle].used = 0;
      return (void *) bios[handle].buf;
    }
    void *bitio_o_close (handle, nbytes)
    
    int handle;
    size_t *nbytes;
    
    /* This function closes a output-bitstream identified by HANDLE and returns
       a pointer to the memory-area holding the bit-stream.
    
       parameters:
       HANDLE:  Bit-stream-handle
       NBYTES:  number of bytes in the bitstream.
    
       The funcion returns a pointer to the memory-area holding the bit-stream or
       NULL if an invalid handle was specified. The memory area must be freed by
       the calling function.
    */
    
    {
    
      if (!bios[handle].used) return NULL;
    
    /******* Fill up the last byte with 0-bits */
    
      while (bios[handle].nbits % 8 != 0) bitio_o_append (handle, 0, 1);
    
      *nbytes = (size_t) ((bios[handle].nbits - 1) / 8 + 1);
      bios[handle].used = 0;
      return (void *) bios[handle].buf;
    }
    
    
    when i step into bitio_o_close function, before
    Code:
    return (void *) bios[handle].buf
    , the address of bios[handle].buf is 0x2a99700930. So we expect that after returning, vpSec0's address will also be 0x2a99700930. But after returning when i print vpSec0, it's address seems 0xffffffff99700930, and this is out of bounds which falls me in Segmentation faults further in my program.

    Please help.
    Thanx.
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    vpSec0, the argument in function Create...., is a copy of vpSec0. Its scope is local to the function. It disappears when the function returns. You should return the value and assign it to vpSec0.
    Code:
    void * vpSec0 = NULL;
    vpSec0 = CreateHVFESection0();
    
    Code:
    void [COLOR=Red]*[/COLOR] CreateHVFESection0()
    {
        ...
        free(nbytes);
        nbytes = NULL;
        [COLOR=Red]return vpSec0;[/COLOR]
    }
    
    The alternative is to pass a pointer to vpSec0 and assign it in the function:
    Code:
    void * vpSec0 = NULL;
    CreateHVFESection0([COLOR=Red]&[/COLOR]vpSec0);
    
    Code:
    CreateHVFESection0(void [COLOR=Red]**[/COLOR] vpSec0)
    {
        ...
       [COLOR=Red]*[/COLOR]vpSec0 = bitio_o_close(hSec0, nbytes);
        free(nbytes);
        nbytes = NULL;
    }
    
     
  3. asilter

    asilter New Member

    Joined:
    Jul 31, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I tried just as you said. But the returning address is 0xffffffff99700910. still out of bounds. And I get segmentation fault furtherly. I want also to send you bitio_o_append function, maybe there, there can be a leak, corruption or something.

    Code:
    typedef struct bitio_stream {          /* structure that defines a bitstrem */
    int used;                            /* identifier if the bitstream is used */
      char *buf;                           /* buffer holding the bitstream */
      long nbits;                          /* currend size of bitstream (counted
                                              in bits !) */
      size_t size;                         /* current size of allocated memory for
                                              holding the bitstream. */
    } bitio_stream;
    
    /*===========================================================================*/
    long bitio_o_append (handle, val, nbits)
    
    int handle;
    unsigned long val;
    int nbits;
    
    /* This function appends a value to a bitstream.
    
       parameters:
    
       HANDLE:  Indicates the bitstream for appending.
       VAL:     Value to be output.
       NBITS:   Number of bits of VAL to be output to the stream. Note that NBITS
                muste be less that sizeof (LONG)
    
       The return-value is the bit-position of the value in the bit-stream, or -1
       on a fault.
    */
    
    {
    /******* Check if bitstream is allready initialized and number of bits does not
             exceed sizeof (unsigned long). */
    
      [URL=http://www.go4expert.com/articles/c-cpp-assert-function-t27488/]assert[/URL] (bios[handle].used);
      assert (sizeof (unsigned long) * 8 >= nbits);
    
    /******* check if there is enough memory to store the new value. Reallocate
             the memory-block if not */
    
      if ((bios[handle].nbits + nbits) / 8 + 1 > (long) bios[handle].size) {
        bios[handle].buf = realloc (bios[handle].buf, bios[handle].size + INCSIZE);
        if (bios[handle].buf == NULL) return 0;
            memset (bios[handle].buf + bios[handle].size, 0, INCSIZE);
        bios[handle].size += INCSIZE;
      }
    
    /******* output data to bitstream */
    bitio_o_outp (handle, val, nbits, bios[handle].nbits);
      bios[handle].nbits += nbits;
    
      return bios[handle].nbits;
    }
    
    
    and the function which is used by bitio_o_append:

    Code:
    /*===========================================================================*/
    void bitio_o_outp (handle, val, nbits, bitpos)
    
    int handle;
    unsigned long val;
    int nbits;
    long bitpos;
    
    /* This function outputs a value to a specified position of a bitstream
    
       parameters:
    
       HANDLE:  Indicates the bitstream for output.
       VAL:     Value to be output.
       NBITS:   Number of bits of VAL to be output to the stream. Note that NBITS
                must be less then sizeof (LONG)
       BITPOS:  bitposition of the value in the bitstream.
    */
    
    {
      int i, bit, bitval;
      size_t byte;
      char *pc, c;
    
    /******* Check if bitstream is allready initialized and number of bits does not
             exceed sizeof (unsigned long). */
    
      assert (bios[handle].used);
      assert (sizeof (unsigned long) * 8 >= nbits);
    
      for (i = nbits - 1; i >= 0; i --) {
    
    /******* Get bit-value */
    
        bitval = (int) (val >> i) & 1;
    
    /******* calculate bit- and byte-number for output */
    
        /*byte = (int) (bitpos / 8);
    bit  = (int) (bitpos % 8);*/
        byte = (int) (bitpos / 8);
        bit  = (int) (bitpos % 8);
        bit  = 7 - bit;
    
    /******* set bit-value to output stream */
    
        pc = bios[handle].buf + byte;
        if (bitval) {
          c = (char) (1 << bit);
          *pc |= c;
        }
        else {
          c = (char) (1 << bit);
          c ^= 0xff;
          *pc &= c;
        }
        bitpos ++;
      }
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice