0

I have a char* data that I casted in order to access it like this

arr[2][3]

I proceeded that way :

char (*arr)[size] = (char (*)[size])data;

My problem is how can I pass "arr" as an argument to a function ?

void func(??? arr)
{
   ...
}

void test(char *data, int size)
{
   char (*arr)[size] = (char (*)[size])data;
   func(arr);
}

I don't know the value of "size" at compile time.

Also, I know that I could just access my value like this : data[2 * size + 3] but I need to avoid multiplications for speed constraints.

6
  • Just void func(char **arr) Commented Jun 16, 2015 at 10:25
  • 2
    VLA such as char (*arr)[size] is not standard C++. Anyway, you cannot cast data into such type, in the same way you cannot cast apple into orange. It doesn't make sense. Commented Jun 16, 2015 at 10:34
  • 7
    "but I need to avoid multiplications for speed constraint" - what do you think the compiler's going to have to do with the indices in arr[x][y] if you do give it knowledge of the array dimensions? Commented Jun 16, 2015 at 10:37
  • @101010 It would be so easy ;) but it doesn't compile Commented Jun 16, 2015 at 11:08
  • 2
    @Olivier: VLA is not allowed in ANY version of C++. Commented Jun 16, 2015 at 11:12

2 Answers 2

1

Also, I know that I could just access my value like this : data[2 * size + 3] but I need to avoid multiplications for speed constraints.

This cannot be avoided in the way you proposed. If you access data like x[2][3], what do you expect the compiler to do? It will translate the indexing it into the equivalent of 2 * size + 3. If this is too slow for your problem, you are out of luck and need to find another way to gain time. The proposed solution which you cannot make work is not a solution, so it's pointless to even try it.

Perhaps you can change your code to use a different access pattern, one without so many complicated jumps that require all those multiplications? Linear memory accesses tend to be the best sort, because they take advantage of memory hierarchy; maybe you can just take advantage of that? It's hard to say what you can change without even know what the code does, though.

Sign up to request clarification or add additional context in comments.

1 Comment

I decompiled to compare, and yes you are right, the compiler does multiply. So I got back to the multiplication version, but it still is slower. Then I suppose the compiler optimizes something somewhere anyway. Thank you for your help
1

I have a char* data that I casted in order to access it like this

And that's the source of your problems. It's a contiguous memory block. Write a view facade if you need a different interface, or...

My problem is how can I pass "arr" as an argument to a function ?

The easiest way is to avoid using (terrible) low-level primitives left in the language for C compatibility.

If you don't know the size at compilation time, use an std::vector. If you do know, use std::array.

but I need to avoid multiplications for speed constraints.

Unless you have benchmark data to back that up, I am dismissing that requirement as a premature optimization.

10 Comments

This is again speed constraints that makes me code like C. I'm not looking for the easiest way. I'm doing real time image processing and I need to access tens of millions data in a few milliseconds. Using std:vector considerably slows down the program. Avoiding the multiplications the way I exposed makes me save a lot of time on the whole process. I just need to split my program into different functions because this is unreadable.
Using std:vector considerably slows down the program. - citation needed. Benchmark results. Code that used std::vector. Everything.
@Olivier $10 says you forgot to call reserve.
@Olivier Sounds like you are misusing something. Is this a debug build? Are you passing the vector by value? Did you reserve()? You need to provide a decent explanation for why std::vector considerably slows down the program. Fact is, std::vector is quite good in the vast majority of scenarios, so if it considerably slows down the program, that means your program has some very unusual characteristics. Knowing those very unusual characteristics is important for anyone trying to help you find a solution that avoids the same "mistakes" that std::vector supposedly does to make the program slow.
My char* data is returned by an external library, so this type cannot be changed. The length of the array is 1280 x 1024 x 4 = 5MBytes. To use std::vector, I need to reallocate the same large amount of memory, which is too much time consuming (about 12ms on my device) whereas the cast is immediate.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.