What is the lifetime of a compound literal passed as a parameter?


cresol

Compile with clang without warning.

typedef struct {
  int option;
  int value;
} someType;

someType *init(someType *ptr) {
  *ptr = (someType) {
    .option = ptr->option | ANOTHEROPT,
    .value = 1
  };

  return ptr;
}

int main()
{
  someType *typePtr = init( &(someType) {
    .option = SOMEOPT
  });
  // do something else with typePtr
}
  1. Is this even valid C?

  2. If so: what is the lifetime of a compound literal?

Yu Hao

Valid C in C99 or later.

C99 §6.5.2.5 Compound literals

The value of a compound literal is the value of the unnamed object initialized by the initializer list. If the compound literal appears outside the function body, the object has static storage duration; otherwise, the object has static storage duration. Otherwise, it has the automatic storage duration associated with the enclosing block.

In your example, the compound literal has automatic storage, which means its lifetime is within its block, i.e. within the function main()it 's in.

Recommended reading @Shafik Yaghmour:

  1. New C: Compound Literals
  2. GCC Manual: 6.25 Compound Literals

Related


Compound literal pointer passed as parameter in C

Function Is pass-by-value cast pointer valid in C? If not why does this work? #include <stdio.h> typedef struct { int size; char* str; } MY_STRUCT; void print_my_struct_prt(MY_STRUCT* mstrct) { printf("%s%d%s%s\n", "size: ", mstrct->size, " c

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

compound literal

I am Islam I ran into a situation in Go and I couldn't find any solution. The problem starts with the following code: graph := chart.BarChart{ Title: "Remote#1 Bar Chart", Background: chart.Style{ Padding: chart.Box{ Top: 40,

Get the value of literal type passed as type parameter

some names I'm trying to write a macro that returns a value of literal type passed as a type parameter, which fails to compile if the parameter is not a literal type. Here are some examples: def literalValue[SC <: String] = //... literalValue["AB"] // returns

What does it mean that a trait has a lifetime parameter?

Goertzenator I understand how lifetime parameters apply to functions and structs, but what does it mean for traits to have lifetime parameters ? Is it a shortcut to introduce lifecycle parameters into its methods, or something else? Shepmaster If your trait ha

What does it mean that a trait has a lifetime parameter?

Goertzenator I understand how lifetime parameters apply to functions and structs, but what does it mean for traits to have lifetime parameters ? Is it a shortcut to introduce lifecycle parameters into its methods, or something else? Shepmaster If your trait ha

What is the lifetime of a while loop parameter using a mutex?

Ricky I'm working on Rust's rustling exercise and came up with a solution that threads1.rslooks like this : struct JobStatus { jobs_completed: u32, } fn main() { let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let status_shared

What does it mean that a trait has a lifetime parameter?

Goertzenator I understand how lifetime parameters apply to functions and structs, but what does it mean for traits to have lifetime parameters ? Is it a shortcut to introduce lifecycle parameters into its methods, or something else? Shepmaster If your trait ha

What is the lifetime of a while loop parameter using a mutex?

Ricky I'm working on Rust's rustling exercise and came up with a solution that threads1.rslooks like this : struct JobStatus { jobs_completed: u32, } fn main() { let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let status_shared

What does it mean that a trait has a lifetime parameter?

Goertzenator I understand how lifetime parameters apply to functions and structs, but what does it mean for traits to have lifetime parameters ? Is it a shortcut to introduce lifecycle parameters into its methods, or something else? Shepmaster If your trait ha