0

Okay, so, I'm trying to do something like a "Compound Literal" AFAIK, I remember doing this on C while programming for embedded devices, but I'd to move to C++. I know that C++ is not C, I thought they were compatible.

GaussBlurParams *GBPs= &(GaussBlurParams) {Size(5,5), (int)3, (int)3, (int)0};

But compiler complains with: taking address of temporary [-fpermissive] error. I though on using a 'new' constructor but It gets worse.

Here's my Definition of GaussBlurParams

typedef struct gaussBlurParams{
    cv::Size ksize;
    int stdX;
    int stdY;
    int borderType;
} GaussBlurParams;

How can I overcome this problem?

Best Regards!

EDIT: Okay, This is what I'm doing:

void ProcessFrames(Mat In, Mat Out, GaussBlurParams GBPs){
    GaussianBlur(In, Out, GBPs.ksize, GBPs.stdX, GBPs.stdY);
}

createTrackbar("Kernel Size Height", "Output Stream Viewer", &GBPs->ksize.height, 10, NULL);
createTrackbar("Kernel Size Width", "Output Stream Viewer", &GBPs->ksize.width, 10, NULL);
createTrackbar("STD X Sigma", "Output Stream Viewer", &GBPs->stdX, 10, NULL);
createTrackbar("STD Y Sigma", "Output Stream Viewer", &GBPs->stdX, 10, NULL);
ProcessFrames(*Frame, *OutputFrame, *GBPs);

EDIT: Here's my Code on Github if you want to take a look! (Commit)

1
  • Can you post some more code what you actually want to do and how you want to use your GBP struct? Especially, how long does it need to stay valid? Commented Mar 20, 2014 at 2:52

1 Answer 1

1

Firstly, there are no compound literals in C++.

Secondly, compound literal declared in local scope creates an automatic object, which gets automatically destroyed once the containing block ends. Are you sure you need a pointer to such a short-lived object? (I'm not saying that this is necessarily wrong, but it might easily be.)

Anyway, in C++ you are not allowed to create pointers to temporary objects. (At least not the way you do it - with the built-in unary & operator.) Just declare a named object and make your pointer point to that named object. Forget about temporaries and compound literals.

GaussBlurParams GBP = { cv::Size(5,5), 3, 3, 0 };
GaussBlurParams *GBPs = &GBP;

Again, make sure the lifetime of that named object is as long as the lifetime of the pointer value, or you will end up with invalid dangling pointer.

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

5 Comments

If you do it that way, you must be careful that you don't store or return GBPs, since the object it points to (GBP) will be destroyed at the end of the function. I'd strive to avoid the explicit pointer entirely, and just call do_gauss_blur(&GBP) directly.
@Christian Aichinger: That's what I stated repeatedly in my answer. Also, while I don't advocate creating a completely unnecessary pointer, I don't see how do_gauss_blur(&GBP) is safer, assuming the pointer and the object are declared next to each other (and thus have the same lifetime). The danger lies in what do_gauss_blur does with the pointer. If it attempts to store it, then we have a problem either way.
I can't use cv::GaussianBlur Function directly, in fact, I'm using some trackbar to modify this parameters on Run Time
@AndreyT: I didn't mean to criticize your answer, I wrote the comment before your final edits. We completely agree on the facts, and I wanted to make sure OP understands that it's easy to shoot yourself in the foot with an explicit pointer to a local variable.
Given the name of the struct (GaussBlurParams), IMHO he's using a Parameter Block pattern, in which the parameter block is passed to a one or more functions to make the code a bit more legible but also to drastically cut the ABI overhead. In order for this to work, the called function cannot store a pointer to the parameter block, and must be done with it upon return, so the parameter block could well have only the lifetime of the function call. IMHO this pattern is better implemented by making the parameter block into a class and the function into a method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.