473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2328
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

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

Similar topics

1
by: Randy Jackson | last post by:
I'm attempting to debug some code that uses the System function. When the function is called, it returns Error 1. Does anyone know what that error might be, or where I can find a list of error...
1
by: Steven T. Hatton | last post by:
If I do: typedef vector<size_t> v_T; pair<v_T, v_T> b_T; b_T bounds(v_T(n), v_T(n)); // if that doesn't work // std::make_pair(v_T(n), v_T(n)); b_T diff_v(n); void...
6
by: dharmadam | last post by:
Is it possible to pass a column name or the order of the column name in the DB2 table table function. For example, I want to update the address of a person by passing one of the address column name...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
5
by: Travis | last post by:
I am using a function that returns a const char * that is usually a word, etc. How can I check to see if what it returns is empty? I tried if (function() == "") and (function() == NULL) and...
2
by: sam.barker0 | last post by:
Hi , I am having 3 functions.When I step through when func b returns to funca.it throws an error "cannot find function bounds" funca() { .... ... funcb(); }
7
by: e2point | last post by:
hi, i got a program that is suppose to run 24x7x365. However after functioning for around 15 minutes, it crashes due to a segmentation fault. program is written in c++ and runs in RH Linux 4. When...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.