cant get value from pointer

Pages: 12
Hello,

I have a problem with variable

unsigned char* to

which I use as an argument of function a(). I need to use that variable later on in function b(), but when returning form function I can t get any value from "to". I tried loats of things, but nothing worked. If someone could help me ... thank you.

Here is code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <NTL/ZZ.h>
#include <conio.h>

NTL_CLIENT 

void a(char *from, unsigned char* to)
{
    int err;
    ZZ n, a, zz_from, zz_to;

    GenPrime(n, 20, err = 80);
    a = RandomBnd(n-1);

    int len = strlen(from)*8;
    zz_from = ZZFromBytes((const unsigned char*) from, len);
   
    zz_to = PowerMod(zz_from, a, n);
    size_t len2 = NumBytes(zz_to);
    to = new unsigned char[len2];
    memset(to, 0, len2);
    BytesFromZZ(to, zz_to, len2);
}

void b(unsigned char* to)
{
	//do something ...
}

int main(void)
{
   char * from = "h";
   unsigned char* to= NULL;

   a(from, to);
   b(to);

   _getch();
   return 0;
}


Edit: deleted this part; see below
Last edited on
that function is like this:

BytesFromZZ(unsigned char * p, const NTL::ZZ &a, long n)

and it converts the big number to string.

Last edited on
Consider this statement: "The only way to pass parameters to a function is by making copies of them." Think about what it means to pass a pointer to something, and exactly what line 20 is doing.

Okay, now, you have a pointer in one function (main()), which you want to modify from a different function (a()). What do you do when you want to modify a variable from a different function? You pass a pointer to it, correct? Then what do you have to do to modify a pointer from a different function? Hint: type 'T' has a corresponding type 'T *', for all values of T.

Note: There's a simpler way to do this, but I'd prefer you figured this one out, first.
i am figuring that out for 3 hours :)

... and I still dont get it :(((
What you're doing is passing a pointer (which is by value). This is just giving the function a copy of the original pointer. So when you use the new[] operator, you're assigning that memory address to the COPY, not the original. The original still points to the same address (NULL) when the function returns. The other problem is, the newly-allocated memory is leaked, because the local pointer copy gets automatically destroyed after the function call, so now you have no way to access the new memory.

It is analogous to doing this:
1
2
3
void myFunction(int i) {
    i = 5;
}


If you were to call this function, you'd never change the input value of i, because the function is only using a copy.

Last edited on
I didn' t know, it was a COPY. Yes, I can get value inside of a(), but not outside.

and what should I do to make original to point that adress? How to do that? I am still very confused about it.
so please, what should I do?
I got it, I had to use a double pointer
Usually you don't want to allocate memory for outside data inside a function like that, but it can be done. For example, the Windows API has some functions that do the following type of thing:

Instead of passing the pointer "*to", you could actually pass the pointer-of-the-pointer, or "**to", and work with that instead. Your function would look like this:

1
2
3
4
5
6
7
8
void a(char *from, unsigned char **to) {

    //Everything else as before

    *to = new unsigned char[len2];
    memset(*to, 0, len2);
    BytesFromZZ(*to, zz_to, len2);
}
;

And your function call in main() would look like:
 
a(from,&to);


Now, when you dereference the copy of "**to" that was passed in, you end up with the correct address where you intended to allocate memory in the first place.
yes, I already did that. Thank you very much :)
Oops... you must have submitted that while I was typing. Anyway, be careful with functions like that, because you still need to somehow keep track of exactly how much memory you allocated, so you don't end up with a memory leak.
yes, thank you again for your help :)
good for you for solving the problem but it think "pointer to pointer" is harder to manage. i really dont know what your trying to do but take a look at my work, a function returning a pointer. i think its a better way to do it. im new to c++ as well so tell me what you think.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int* return_pointer() {
    int* data = new int[5]; //allocate 5 ints
    for(int x=0; x<5; x++) //give a value of 10, 20, 30, 40, 50
        data[x] = (x+1)*10;
    return data; //return the pointer to the allocated ints
}

int main() {

    int* pointer = 0; //pointing to null
    pointer = return_pointer(); //pointer now points to the ints allocated by the function
    for(int x=0; x<5; x++) //prints the data
        cout << pointer[x] << "\n";
    return 0;
}

i had to do it that way, because function a() is actually like this:

 
void a(char * from,  unsigned char **to, unsigned char **to2)


so I need to modify 2 variables. If I had one, I would do it the same way as you did.
ok, its your own style.
another choice is to make the variable scope to be global.
either way, it solves the problem anyway
another choice is to make the variable scope to be global.
Oh, yeah. That makes a lot more sense. Of course it's better to expand the scope than to add a level of indirection.
@blackcoder41:

Yes, you can do it the way you first suggested, but that's definitely not "easier to manage". Either way, you ought to be monitoring the length of your array, either by:

1) Passing a pointer to a length integer, and returning the array pointer. You can dereference the length integer's pointer and assign its value inside the function, and then return the new array.
 
int *myFunction(int *pLength);

If you're using C++ instead of C, you can instead just pass the length integer as a non-const reference, and you can change the value that way without worrying about pointers.
 
int *myFunction(int& length);


2) Passing a double-pointer and returning the length as an int. You allocate memory as described in the above answers, and just return the length as a regular int. (This is preferred over #1 in most cases).
 
int myFunction(int **pArray);


3) Passing both the number (reference or pointer) and the double-pointer, and returning void.

Ultimately, if you're using C++, you might as well take advantage of its standard containers (or your own). That's the whole point of the language, really - objects manage things for themselves. For example, it is always preferable to use containers like STL's vector, in place of using arrays and having to keep track of lengths yourself. If we wanted to do that all the time, we'd just use pure C.
Last edited on
@jrohde
thanks, i am please to be correct by you and learning from mistakes. sorry if i dont know what i'm talking about, i am new to programming and dont know any C. i go straight to C++. and to be honest i dont understand you explanation in 1, can you please give complete code?

Ultimately, if you're using C++, you might as well take advantage of its standard containers (or your own). That's the whole point of the language, really - objects manage things for themselves. For example, it is always preferable to use containers like STL's vector, in place of using arrays and having to keep track of lengths yourself. If we wanted to do that all the time, we'd just use pure C.


i am about to say that he should build his own class, but yeah i think you've got a better idea on using vector if that is a built in class in c++. can you please explain what STL's vector mean? i am guessing vector is a built in class in standard library, but i really don't know. sorry for being n00b
can you please explain what STL's vector mean?
http://lmgtfy.com/?q=vector+C%2B%2B
Pages: 12