Click to See Complete Forum and Search --> : my pointers are not getting changed when passing into a function...
C++ Newbie
July 21st, 2005, 01:33 PM
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?
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++;
}
Rigel
July 21st, 2005, 02:06 PM
What values are you passing into the function as parameters?
golanshahar
July 21st, 2005, 02:06 PM
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
C++ Newbie
July 21st, 2005, 02:13 PM
I showed how i called the function.
golanshahar
July 21st, 2005, 02:21 PM
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
unsigned int remaining_bytes = static_cast<unsigned int>(num_bytes_to_insert & BITMASK_FOR_SIGNIFICANT_TWO_BITS);
will be zero!
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
C++ Newbie
July 21st, 2005, 03:02 PM
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.
D_Drmmr
July 21st, 2005, 03:17 PM
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:
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:
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
typedef unsigned char* something;
Do you understand?
qynum123
July 21st, 2005, 10:46 PM
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 ?
NMTop40
July 22nd, 2005, 03:10 AM
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).
HighCommander4
July 22nd, 2005, 04:45 PM
?? I never new there was such thing as "unsigned void*" ...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.