How to iterate through const char** variable - c++11

I want to iterate through a const char** variable.
auto temp = ReturnCharPointer();//This method return const char**
This pointer consists of two strings 0-"John", 1-"David".
When I trying iterate through it I am getting exception as violating access for reading.
for(int i=0; temp[i] != NULL; i++)
{
printf(temp[i]);//once i's position is 2 that is no data is there, it is
//throwing exception
}
As per the comments below, please find the below for little more explanation,
I have a C# business layer, where I have a method defined which returns string[].
In my managed c++ DLL I have referenced this C# sharp dll and trying to call this method and store the string[](in managed c++ array^) to list or vector.
C# Method:
public string[] ArrayofStrings()
{
return new[] {"John", "Deer"};
}
Managed c++:
void DisplayName()
{
auto strArray = CSharpObj.ArrayofStrings();
//How to iterate and assign it list<string>. ????
}
How can I convert array^ to list or vector.

Related

How to convert raw to smart pointer when smart pointer is declared in header?

In our code we are mainly using raw pointers and I would like to introduce smart pointers step by step (i.e. without changing existing functions). We have a factory that returns a pointer to a created object. Several methods in the class make use of this object so the pointer to the created object is declared as a class member.
std::unique_ptr<IObject> object;
Now, I would like to replace the raw pointer with a smart pointer.
I found out that I can pass a raw pointer to the constructor of a smart pointer (in my case I think unique_ptr is correct one) but because the smart pointer is declared in the header this does not work.
I tried using
object = make_unique<IObject>(CreateObject(1));
but this gives a compilation error because IObject is abstract.
The only solution I could find is creating a temporary unique_ptr and then using move.
//Factory.h
std::unique_ptr<IObject> object;
//Factory.cpp
IObject* ObjectFactory::CreateObject(int type)
{
switch( type )
{
case 1:
return new A();
case 2:
return new B();
}
}
//My solution is:
object = std::move( std::unique_ptr<IObject>(ObjectFactory::CreateObject(1)) );
I was wondering if there is a better way than creating a temporary smart pointer and then using move to transfer ownership.
You can use reset member function of unique pointer to set Iobject object type to a raw pointer from your factory.
//Factory.h
std::unique_ptr<IObject> object;
//Factory.cpp
IObject* ObjectFactory::CreateObject(int type)
{
switch( type )
{
case 1:
return new A();
case 2:
return new B();
}
}
//use reset
object.reset(ObjectFactory::CreateObject(1));
Here is a minimal working example.
#include <iostream>
#include <memory>
class Abs {
public:
virtual ~Abs() = 0;
};
Abs::~Abs(){ std::cout << "called\n"; }
class A: public Abs {};
class B: public Abs {};
Abs* Factory(int type)
{
switch(type)
{
case 1:
new A();
break;
case 2:
new B();
break;
}
}
int main()
{
std::unique_ptr<Abs> obj;
obj.reset(Factory(1));
return 0;
}

Factory function for initialization of static const struct with array and lambda

I have a structure that should be statically initialized.
struct Option
{ char Option[8];
void (*Handler)(const char* value);
};
void ParseInto(const char* value, const char** target); // string option
void ParseInto(const char* value, int* target, int min, int max); // int option
static int count = 1;
static const char* name;
static const Option OptionMap[] =
{ { "count", [](const char* value) { ParseInto(value, &count, 1, 100); } }
, { "name", [](const char* value) { ParseInto(value, &name); } }
// ...
};
Up to this point it works.
To get rid of repeating the lambda function definition over and over (there are dozens) I want to use a factory like this:
struct Option
{ const char Option[8];
const void (*Handler)(const char* value);
template<typename ...A>
Option(const char (&option)[8], A... args)
: Option(option)
, Handler([args...](const char* value) { ParseInto(value, args...); })
{}
};
static const Option OptionMap[] =
{ { "count", &count, 1, 100 }
, { "name", &name }
};
This does not work for two reasons:
I did not find a type for the first constructor parameter option that perfectly forwards the initialization of the character array. The difficult part is that the length of the assigned array does not match the array length in general.
The even harder part is that the lambda function has a closure and therefore cannot decay to a function pointer. But all parameters are compile time constants. So It should be possible to make the constructor constexpr. However, lambdas seem not to support constexpr at all.
Anyone an idea how to solve this challenge?
The current work around is a variadic macro. Not that pretty, but of course, it works.
Context is C++11. I would not like to upgrade for now, but nevertheless a solution with a newer standard would be appreciated. Problems like this tend to reappear from time to time.
There are some further restrictions by the underlying (old) code. struct Option must be a POD type and the first member must be the character array so a cast from Option* to const char* is valid.

I am getting runtime error upon incrementing the pointer, what is the plausible reason?

