struct

Why can glBufferData buffer structs for UBO and SSBO when c++ does not specify struct layout

时光怂恿深爱的人放手 提交于 2020-01-10 04:15:07
问题 I was browsing this page on how to use Uniform Buffer Objects in openGL and saw the following struct: struct shader_data_t { float camera_position[4]; float light_position[4]; float light_diffuse[4]; } shader_data; being buffered into an openGL Uniform Buffer Object using GLuint ubo = 0; glGenBuffers(1, &ubo); glBindBuffer(GL_UNIFORM_BUFFER, ubo); glBufferData(GL_UNIFORM_BUFFER, sizeof(shader_data), &shader_data, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); and being used in the

Why would you ever use the same lifetimes for references in a struct?

核能气质少年 提交于 2020-01-10 03:46:11
问题 This question is similar to When is it useful to define multiple lifetimes in a struct?, but hopefully different enough. The answer to that question is helpful but focuses on advantages of one approach (using distinct lifetimes for references in struct) but not on drawbacks (if any). This question, like that, is looking for guidance on how to choose lifetimes when creating structs. Call this the tied together version because x and y are required to have the same lifetime: struct Foo<'a> { x:

Does a flexible array member increase sizeof a struct?

浪尽此生 提交于 2020-01-09 11:05:56
问题 I have the following kind of code: typedef struct { u32 count; u16 list[]; } message_t; ... message_t* msg = (message_t*)buffer; msg->count = 2; msg->list[0] = 123; msg->list[1] = 456; size_t total_size = sizeof(*msg) + sizeof(msg->list[0]) * msg->count; send_msg( msg, total_size ); Problematic line is the line with sizeofs. I am not sure is that correct way to count needed space. Does sizeof(*msg) contains already something about the list member? I can test it with my compiler, but does

Why is there no RAII in .NET?

若如初见. 提交于 2020-01-09 06:05:47
问题 Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up is moved from the class writer to its consumer (by means of try finally or .NET's using construct) seems to be markedly inferior. I see why in Java there is no support for RAII since all objects are located on the heap and the garbage collector inherently doesn't support deterministic destruction, but in .NET with the

C: pointer to struct in the struct definition

余生颓废 提交于 2020-01-08 12:23:25
问题 How can I have a pointer to the next struct in the definition of this struct: typedef struct A { int a; int b; A* next; } A; this is how I first wrote it but it does not work. 回答1: You can define the typedef and forward declare the struct first in one statement, and then define the struct in a subsequent definition. typedef struct A A; struct A { int a; int b; A* next; }; Edit: As others have mentioned, without the forward declaration the struct name is still valid inside the struct

Lower_bound matching wrong strings

心已入冬 提交于 2020-01-08 05:24:11
问题 Now I am completely confused. I am googling all day and still can't get why this code doesn't work. I have vector of structs and those structs have string property. When I want to add a new struct into vector , as first I have to check whether a struct with the same string property is already there. If it is, it won't be added. #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Try{ string name; Try( string name) : name ( name ) { } bool operator < ( const

Lower_bound matching wrong strings

﹥>﹥吖頭↗ 提交于 2020-01-08 05:23:18
问题 Now I am completely confused. I am googling all day and still can't get why this code doesn't work. I have vector of structs and those structs have string property. When I want to add a new struct into vector , as first I have to check whether a struct with the same string property is already there. If it is, it won't be added. #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Try{ string name; Try( string name) : name ( name ) { } bool operator < ( const

Create and communicate an “array of structs” using MPI Derived datatypes

依然范特西╮ 提交于 2020-01-07 05:07:28
问题 I am trying to program an MPI_Alltoallv using an MPI Derived datatype using MPI_Type_create_struct. I could not find any examples solving this particular problem. Most examples like this perform communication(Send/Recv) using a single struct member, whereas I am targeting an array of structs. Following is a simpler test code that attempts a MPI_Sendrecv operation on an array of structs created using DDT: #include <stdio.h> #include <stdlib.h> #include <mpi.h> #include <stddef.h> typedef

Matlab: Sum structures

别来无恙 提交于 2020-01-07 04:35:21
问题 I'm looking for a very simple method to sum structures which have identical substructure hierarchies. Note : This began with an attempt to sum bus structures in Simulink. MWE Assume I have 2 structures red and blue . Each structure has subfields a , b , and c . Each subfield has subfields x1 and x2 . This is drawn below: red. [a, b, c].[x1, x2] blue.[a, b, c].[x1, x2] Does a function exist such that a mathematical operator may be applied to each parallel element of red and blue ? Something

Set of structs in Go

夙愿已清 提交于 2020-01-07 02:36:08
问题 If I have a number of structs that I want to store: type Stuff struct { a string b string } I can do it with a slice, but it seems like it would use less memory to use a proper set structure. Unfortunately Go doesn't have a set structure. Everyone recommends using map[Stuff]struct{} but that doesn't work because Stuff is a struct. Anyone have any good solutions? Ideally without having to download a library. 回答1: Usually set and map data structures require more memory than storing a list of