473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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.
Jul 21 '06 #1
1 1524
Vinod wrote:
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
Jul 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Neil Zanella | last post by:
Hello, Consider the following program. There are two C style string stack variables and one C style string heap variable. The compiler may or may not optimize the space taken up by the two stack...
3
by: ambar.shome | last post by:
i have a function as listed below: char* ltoa(char* chr) { char* myChr=new char; strcpy(myChr,chr); return myChr; } in the above code i am returning a reference or pointer to a local
12
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
19
by: Robert Smith | last post by:
I am wondering why it is possible to return a pointer to a string literal (ie. 1) but not an array that has been explicitly allocated. (ie. 2) ? Both would be allocated on the stack, why does the...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.