473,387 Members | 1,891 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,387 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 2330
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.