Passing pointer on pointers into function [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Couldn't find answer to this. When I have pointer on pointers
char **buffer;
and I want to pass it to a function
void some_func(char **buffer) {}
so after this function call buffer will contain data from this function how should I call this function please ?

in your example it is correct but the fact is that you are passing this pointer to pointer by value so pass it by pointer:
// initialize it
char** buffer;
void some_func(char ***buffer) {} // by reference
and in function call:
some_funct(&buffer);

some_func(&buffer);
That's the call you need.
And the function should be...
void some_func(char ***buffer) {}
To manipulate buffer...
*buffer = something;

Related

Returning pointer to an array of arrays created using new [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have created an array of arrays using char (*H)[N] = new char[M][N];
I want to return this pointer to my main function. My question is what should the function return type be in this case? Am I allowed to have a return type as a pointer to an array of arrays.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes, this would be the syntax for your particular case:
char (*get_array())[N] {
return H;
}
But you really should consider using either std::unique_ptr<char[N]> or std::array<std::array<char, N>, M>.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes. One way to do that would be to define a type alias and use it as the return type.
using MyArrayPointer = char (*)[N];
MyArrayPointer foo()
{
auto ptr = new char[M][N];
return ptr;
}

How to protect a pointer's address that an user outside the class can modify? c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a buffer that will sent the pointer to read the data directly to do zero copy. But how can I protect the address pointer from code outside the class?
const void * returnReadPointer(size_t arraySize)
{
if ( arraySize < MemoryUsageInArray)
{return array + arrayIndex}
else{ return null}
}
The return pointer can access the array which is the buffer. I want to try to protect it from improper usage.
If you hand the address of a memory location to your client, they can do anything with it. Change your API and instead of returning an address, provide only the functionality they will need:
<your_type> readValue(size_t index)
{
// index validation etc.
.
.
return array[index];
}
Make the return value a const void *.
const void * returnReadPointer(size_t arraySize)
You can change the function prototype as below to protect your pointer from being changed.
const void * const returnReadPointer(size_t arraySize)

type casting the arguments of a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If the code is like the below,
void func(std::string str)
{
...
}
void main()
{
std::string p1 = "abcd";
char p2[SOME_LENGTH] = "abcd";
func(p1); // (1)
func(p2); // (2)
}
which way is efficient between (1) and (2)?
They are equally efficient/inefficient. Both involves copying the string and using the copy as the value of the argument 'str'. A better way would be declaring func as
void func(const std::string &str) {
}
This can avoid copying of the string.

C++ Pointer type cast from class name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to cast a Pointer in C++ from class name in String?
Psuedocode:
int * ptr = something;
myStruct ptrstruct = (ClassFromString("myStruct") ptr);
// The class/struct name is passed in as String
Thank you
I'm not a c++ guru but I have two ideas that may help with brainstorming:
May the use of the registry pattern as described here would be of help: Instantiate class from name?
Secondly, following the registry pattern idea you could crate a function for casting e.g. MyClass something = registry.cast("MyClass", ptr);
I am not sure but this must work
Only Void pointer or boost can help, if it happens
thing * p = something; // pointer to object
void * pv = p; // pointer to void
thing * p2 = static_cast<thing *>(pv); // pointer to the same object
Maybe same kind of situation is while returning values from Threads
Overall reflection is not possible in c++.
Its just brainstroming.

Why do I need to const_cast when releasing memory for a const data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
While releasing memory of a const data, why do I need to const_cast it. What will be the result If I ignore it.
You don't. This is completely valid, standard conform, code:
int const * const a = new int(42);
delete a;
It sounds like you are using std::free from <cstdlib> to do this instead, which signature is
void free(void *);
In this case the implicit conversion from int const * const to void * fails because you can only implicit const-cast, not cast const away. You want the first version in C++ anyway.