0

I looked at this excellent answer but can't figure out how to apply it to this snipped:

//this is in the .hpp file
std::atomic<int> size = 10;
std::recursive_mutex *locks[2];

//in some function of the class
//it's important that the 2nd array dimension is dynamic
the_lock[0] = new std::recursive_mutex[size]; 
the_lock[1] = new std::recursive_mutex[size];
std::recursive_mutex (*locks_2)[2][size] = &locks;

The assignment gives me

error: cannot convert ‘std::recursive_mutex* (*)[2]’ to ‘std::recursive_mutex (*)
[2][(((sizetype)(((ssizetype)((**here be long type information, since I'm using
templates a lot**, long unsigned int, std::less<long unsigned int>          
>::size.std::atomic<long unsigned 
int>::<anonymous>.std::__atomic_base<_IntTp>::operator 
std::__atomic_base<_IntTp>::__int_type<long unsigned int>()) + -1)) + 1)]’ in 
initialization

How can I obtain a pointer to 'locks'?

8
  • 1
    Arrrr... please don't call your mutex "lock". A mutex is a mutex, and a lock is a lock. Commented Jul 3, 2013 at 22:10
  • Sorry for that. I'm trying to implement Java-style pseudo code from a book and am keeping the names to avoid confusion Commented Jul 3, 2013 at 22:13
  • OK -- I think that's saying more about Java than about you :-) (Or at least about the book.) Commented Jul 3, 2013 at 22:13
  • Any reason to allocate each object individually? Commented Jul 3, 2013 at 22:17
  • Why should a Java based book call it's locks "mutex"? Commented Jul 3, 2013 at 22:18

3 Answers 3

3

The error message is actually giving away the solution for free:

std::recursive_mutex * (*locks_2)[2] = &locks;
Sign up to request clarification or add additional context in comments.

3 Comments

This does NOT WORK! ' error: cannot convert ‘std::recursive_mutex* ()[2]’ to ‘std::recursive_mutex ()[2]’ in initialization'
@mort: Sorry, my bad, forgot a *. Fixed.
I accepted this answer because it helped figure out the correct type. However, the other answers are correct as well.
2

One can use the fact that this sort of thing has been officially declared to be ridiculously hard to get right for no apparent reason that the compiler cannot solve for you. If your compiler supports C++ 2011 that is. Use:

auto my_ptr = &locks; // point to locks, let the compiler worry about types

Requires that you compile your code as C++ 2011 though. (-std=c++11 for GCC).

1 Comment

Not a problem, since I already use '-std=c++11' (with clang though)
1

locks is just an array of pointers. So you could use a pointer pointer to point to it.

std::recursive_mutex **locks_2 = locks;

2 Comments

Strictly speaking, that's not a "pointer to locks". At best that's a "pointer to locks[0].
Works and is sufficient for my requirements.

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.