Char* variable in struct having junk address instead of NULL ptr

G

Guest

Hi,

In VC8 project, I am having a struct which is having a char* variable.

Now I am creating a 3 elements array object for the struct. I send the base
address of the object using VARIANT to a function where I am casting to
struct again using reinterpret_cast.

Then If I am trying to traverse the array object from the index 0 to 2, it
is working fine, If I am going for the index 3rd element, that char* variable
addres is having some junk address, so some where it is crashing. That char*
variable address should be NULL pointer or Bad Pointer.

But sometimes if I am going to the index 4th element, char* variable address
is NULL. It is not consistant.

What can we do for that? I can't even initialize the char* variable in that
struct to NULL(0) in VC8.

Thanks,
Vinod.
 
D

David Wilkinson

Vinod said:
Hi,

In VC8 project, I am having a struct which is having a char* variable.

Now I am creating a 3 elements array object for the struct. I send the base
address of the object using VARIANT to a function where I am casting to
struct again using reinterpret_cast.

Then If I am trying to traverse the array object from the index 0 to 2, it
is working fine, If I am going for the index 3rd element, that char* variable
addres is having some junk address, so some where it is crashing. That char*
variable address should be NULL pointer or Bad Pointer.

But sometimes if I am going to the index 4th element, char* variable address
is NULL. It is not consistant.

What can we do for that? I can't even initialize the char* variable in that
struct to NULL(0) in VC8.

Thanks,
Vinod.

Vinod:

A "3 elements array object" can only be indexed from 0 to 2. Index 3 is
off the end of the array, so anything in it is garbage. And if you write
to it, you will trash your memory.

To initaialize a char* member variable (or any other variable) of a
struct, use the constructor or initializer list, just as for a class.

David Wilkinson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top