Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I am doing a simple application to define a structure and place data in tha structure to learn the concept of structures. But when trying to insert data to structure i am getting an access violation. Following is the code portions.


C++
In Test.h file


C++
<pre lang="C++">typedef struct Msg
{
unsigned char*   message_id;
unsigned char*   message_name;
}Msg_t; 


C++
In Test.cpp file



C++
Msg_t *new_node[10];

const char *src = "E0";
new_node[0]->message_id = (unsigned char *)_strdup(src); //getting access violation error here.


I am totally new to this environment.. Why am i getting this error?
Help..
Posted
Updated 25-May-18 8:37am

Quote:
Msg_t *new_node[10];
The above line create an array of 10 pointers. Fine.


Quote:
new_node[0]->message_id = ...
In order to make the above line work, new_node[0] must point to a valid Msg_t instance while it is pointing to garbage.

You have to write something like
new_node[0] = new Msg_t();

before dereferencing new_node[0].
 
Share this answer
 
v2
Comments
Kornfeld Eliyahu Peter 13-Jan-16 6:57am    
Just to add that when running from IDE (Visual Studio) all pointers are initialize to 0, where in real runtime it will be a true garbage...
+5
CPallini 13-Jan-16 7:02am    
Thank you.
Lekshmi KR 13-Jan-16 7:01am    
I initialized as you told me and got it working perfectly.. Thank you so much for the help
CPallini 13-Jan-16 7:03am    
You are welcome. Don't forget to delete any new-ed object:
https://en.wikipedia.org/wiki/New_and_delete_(C%2B%2B)
Lekshmi KR 13-Jan-16 7:05am    
Yea sure.. I have deleted it after use.
This is a common problem and a moments thought will tell you what the problem is.
Look at the address that it's complaining about: "0x00000000" - or "zero" as it's also known. How likely is it that anything you need to use is going to be located at the first possible address in memory? Absolutely not probable!

So use the debugger and find out why it's zero - which means finding out exactly which bit is zero.
My guess is that your new_node array is not initialized, so it contains zeros - but only you can confirm that as we can't run your code!
When you know that, you can find out why it's zero - probably because you haven't executed the code to actually insert any Msg_t elements into it...
 
Share this answer
 
Perhaps try to isolate the error down to the smallest chunk that would create the error; For example, if the Test.cpp code is:

Msg_t *new_node[10];
 
const char *src = "E0";
_strdup(src); //getting access violation error here?


Do you still get the error message here?

Or if that works, how about here? Does that compile fine?

Msg_t *new_node[10];
 
const char *src = "E0";

unsigned char *testresult;

testresult = (unsigned char *) _strdup(src); //getting access violation error here??



Also, you can find some documentation here _strdup, _wcsdup, _mbsdup for _strdup.

It seems like it might already being returning (char *) and perhaps you could just adjust the structure to align with what it is returning.

Explicitly, if the structure is changed to:

typedef struct Msg
{
char   *message_id;
char   *message_name;
}Msg_t;


then does the same issue occur?
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900