how cloud be support dynamically template for fixed buffers during compile time? #1920
Replies: 11 comments
-
Hi |
Beta Was this translation helpful? Give feedback.
-
I believe @sgf is asking for dynamically variable-sized structs. i.e. he wants to allocate the memory for a struct (including a variable length part) all in one span, and then use it accordingly. |
Beta Was this translation helpful? Give feedback.
-
If I understood the proposal correctly, @sgf does want a kind of integer-generics/templates in C# -- just like in C++. public readonly strcut Vector<int D>
{
private readonly float[] _coefficients = new float[D];
public int Dimension => D;
// ....
}
public readonly strcut Matrix<int N, int M>
{
// ....
public (int Width, int Height) Dimensions => (N, M);
public static Matrix<U, W> operator* (Matrix<U, V> m1, Matrix<V, W> m2) => ....;
} Personally, I like integer generic parameters, however I think that we should be very careful on exactly how it is implemented and where the limitations currently reside... |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt yes,right.english is not my first language.im sorry about my english. dynamically variable-sized structs base templates in compile-time. that means,we can use sizeof() for it. |
Beta Was this translation helpful? Give feedback.
-
@sgf |
Beta Was this translation helpful? Give feedback.
-
@ygc369
I don't know what you're talking about. What does it mean when you create a struct that specifies its internal length. My main hope here is that there is a mechanism to hold variable-length objects such as strings. But I can limit the maximum length, but also can save the actual length and encoding and other meta-information. 不知道你说的创建时指定其内部长度的结构体是什么意思。 我之所以提出这个问题是因为我想利用内存布局做一个 同时兼容C++和C# 内存布局的 rpc系统。 |
Beta Was this translation helpful? Give feedback.
-
@sgf struct dynamic_string
{
int length;
char content[0];
};
void main()
{
int len, i;
struct dynamic_string *ds;
scanf("%d", &len);
ds = (struct dynamic_string *)malloc(sizeof(struct dynamic_string)+sizeof(char)*len);
ds->length = len;
for(i=0; i<len; i++) ds->content[i]='#';
......
} 如果C#里也有类似的机制就好了。 |
Beta Was this translation helpful? Give feedback.
-
@ygc369 但是你提到的这个 和C#中的string的并无区别 😂。 我是想解决 因为C++ 没有反射导致的序列化实现复杂的问题。 |
Beta Was this translation helpful? Give feedback.
-
@sgf class A
{
int length;
int data[?];
};
A a1 = new(10) A(); //data数组的长度是10
A a2 = new(20) A(); //data数组的长度是20
int n = Console.Read();
A a3 = new(n) A(); //data数组的长度是n |
Beta Was this translation helpful? Give feedback.
-
I have absolutely no idea what your intentions are. What you want, the above test program can be run. I don't know what the difference is with what you want. 我完全无法理解你的意图是什么?你要什么,上述测试程序可以运行。我不清楚和你要的有何区别。 另外 你的例子中 a1,a2,a3 是三种不同的变量,即3个对象,即使可变又有何意义?本身就是3个不同的对象了。
|
Beta Was this translation helpful? Give feedback.
-
You can do this in C# today: [StructLayout(LayoutKind.Sequential, Size = 6)]
struct InlineString
{
public int length;
public char fstChar;
}
unsafe
{
byte[] bytes = new byte[sizeof(InlineString) + sizeof(char) * 4];
fixed (byte* b = bytes)
{
InlineString* str = (InlineString*)b;
str->length = 5;
for (int i = 0; i < 5; i++) (&str->fstChar)[i] = '#';
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
example:
where i hope i can define I want to define a fixed-size string type like fllows:
just look at the [int maxLen]
its a dynamically variable-sized structs (the sizeof(struct[?]) dynamically during compile time)
then i cound be define some fields of User like:
ye,as u see,the FixedString is still could be when Compile has a fixed size.
we can use sizeof(User) and got size:
Size Of User=sizeof(FixedString[30])+sizeof(FixedString[50])=
sizeof(byte)+sizeof(fixed char Data[30])+
sizeof(byte)+sizeof(fixed char Data[50])=(4+1+60)+(4+1+100)=170
now we can get the Length and charset and read data from FixedString,and we dont need search the '\0'.from the old buffer.
What are the benefits of doing so:
We can save the strings of a fixed size structure.
then we can use pointer directly read and write it like:
or
or
or
Beta Was this translation helpful? Give feedback.
All reactions