struct

realloc(): invalid next size - realloc dynamic struct

南笙酒味 提交于 2020-01-25 00:19:06
问题 I started learning about struct in C. Today I found a problem, that I can't solve. I have this code: typedef struct fraze { char *mostSearch = NULL; // for string from user double freq; } s_FRAZE; int readFraze( ) { int i = 2, k; size_t len = 0; char c; s_FRAZE *s; s = (s_FRAZE *)malloc( i * sizeof( int )); k = 0; while( (c = getchar()) != '\n') { ungetc( c, stdin ); if( scanf( "%lf%c", &s[k].freq, &c) != 2 || c != ':' ) { return 1; } if( k + 1 >= i ) { i *= 2; printf("%d\n", i ); s = (s

How to populate array of structs, without using a line for code for each member?

好久不见. 提交于 2020-01-24 23:36:30
问题 I'm currently trying to find a better way to populate an array of structs. first off, this is the data going in to making my struct enum DataType { Uint8, Uint16, Byte, Int16, Int32, TypeSatInfo, TypeCell, ByteArray } enum ReadWriteState { ReadWrite, ReadOnly, WriteOnly } struct Parameter { public string ParamName; public short ParamId; public DataType Datatype; public ReadWriteState ReadWrite; } Then I have a class which should hold my populated array static class Parameters { public static

How to populate array of structs, without using a line for code for each member?

天大地大妈咪最大 提交于 2020-01-24 23:36:26
问题 I'm currently trying to find a better way to populate an array of structs. first off, this is the data going in to making my struct enum DataType { Uint8, Uint16, Byte, Int16, Int32, TypeSatInfo, TypeCell, ByteArray } enum ReadWriteState { ReadWrite, ReadOnly, WriteOnly } struct Parameter { public string ParamName; public short ParamId; public DataType Datatype; public ReadWriteState ReadWrite; } Then I have a class which should hold my populated array static class Parameters { public static

Complex trait requirements on struct

独自空忆成欢 提交于 2020-01-24 15:05:46
问题 I have a fairly complex trait set up and I'm having trouble lining the pieces up. Right now it looks roughly like this: /// Trait for models which can be gradient-optimized. pub trait Optimizable { type Data; type Target; // The contract // } /// Trait for optimization algorithms. pub trait OptimAlgorithm<M : Optimizable> { // The contract // } Now I want to be able to allow a struct implementing OptimAlgorithm to be a field in a struct implementing Optimizable . This would look something

How readily do unterminated char arrays in C++?

狂风中的少年 提交于 2020-01-24 10:14:08
问题 Sorry if this is solvable via Google -- I couldn't find anything. char foo[4] = "abcd"; is invalid in C++ (due to the need for '\0' terminator) but IIRC valid in C -- is that correct? I have a set of structs with a large number of "fixed length" character fields that are to be blank-padded and not '\0' terminated. I would like to be able to do the usual sort of struct initialization -- you know mystruct bar = {17, "abcd", 0x18, "widget ", ... But I can't do that in C++. One solution I guess

Error: In C, got the error “dereferencing pointer to incomplete type” in a struct pointer

痞子三分冷 提交于 2020-01-24 08:55:47
问题 Hello Everybody! I got the following error, while trying to test a code for the game Clever Frog: error: dereferencing pointer to incomplete type The 'full code' is at pastebin.com - here (won't expire). But I think that with the explanation below, anybody can understands. Note: I haven't implemented yet the function that will erase the allocated memory and other things. I have a struct defined in a 1.c file: #include "1.h" ... struct test { int a; }; ... I have a 1.h wicth have the typedef

Why struct assignment works with arrays in structs

元气小坏坏 提交于 2020-01-24 04:33:10
问题 I was attempting to explain to a co worker a concept and came to the realization I was incorrect in my understanding. How are structs with arrays embedded assignable? For example: typedef struct { uint8_t data[8]; } Test; ... Test test1; Test test2; ... some assignment to test1 test2 = test1; I know if data was of type pointer that we would need to implement a deep copy but I'm trying to understand fully the way this works. My though process is that as 'data' would normally be the pointer to

JSONEncoder with multiple structures

[亡魂溺海] 提交于 2020-01-24 01:09:07
问题 Hi I have multiple JSON Packets like I wrote below. { "data" : { "lng" : 36.159999999999997, "lat" : 50.359999999999999, "accuracy" : 5 }, "header" : { "type" : "loc" } } and this one { "data" : { "time" : 15646565455, "tz" : "+2", "system" : 5 }, "header" : { "type" : "syn" } } I have structure for my packets and it worked for one structure of packets. But how to use it for multiple packets structures. struct Packet : Codable { enum StructureType : String, Codable { case location = "loc"

C++ Find struct in list using a string item?

柔情痞子 提交于 2020-01-23 13:03:34
问题 I'm very new to c++ and I'm trying to figure how to find a struct inside a list using a string. I have a struct like this: struct entrada { string token; string lexema; string tipo; }; and a list: list<entrada> simbolos; Insert here some 'entrada' in 'simbolos' Let's say I want to search for a 'entrada' with a certain 'lexema', and cout the other strings. Is there a simple way to do this? Like a function or something. I did it using while/for, but it isn't how I want to do. 回答1: In accordance

TensorFlow Custom Allocator and Accessing Data from Tensor

江枫思渺然 提交于 2020-01-23 12:10:14
问题 In TensorFlow, you can create custom allocators for various reasons (I am doing it for new hardware). Due to the structure of the device, I need to use a struct of a few elements as my data pointer which the allocator returns as a void* . In the kernels that I am writing, I am given access to Tensors but I need t get the pointer struct that I wrote. Examining the classes, it seemed that I could get this struct by doing tensor_t.buf_->data() Tensor::buf_ TensorBuffer::data() The problem is