I am doing unit testing using google C++ testing tools. I am a novice C programmer as well as tester. The following is my code snippet :
TEST_F (def_fix, SR_M_T) {
I_M_S * arg;
RM__S_F_R_T* message_data;
char mT[MAX_M_T_LENGTH] = "data";
strcpy(message_data->mT,mT);
message_data->rUID[0] = '\0';
message_data->uT[0] = '\0';
unsigned char buffer[sizeof(RM__S_F_R_T)+2];
memcpy(&buffer[2],message_data,sizeof(RM__S_F_R_T));
buffer[0] = 1; //This assignment is not important for this test
buffer[1] = 2; //This assignment is not important for this test
arg->payload_data = buffer;
handleSR(arg);
}
What the above does is creating an incoming message. This incoming message arg
is to be used as parameter to call a function handleSR (void *).
def_fix
is a default fixture class which contains SetUp()
and TearDown()
and nothing fancy in them.
RM__S_F_R_T
is a structure which contains and 3 strings. I_M_S
is a structure containing a pointer to RM__S_F_R_T
.
When I google test this, it gives a segmentation error. However when I change: RM__S_FR_R_ * message_data;
to RM__S_FR_R_ message_data;
and use .
operator instead of ->
operator, things tend to work. Can anyone explain?
These two statements:
RM__S_F_R_T* message_data;
strcpy(message_data->mT,mT);
are the culprit. You never initialise the RM__S_F_R_T
(who designed this thing?!) pointer. When not using it as a pointer, you create an instance on the stack that you can use.
To fix either don't use a pointer (you already found that out), or allocate memory:
message_data = malloc(sizeof(RM__S_F_R_T));
don't forget to free(message_data)
when you are done with the instance, or you'll leak memory.
It's because you declare message_data
to be a pointer, but don't point it at anything. So then it will point to a random memory location, causing undefined behavior when you dereference it.
Either declare it as not a pointer, or allocate memory for it.
你还没有为RM__S_F_R_T *提供内存。之后使用message_data
message_data = (RM__S_F_R_T*)malloc(sizeof(RM__S_F_R_T));
Initialize I_M_S * arg
and RM__S_F_R_T* message_data
:
I_M_S * arg = malloc(sizeof(I_M_S));
RM__S_F_R_T* message_data = malloc(sizeof(RM__S_F_R_T));
Also, make sure you don't use these allocated pointers after the scope of function TEST_F()
because you assign arg->payload_data = buffer
where scope of localized variable buffer
ends after the function TEST_F()
.
And so, free these pointers before you leave TEST_F()
:
free(arg); free(message_data);
message_data
doesn't point to anything. You need to pass an initialised RM__S_F_R_T struct pointer to your function as an argument or initialise it inside the function itself.
Further, I presume message_data->mT
is a char pointer. It's not pointing to anything currently.
You can assign space for it using malloc
, or if it's a char array then make sure it's big enough to hold mT
into it (provided its struct is already initialised).
The above applies to all of message_data
's members.
As the other posters have already mentioned, don't forget to free()
anything you have malloc()
-ed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.