I am new to C++, while I was implementing a tag class, I encountered a runtime error.
Upon debugging I found out that the runtime error was caused by incrementing pointer(**) attrib_list, but the other pointer which points to same memory address produces no error on incrementing,
Please explain to me what is the reason for this odd behaviour ?
I used hackerrank online ide to compile this code
class Tag{
public:
Tag():
tagname{""}, sz{0}, t_sz{0}, attrib_list{nullptr}, ptr_nav{nullptr}{
}
Tag(string name, int n_att):
tagname{name}, sz{0}, t_sz{n_att}, attrib_list{new string*[n_att]}{
for(int i=0; i<n_att; i++){
attrib_list[i] = new string[2];
}
ptr_nav = &attrib_list[0]; //take risk here
}
~Tag(){
for(int i=0; i< t_sz; i++){
delete[] attrib_list[i];
}
attrib_list = nullptr;
ptr_nav = nullptr;
t_sz, sz = 0;
}
// this function produces rintime error
void add_attribute(string name, string value){
(*attrib_list)[0] = name;
(*attrib_list)[1] = value;
sz++;
attrib_list++;
}
/*
This does not produce any error, why ????
void add_attribute(string name, string value){
(*ptr_nav)[0] = name;
(*ptr_nav)[1] = value;
sz++;
ptr_nav++;
}
*/
private:
string tagname;
string **attrib_list, **ptr_nav;
int sz, t_sz;
};
int main() {
Tag t("tag1", 2);
t.add_attribute("value1", "1"); //runtime error
t.add_attribute("value2", "2"); //runtime error
return 0;
}
After two calls to add_attribute, attrib_list is incremented twice and now points past the end of the original new[]-allocated array. Which is not in itself a problem.
But then your Tag instance goes out of scope, its destructor runs and tries to access attrib_list[i] on a now-invalid attrib_list pointer, which of course exhibits undefined behavior.
Unrelated to the immediate issue: t_sz, sz = 0; does not assign 0 to two variables (as you seem to believe), but only to sz. Read about comma operator in your favorite C++ reference. In any case, you don't need to do that in the first place (nor to set the two pointers to nullptr) - they are about to be destroyed anyway, their values at that point don't matter.

an iterator that constructs a new object on dereference

I have a Visual Studio 2013 C++11 project where I'm trying to define an iterator. I want that iterator to dereference to an object, but internally it actually iterates over some internal data the object requires for construction.
class my_obj
{
public:
my_obj(some_internal_initialization_value_ v);
std::wstring friendly_name() const;
// ...
};
class my_iterator
: public boost::iterator_facade<
my_iterator,
my_obj,
boost::forward_traversal_tag>
{
// ...
private:
my_obj& dereference() const
{
// warning C4172: returning address of local variable or temporary
return my_obj(some_internal_initialization_value_);
}
};
int main( int argc, char* argv[])
{
my_container c;
for (auto o = c.begin(); o != c.end(); ++o)
printf( "%s\n", o->friendly_name().c_str() );
}
These internal values are unimportant implementation details to the user and I'd prefer not to expose them. How can I write the iterator that does this correctly? The alternative is that I would have to do something like this:
my_container c;
for (auto i = c.begin(); i != c.end(); ++i)
{
my_obj o(*i);
printf( "%s\n", o.friendly_name().c_str() );
}
From the boost page on iterator_facade, the template arguments are: derived iterator, value_type, category, reference type, difference_type. Ergo, merely tell it that references are not references
class my_iterator
: public boost::iterator_facade<
my_iterator,
my_obj,
boost::forward_traversal_tag,
my_obj> //dereference returns "my_obj" not "my_obj&"
See it working here: http://coliru.stacked-crooked.com/a/4b09ddc37068368b

c# PInvoke w_char_t**

I have the following c++ function which I cannot alter (3rd-Party):
[c++]
int __stdcall TEST(wchar_t **xml, int &result_size)
{
// xml is instantiated here!
}
[c#]
class native
{
[DllImport("somedll.dll")]
public static extern int TEST(StringBuilder a, ref int size);
{
}
}
Example:
StringBuilder b = new StringBuilder();
int size = 0;
native.Test(b,ref size)
The stringbuilder object only contains first character . If I resize the object:
b.Length = size; The data is incorrect except first character.
Is this the correct way to pass wchar_t** from c++ to c#?
Regards,
John
The function would be p/invoked like this:
[DllImport(#"mylib.dll")]
static extern int TEST(out IntPtr xml);
I removed the size paramter since it is not needed since you can use a null-terminated string.
Call the function like this:
IntPtr xmlptr;
int retval = TEST(out xmlptr);
string xml = Marshal.PtrToStringUni(xmlptr);
// deallocate xmlptr somehow
The tricky bit is to deallocate the memory allocated on the native side. Either use a shared allocator, e.g. the COM allocator. Or export a deallocator from the native code.
Personally I'd re-design the interface to use COM BSTR. I'd have the C++ return a BSTR and on the managed side use [MarshalAs(UnmanagedType.BStr)]. Then the framework handles all the deallocation and marshalling for you.

Resources