471,305 Members | 1,257 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Function returns an address that is out of bounds

Hi,

I'm trying to assign variables where vpSec0 points.

Expand|Select|Wrap|Line Numbers
  1. void * vpSec0 = NULL;
  2. CreateHVFESection0(vpSec0);
  3.  
CreateHVFESection0 function is below.

Expand|Select|Wrap|Line Numbers
  1. void CreateHVFESection0(void * vpSec0)
  2. {
  3.         int hSec0;      
  4.         size_t * nbytes = (size_t *) malloc(sizeof(size_t));
  5.         hSec0 = bitio_o_open(); 
  6.  
  7.         /* 'B','U','F','R' */
  8.         bitio_o_append(hSec0,66,8);
  9.         bitio_o_append(hSec0,85,8);
  10.         bitio_o_append(hSec0,70,8);
  11.         bitio_o_append(hSec0,82,8);
  12.  
  13.         /* Total length of BUFR message in bytes */
  14.         bitio_o_append(hSec0,0,24);
  15.  
  16.         /* BUFR Edition Number = 4 */
  17.         bitio_o_append(hSec0,4,8);
  18.         vpSec0 = bitio_o_close(hSec0, nbytes);
  19.  
  20.         free(nbytes);
  21.         nbytes = NULL;
  22.  
  23.  
  24. }
  25.  
CreateHVFESection0 uses some other functions. But I think the problem is here:
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. void *bitio_o_close (handle, nbytes)
  2.  
  3. int handle;
  4. size_t *nbytes;
  5.  
  6. /* This function closes a output-bitstream identified by HANDLE and returns
  7.    a pointer to the memory-area holding the bit-stream.
  8.  
  9.    parameters:
  10.    HANDLE:  Bit-stream-handle
  11.    NBYTES:  number of bytes in the bitstream.
  12.  
  13.    The funcion returns a pointer to the memory-area holding the bit-stream or
  14.    NULL if an invalid handle was specified. The memory area must be freed by
  15.    the calling function.
  16. */
  17.  
  18. {
  19.  
  20.   if (!bios[handle].used) return NULL;
  21.  
  22. /******* Fill up the last byte with 0-bits */
  23.  
  24.   while (bios[handle].nbits % 8 != 0) bitio_o_append (handle, 0, 1);
  25.  
  26.   *nbytes = (size_t) ((bios[handle].nbits - 1) / 8 + 1);
  27.   bios[handle].used = 0;
  28.   return (void *) bios[handle].buf;
  29. }
  30. void *bitio_o_close (handle, nbytes)
  31.  
  32. int handle;
  33. size_t *nbytes;
  34.  
  35. /* This function closes a output-bitstream identified by HANDLE and returns
  36.    a pointer to the memory-area holding the bit-stream.
  37.  
  38.    parameters:
  39.    HANDLE:  Bit-stream-handle
  40.    NBYTES:  number of bytes in the bitstream.
  41.  
  42.    The funcion returns a pointer to the memory-area holding the bit-stream or
  43.    NULL if an invalid handle was specified. The memory area must be freed by
  44.    the calling function.
  45. */
  46.  
  47. {
  48.  
  49.   if (!bios[handle].used) return NULL;
  50.  
  51. /******* Fill up the last byte with 0-bits */
  52.  
  53.   while (bios[handle].nbits % 8 != 0) bitio_o_append (handle, 0, 1);
  54.  
  55.   *nbytes = (size_t) ((bios[handle].nbits - 1) / 8 + 1);
  56.   bios[handle].used = 0;
  57.   return (void *) bios[handle].buf;
  58. }
  59.  
  60.  
when i step into bitio_o_close function, before
Expand|Select|Wrap|Line Numbers
  1. 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.
Jul 31 '07 #1
1 2224
weaknessforcats
9,208 Expert Mod 8TB
Part of your problem is right here:
void CreateHVFESection0(void * vpSec0)
{
etc...
Inside this function vpSec0 is a copy of the vpSec0 used to make the call.

C has only call by value so function arguments are always copies of the variables used to call the funciton.

To change the address in the vpSec0 used on the call you need to pass the address of the pointer:

Expand|Select|Wrap|Line Numbers
  1. void CreateHVFESection0(void ** vpSec0)
  2. {
  3. etc...
  4.  
Then inside CreateHVFESection0() you change the caller's pointer by:

Expand|Select|Wrap|Line Numbers
  1. *vpSec0 = bitio_o_close(hSec0, nbytes);
  2.  
Jul 31 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Randy Jackson | last post: by
1 post views Thread by Steven T. Hatton | last post: by
6 posts views Thread by dharmadam | last post: by
8 posts views Thread by Ravindranath Gummadidala | last post: by
5 posts views Thread by Travis | last post: by
2 posts views Thread by sam.barker0 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.