Can't successfully pass an char * from a function

Hi,

For an exercise I'm trying to pass a char * from a function. Somehow it's not giving the address though. What am I doing wrong?

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char* takemychar(char * cp) {
    char * nieuwe = new char; 
    cout << (long)&nieuwe << "= new address"<< endl;

    return nieuwe;
}

int main(int argc, const char * argv[])
{    
   
    char c = 'c';
    char * tp = &c;
    cout << (long)tp << endl;
    tp = takemychar(tp);
    cout << (long)tp << endl;    
    delete tp;
}


output:

140734958778559
140734958778480= nieuwe adres
4454353440


For one, I don't understand why the function is using an address with a lower number than the first declared char c.
More important, I was expecting I was passing tp the address of nieuwe.
Yet it didn't happen and created a whole new address>

What am I doing wrong?
Last edited on
cout << (long)&nieuwe << "= new address"<< endl;
You are printing the address of the pointer. Remove the & and you should get the same output as line 15 gives.
@Peter
It was giving me a headache. And now seeing the solution it's so obvious. Thanks a ton!!!
Great, now still one more thing I don't understand. I made the function slightly more complex now, pointing to a "array" rather than a single char.
When trying to delete that array afterwards, it appears not to be empty.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char* takemychar(char * cp) {
    char * nieuwe = new char[sizeof(cp)]; cout << (long)nieuwe << "= new address"<< endl;
    for (int i = 0; i < sizeof(cp); i++)
        nieuwe[i] = cp[i];
        cout << (long)nieuwe << "= nieuwe adres "<< *nieuwe << endl;
        nieuwe[sizeof(cp)-1] = '\0';
        cout << (long)nieuwe << "= nieuwe adres"<< endl;
    return nieuwe;
}

int main(int argc, const char * argv[])
{    
   
    char c[8] = "beestje";
    char * tp = c;
    cout << (long)tp << endl;
    tp = takemychar(tp);
    cout << (long)tp << "= " << tp[4] << endl;    
    delete[] tp;
    cout << (long)tp << "= " << tp[4] << endl;  
} 


140734850087088
140596429720096= new address
140596429720096= nieuwe adres b
140596429720096= nieuwe adres
140596429720096= t
140596429720096= t


Why does delete[] tp not delete the array it's pointing to?
Note that sizeof(cp) will give you the size of the pointer and not the size of the string.

You shouldn't access the array after it has been deleted. It's undefined behaviour and anything could happen.

What probably happens is that the memory still contains the same content ready to be used for future allocations. If you allocate a new array it is likely that it will get the same address and the same content as the old array had when it was deleted.

delete[] tp;
cout << (long)tp << "= " << tp[4] << endl;
Why does delete[] tp not delete the array it's pointing to?


delete[] does in fact delete the array it's pointing to, where "deleting" stands for marking the storage the array occupied as available for future allocations, and executing the destructors of the array elements (which, in your case, do nothing)

However it does not modify the value of the object (of type char*) called "tp", so it still holds the same address, and since no new allocations happened yet, when you're accessing the freshly deleted array element with tp[4], you're still seeing the old value. In this case, on your compiler. But be aware that dereferencing a pointer to the non-existant object is undefined behavior and not guaranteed to work (however you define 'work').

You should treat that tp after delete[] tp; the same way you'd treat it after char* tp;, as an invalid pointer.

Also keep in mind that new[]/delete[] are almost never used in C++: we have strings and vectors.
I see, that makes a lot of sense. So the code was working fine after all.

I'd be happy to move on to strings & vectors, unfortunately this was the limit of the exercise, which turns out to be a hell of a good practice :-)

Thanks for your time and clarification Peter & Cubbi.
Topic archived. No new replies allowed.