|
Which would be preferred code coverage tool for a bug fix project.
I have a base code and I had done some modification in some existing function as part of fix. I would like to ensure the code coverage of modified codes. Is there any tools in your knowledge that can help me to find how much of my modified code is covered.
aks
|
|
|
|
|
Hi all;
I am relearning C after about 20 years. (I had a done a course after which never wrote a line of C code to earn beer money). I would appreciate any sort of help with my current quandary - a function returning a pointer and returning structure variables.
#include <stdio.h>
typedef struct
{
float Centre[3];
float Radius;
} Sphere;
Sphere *MakeSphere(float *centre, float *radius);
void PrintSphereDetails(Sphere *s);
int main(int argc, const char * argv[])
{
float c1[3] = {10.0,20.0,30.0};
float r1 = 10.0;
Sphere *s1 = MakeSphere(&c1[0], &r1);
PrintSphereDetails(s1);
return 0;
}
Sphere *MakeSphere(float *centre, float *radius)
{
Sphere s;
s.Centre[0] = *centre++;
s.Centre[1] = *centre++;
s.Centre[2] = *centre;
s.Radius = *radius;
return &s;
}
void PrintSphereDetails(Sphere *s)
{
printf("------------------------------------------------\n");
printf("Centre: (%6.2f, %6.2f, %6.2f)\n", s->Centre[0], s->Centre[1], s->Centre[2] );
printf("Radius: %6.2f\n\n", s->Radius);
}
I am working in XCode and the code works, but I get a warning "Address of stack memory associated with local variable 's' returned." for the code line return &s in function MakeSphere. That is exactly what, I think, I need so what is XCode telling me?
Another problem, which XCode solved for me has to do with the way to get the values of the structure variables in function PrintSphereDetails. Initially I had them as s.Centre[0] but XCode flagged these and recommended doing s->Centre[0]. What is the difference?
Regards
Eric
The author's comes from a long line of evolved fish.
|
|
|
|
|
ericgahn wrote: Sphere s;
That line of code is creating the instance on the stack. The stack is only valid within the method and there is no assurance that it will continue to be valid.
You need to allocate it on the heap - look at the malloc method and associated other methods.
And remember to delete it when you are done with it.
|
|
|
|
|
Thank you for your reply.
Very helpful. I googled malloc() and used this site to further understand suggested soln.
Eric
|
|
|
|
|
As another option, you could change your function this way
void MakeSphere(float *centre, float *radius, Sphere *);
and initialize the struct allocated by the caller.
Veni, vidi, vici.
|
|
|
|
|
CPallini wrote: void MakeSphere(float *centre, float *radius, Sphere *);
So if I understand correctly a Sphere 'instance' would be created in Main() and then a pointer to this instance passed to the function?
|
|
|
|
|
Yes, you got it right.
Veni, vidi, vici.
|
|
|
|
|
When I attempt to set custom deleter of std::unique_ptr, I got this error:
.\src\iMoney.cpp(5): error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(HINSTANCE__ *,void (__cdecl *const &)(HMODULE))' : cannot convert parameter 2 from 'void (__thiscall iMoney::* )(HMODULE)' to 'void (__cdecl *const &)(HMODULE)'
2> with
2> [
2> _Ty=HMODULE,
2> _Dx=void (__cdecl *)(HMODULE)
2> ]
2> Reason: cannot convert from 'void (__thiscall iMoney::* )(HMODULE)' to 'void (__cdecl *const )(HMODULE)'
2> There is no context in which this conversion is possible
2>
2>Build FAILED.
anybody can help me?
Here is my code snippet:
std::unique_ptr<HMODULE, decltype(&iMoney::freeLib)> m_ptrModule;
void iMoney::freeLib(HMODULE hModule){
m_pThisApp->FreeLibrary(hModule);
}
iMoney::iMoney(CReader_App* pApp)
: m_pThisApp(pApp)
, m_ptrModule(nullptr, &iMoney::freeLib)
{
}
|
|
|
|
|
I believe the delete method should take a iMoney not HMODULE (which is what the error says.) And since it takes an iMoney there is no reason for it to not be static.
|
|
|
|
|
Windows 7, visual Studio 2008, C++
WCHAR *op_type = NULL;
if( x )
op_type = &L"First";
else
op_type = &L"Second";
This solicits the error
Quote: error C2440: '=' : cannot convert from 'const wchar_t (*)[6]' to 'WCHAR *'
What is my syntax error?
I tried the declaration:
const wchar_t *op_type2 = NULL;
and received the error:
Quote: error C2440: '=' : cannot convert from 'const wchar_t (*)[6]' to 'const wchar_t *'
Which seems wrong to me. Why does the pointer care how long the string is? It is but the address of the first character.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 7-Jun-14 20:28pm.
|
|
|
|
|
For the first part of your question, you do not need the & sign.
WCHAR *op_type = NULL;
if( x )
op_type = L"First";
else
op_type = L"Second";
For the second part of your question, you should not be getting any errors.
It is a valid statement.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
Lets see if I really understand what you told me.
The declaration:
const WCHAR *op_type = NULL;
was good. The use:
op_type = &L"Shutdown";
was incorrect. The declaration of the constant string:
&L"Shutdown";
is effectively a WCHAR array. Arrays are always passed by address so adding the & symbol was creating an pointer to a pointer. The error message:
Quote: error C2440: '=' : cannot convert from 'const wchar_t (*)[9]' to 'const WCHAR *'
was telling me that but I was too wrapped up in reading the [9] as a length rather than an indicator that this is an array and the address is passed rather than the value.
Thank you for helping me with this.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
You logic is flawed but your result is correct.
The "L" prefix is a sort of precompiler literal Microsoft has put in place it's not a macro or such and it is sort of bound like the quotes are in the precompiler
look at how you write a normal string constants
const char* txtcnst = "SOME_CONSTANT_TEXT";
There is no "&" pointer there so why just because you want a unicode string would they change it
The answer is it is the way it is because that is how C language defined the use of constants and think of L as part of the quotes "" simply telling the compiler the text is unicode format.
In vino veritas
|
|
|
|
|
|
Is this line of code valid in C++:
int a{1};
|
|
|
|
|
|
|
My searches revealed only a partial answer to int a{1};
What does this do and why might I use it?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
|
I should have included "initialize" in my search.
On one hand it really doesn't add anything.
On the other, it can standardize initialization methods. An int can be initialized in the same manner as an array. That is of little significance, but often consistency adds more than is immediately apparent.
Thanks for taking the time to reply.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
Hi,
Yes. This is the new direct list initialization available in C++11.
Best Wishes,
-David Delaune
|
|
|
|
|
Thank you for your reply.
|
|
|
|
|
Friends, how to compress speex ogg packets into a *. Oog file?(can make music player play the files)
please!
|
|
|
|
|
I am getting the errors related to WiX Toolset while compiling one of the applications of C++ in VS2005.
The error message shows that:
"WiX Toolset compiler has encountered a problem and needs to close".
I googled for the related information and tried by installing the 2007 version of Toolset compiler wix38.exe. But still not able to resolve the issue.
Please let me know how to rectify the error..
|
|
|
|
|
Without any more detail no one can guess what is wrong. You would be better posting your query on a forum dedicated to this product, since it has nothing to do with C++ per se.
|
|
|
|