user2384250
user2384250

Reputation: 558

Lifetime and memory setup of this struct

given a struct in the form ( declared before the main() or after )

struct
{
    ...
} bar;

I don't know how to manage this and how to treat this kind of object.

In general I would like a 360° answer about this but I also have few questions:

For the last one I have prepared a small snippet here, that doesn't works but shows what I'm trying to achieve.

Upvotes: 0

Views: 113

Answers (5)

Mike Seymour
Mike Seymour

Reputation: 254501

is this equivalent to declaring a static struct ?

No, you'd use the static keyword for that, as for any other static variable. It's declaring a global variable, i.e. with static storage duration and external linkage, equivalent to:

struct Bar {...};
Bar bar;

it's legal and safe in C++11 ?

Yes.

how to access bar by reference ?

Since the type has no name, it has to be inferred, either by template argument deduction or, in C++11:

auto & ref = bar;

template <typename Bar> void bar_by_ref(Bar & bar) { 
    /* do something with bar */ 
}

Alternatively, in C++11, the type can be extracted by decltype:

decltype(bar) & ref = bar;

Upvotes: 0

ForEveR
ForEveR

Reputation: 55887

It's global object of unnamed struct. You cannot use this type as argument to function (since you doesn't know real type of the object). You can use decltype for this, but it doesn't needed since it's global object. It's legal and safe.

#include <iostream>

struct
{
    double k1 =  0.123;
} bar;

int foo(decltype(bar)& a)
{
    return a.k1-1;
};

int main()
{
    std::cout << foo(bar) << std::endl;
    return(0);
}

example

Really, since you have only one global object of this struct - you simply can use

#include <iostream>

struct
{
    double k1 =  0.123;
} bar;

int foo()
{
    return bar.k1-1;
};

int main()
{
    std::cout << foo() << std::endl;
    return(0);
}

Upvotes: 3

rahul.deshmukhpatil
rahul.deshmukhpatil

Reputation: 997

1> bar is only single instance of its type. You can not have another instance of same type as bar. Its type is unnamed type struct, just like anonymous union. It is legal to have one such. It is more like global static(but not it is not static) just because you cannot refer it by extern keyword in other file.Any hack you have to refer it in other file ...?

3> It is legal to have it.

4> You can not declare reference to variable whose type you do not know. .Although u can access/modify same object anywhere in the file, so no need of reference. References are used to remove * and -> from code, mainly, otherwise what references do, is all possible with pointer.

Upvotes: 0

wallyk
wallyk

Reputation: 57774

No. static is a storage attribute. A struct is like any other data type: it can be static, volatile, auto, extern, and others defined by some implementations.

It is legal and safe.

Reference such a structure like this:

struct {
     int   field1, field2;
     double  yada, yada2, yada3;
} bar1, bar2;

bar1.yada3 = 3.14159265358979;   // assign a field in the struct
bar2 = bar1;   // copy contents of bar1 to bar2

Upvotes: 0

Spook
Spook

Reputation: 25927

This is simply a global variable. You can access it from any function or method in this compilation unit. The correct way of declaring it (in regard to your snippet) would be:

struct foo
{
    double k = 1.23;
};

foo bar;

Now you can simply pass it by reference like this:

void f(foo & b)
{
    b.k = 34.5;
}

In your snippet, bar is a variable of type being an unnamed struct:

struct <unnamed>
{
    double k = 1.23;
} bar;

Since you did not give any name to your type, you're unable to pass it as a parameter to functions in the classic way. C++11 allows you to do so using the decltype directive:

struct
{
    double k = 1.23;
} bar;

void f(decltype(bar) & b)
{
    b.k = 3.45;
}

As for your questions:

  • It is not the same as a static struct. Static variables behave in a little different way. Static classes (= structs) too.

  • It is legal and safe to use global variables in C++11, but it is strongly advised not to use them, because they may easily break program's logic or architecture. Instead, one should use static classes or singletons.

Upvotes: 0

Related Questions