|
-
July 21st, 2005, 01:33 PM
#1
my pointers are not getting changed when passing into a function...
I have the following function that takes in 2 pointers, does copies and then returns. The problem I have is that when the function returns I want the pointers to have the modified values but they retain the original values. Can someone tell me why & how to fix it?
Code:
unsigned char *p_source_buf;// {I set this to a memory buffer which read in a file};
unsigned char *pInBuf = new char[200];
unsigned int num_bytes_to_insert;
void main(void){
byte_copy(m_pInBuf, p_source_buf, num_bytes_to_insert);
}
const unsigned int BITMASK_FOR_LEAST_SIGNIFICANT_TWO_BITS = 0x3;
inline void byte_copy(unsigned char *p_in_buf, unsigned char *p_source_buf, unsigned int num_bytes_to_insert){
//Transfer remaining bytes individually
unsigned int remaining_bytes = static_cast<unsigned int>(num_bytes_to_insert & BITMASK_FOR_SIGNIFICANT_TWO_BITS);
for(unsigned int i=0;i<remaining_bytes;i++)
*p_in_buf++ = *p_source_buf++;
}
Last edited by C++ Newbie; July 21st, 2005 at 02:13 PM.
-
July 21st, 2005, 02:06 PM
#2
Re: my pointers are not getting changed when passing into a function...
What values are you passing into the function as parameters?
-
July 21st, 2005, 02:06 PM
#3
Re: my pointers are not getting changed when passing into a function...
the code you posted looks fine, meaning the pointers in the function should changed, unless the remaining_bytes is qual to 0 and the loop is never performed 
if i helped dont forget to rate :-)
Cheers
-
July 21st, 2005, 02:13 PM
#4
Re: my pointers are not getting changed when passing into a function...
I showed how i called the function.
-
July 21st, 2005, 02:21 PM
#5
Re: my pointers are not getting changed when passing into a function...
you showed the function ineed but you didnt show the paramters you passing to it if num_bytes_to_insert == 0 then the loop never get perform
casue the next line
Code:
unsigned int remaining_bytes = static_cast<unsigned int>(num_bytes_to_insert & BITMASK_FOR_SIGNIFICANT_TWO_BITS);
will be zero!
Code:
unsigned int num_bytes_to_insert;
this paramter is never initialzied and its zero!
so like i said the loop never performed hence pointers not moving.
Cheers
-
July 21st, 2005, 03:02 PM
#6
Re: my pointers are not getting changed when passing into a function...
Oh I see what you are saying. That is not the problem. I had to cut and paste this code from a larger framework so what I showed you is essentially what is going on. I stepped thru the debugger and it is passing between 2 & 300 bytes at a time. The pointers look like they increment properly within the routine but when the routine returns, the data in the buffers has changed but not the pointers themselves.
-
July 21st, 2005, 03:17 PM
#7
Re: my pointers are not getting changed when passing into a function...
I think this is because the pointers are passed to the function by value. That is, when p_in_buf is incremented, m_pInBuf is not, because they are not the same pointers.
It's the same as this:
Code:
void myfunc(int a, int b)
{
a = b;
b++;
}
void main() {
int x = 0, y = 1;
myfunc(x, y);
cout << x << ", " << y << endl;
}
The output will be: "0, 1"
This example can be rewritten as:
Code:
typedef int something;
void myfunc(something a, something b)
{
a = b;
b++;
}
void main() {
something x = 0, y = 1;
myfunc(x, y);
cout << x << ", " << y << endl;
}
Now consider what happens when you replace the first line with
Code:
typedef unsigned char* something;
Do you understand?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 21st, 2005, 10:46 PM
#8
Re: my pointers are not getting changed when passing into a function...
what you really want to do? I debug the code it seems no problem.Do you want to change the value of the buffer ? or you want do other ?
here is my code:
#include <string.h>
#include <stdio.h>
unsigned char p_source_buf[] = "This is a test";// {I set this to a memory buffer which read in a file};
unsigned char *pInBuf = new unsigned char[200];
unsigned int num_bytes_to_insert = 15;
inline void byte_copy(unsigned char *p_in_buf, unsigned char *p_source_buf, unsigned int num_bytes_to_insert){
//Transfer remaining bytes individually
//unsigned int remaining_bytes = static_cast<unsigned int>(num_bytes_to_insert & BITMASK_FOR_SIGNIFICANT_TWO_BITS);
for(unsigned int i=0;i<num_bytes_to_insert;i++)
*p_in_buf++ = *p_source_buf++;
}
void main(void){
byte_copy(pInBuf, p_source_buf, num_bytes_to_insert);
printf("%s\n",pInBuf);
}
Do we misunderstand you ?
-
July 22nd, 2005, 03:10 AM
#9
Re: my pointers are not getting changed when passing into a function...
Firstly use code tags.
Secondly, main returns int not void.
Thirdly, you are only copying up to 3 bytes. I think though you wanted to copy a 4-byte word at a time until you reached the last part of the buffer at which point you are copying the remaining bytes. However I don't see the part where you are transferring 4 bytes at a time.
Fourthly, you are not modifying soruce_buf so that one should be const unsigned char *. But const unsigned void* would be even better.
If you are going to do it that way for performance reasons then you'd want to put the boundary at 4 bytes, not necessarily the start of the buffer. And then which 4-byte boundary, the source or the target?
Are you sure this is going to be faster than memcpy? (Probably not, and probably therefore not worth the effort).
-
July 22nd, 2005, 04:45 PM
#10
Re: my pointers are not getting changed when passing into a function...
?? I never new there was such thing as "unsigned void*" ...
Old Unix programmers never die, they just mv to /dev/null
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|