标签: struct

结构成员访问的性能

我有一个高性能的关键代码部分,需要访问结构的多个字段。将结构本身分配给变量,或使用指针获取结构的成员是否更快,例如

struct A* = A_arr + n;
int a = A->t - A->s;
Run Code Online (Sandbox Code Playgroud)

对比

struct A = A_arr[n];
int a = A.t - A.s;
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

c optimization performance struct

-1
推荐指数
1
解决办法
254
查看次数

C# 布尔值比较错误?

这怎么可能?这两个值都在内存中,并且在设置了这两个值后在断点处暂停执行。我很高兴找到一个 if 语句,该语句检查当值实际上相同时要执行的两个布尔值是否不同。因此,我在同一断点处使用“立即窗口”进行了更深入的研究。

在此处输入图片说明

出于好奇,我停止了程序,并且(就在我注意到这种不一致的原始调试点)我插入了几行代码:

var one = true;
var two = true;
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我破坏了 C# 还是什么?同一方法调用范围内的两对布尔值(我仔细检查了它们的 .GetType() 返回布尔值)如何使用相等运算符不一致?

尽管这无关紧要,但第一对(其中 double equals 方法返回错误的结果)是在可能会或可能不会更改属性值的方法调用之前和之后使用 PropertyInfo.GetValue(object target) 收集的在目标上。我使用了两次 GetValue 以确保在“做某事”之前更改了属性的值。

我在多次执行程序时得到相同的结果,所以我不认为这是执行环境的侥幸。

c# struct equality

-1
推荐指数
1
解决办法
669
查看次数

c - 如何为c中的结构数组malloc

我一直在寻找一种方法来自己做这件事,但我还没有找到与我的情况完全匹配的情况,而且我的经验不足,无法从类似情况中得出该怎么做。所以我希望在我的具体情况下得到一些帮助。

我有一个结构体,我需要创建 3 个它们的数组。但是当我使用 [] 分配内存时,内存不足。所以我想我需要使用malloc;但我不知道该怎么做。这是我的代码:

struct key {

char symbol[10];
int quantity;
char GroupID[10];

};  
Run Code Online (Sandbox Code Playgroud)

然后在主要我有:

struct key PrevKeys= malloc(345000*sizeof(struct key));
struct key ActivityKeys= malloc(345000*sizeof(struct key));
struct key CurKeys= malloc(345000*sizeof(struct key));
Run Code Online (Sandbox Code Playgroud)

但是我不断从编译器中收到“无效的初始值设定项”错误。

早些时候我试过这个,它编译得很好,但是当我运行它时给了我一个段错误(我假设是因为堆栈中没有足够的内存):

 struct key PrevKeys[345000];
 struct key ActivityKeys[345000];
 struct key Curkeys[345000];
Run Code Online (Sandbox Code Playgroud)

关于如何分配这 3 个结构数组的任何想法都将非常非常感谢。

c arrays malloc struct

-1
推荐指数
1
解决办法
6232
查看次数

如何解决“参数类型不完整”错误?

我是新手,我需要帮助调试我的代码。当我编译它时'形式参数1的类型不完整'和形式参数2的类型不完整'错误出现在

 printf("Age is %d years.\n", calc_age(birth, current));
Run Code Online (Sandbox Code Playgroud)

而'参数 1 ('出生') 的类型不完整' 和'参数 2 ('当前') 的类型不完整' 错误出现在

int calc_age (struct date birth, struct date current) {
Run Code Online (Sandbox Code Playgroud)

感谢帮助,谢谢!

#include <stdio.h>
int calc_age (struct date birth, struct date current);


int main(void)
{

    struct date {
        int month[1];
        int day[1];
        int year[1];

};


    struct date birth, current;
    char c;

    printf("Input the birthdate (MM/DD/YY): ");
    scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
    printf("Input date to calculate (MM/DD/YY): ");
    scanf("%d %c %d %c …
Run Code Online (Sandbox Code Playgroud)

c arrays struct function

-1
推荐指数
1
解决办法
1万
查看次数

std::map 的自定义比较器不起作用

我在写一些C++代码时遇到了以下现象:

我有一张看起来像这样的地图:

std::map<test_struct_t*, unsigned int, cmp_by_value> testmap;
Run Code Online (Sandbox Code Playgroud)

该映射位于我的程序中,结构定义为:

struct test_struct_t {
  int x; int y; int val;

  bool operator<(const test_struct_t &o) const {
      return x < o.x || y < o.y || val < o.val;
  }
  test_struct_t(int a, int b, int c) : x(a), y(b), val(c) {}
};
Run Code Online (Sandbox Code Playgroud)

我写的自定义比较器是:

struct cmp_by_value {
  bool operator()(const test_struct_t *a, const test_struct_t *b) const 
  {
      return *a < *b;
  }
};
Run Code Online (Sandbox Code Playgroud)

现在,在我的主要方法中,我执行以下操作:

testmap.insert({new test_struct_t(0, 0, 2 ), 6});
testmap.insert({new test_struct_t(0, 1, 2 ), 6}); …
Run Code Online (Sandbox Code Playgroud)

c++ struct dictionary stdmap std

-1
推荐指数
1
解决办法
647
查看次数

在下列情况下,为什么结构地址在赋值后不等于

#include <stdio.h>


int main() {
    struct my_structure {
        char name[20];
        int number;
        int rank;
    };

    struct my_structure var = {"Stud", 35, 1};
    struct my_structure *ptr;
    ptr = &var;

    printf("%p ", &var);
    printf("\n");
    printf("%p ", &ptr);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码在c99下编译.ptr不等于var.为什么?


0x7ffeea577988


0x7ffeea577978

c struct pointers

-1
推荐指数
1
解决办法
60
查看次数

死锁与匿名互斥和结构

假设我有这两种结构:

type A struct {
    Mutex sync.Mutex
    i int
}

type B struct {
    A
    sync.Mutex
}
Run Code Online (Sandbox Code Playgroud)

现在,当我试图锁定B然后A我遇到了僵局:

var b B
b.Lock()
b.Mutex.Lock()
b.Mutex.Unlock()
b.Unlock()
Run Code Online (Sandbox Code Playgroud)

我想通了,这是与结构的互斥体的名字相关A,例如,有是,如果我将其命名为无僵局Mutexx,而不是Mutex.但我不知道为什么这很重要.有人可以解释一下这种行为吗?

https://play.golang.org/p/UVi_WLWeGmi

struct mutex deadlock embedding go

-1
推荐指数
1
解决办法
119
查看次数

Why am I getting segmentation fault while implementing linked list?

In this function, I get segmentation fault. I think it has something to do with memory allocation. What mistake am I making?

Now, if I initialize Node* a =NULL, i get my head pointer as NULL in the end.

struct Node {
    int data;
    struct Node* next;
    Node(int x) {
        data = x;
        next = NULL;
    }
};

Node* addTwoLists(Node* first, Node* second) {
    // Code here
    Node *a;
    Node *head = a;
    int bor = 0;
    while(first->next && second->next) …
Run Code Online (Sandbox Code Playgroud)

c++ struct initialization linked-list singly-linked-list

-1
推荐指数
1
解决办法
83
查看次数

C: How do I correctly access an Array element of a Struct passed by reference?

I have this code:

typedef struct _structVar{
   int myArray[10];
} structVar;

structVar MyStruct;

Run Code Online (Sandbox Code Playgroud)

Then I'm passing the struct by reference to a function:

myFunct(&MyStruct);
Run Code Online (Sandbox Code Playgroud)

How I access array elements inside MyFunct?

void myFunct(structVar *iStruct){

   for(char i=0; i<10; i++)
      (*iStruct)->myArray[i] = i; //Fix Here
}
Run Code Online (Sandbox Code Playgroud)

Thanks for the help.

c arrays struct pointers member-access

-1
推荐指数
1
解决办法
60
查看次数

Go中[] Foo(nil)和[] Foo {}之间的区别

我是新去,想知道之间的区别[]Foo(nil)[]Foo{}

(我在测试中使用此函数,我想在此指定函数错误时应返回nil, err。go linter在使用nil或时会抱怨[]Foo{},但在使用时会起作用[]Foo(nil)。)

我尝试过的

我在Go文档和SO上进行了查看,发现Struct的内容有关,Foo{}但没有[]Foo(nil)

当我使用时[]Foo{},测试失败输出:

expected: []Foo{}
actual  : []Foo(nil)
Run Code Online (Sandbox Code Playgroud)

为FMT输出[]Foo(nil)[]Foo{}是相同的:

fmt.Println([]Foo(nil)) // []
fmt.Println([]Foo(){}) // []
fmt.Printf([]Foo(nil)) // []Foo
fmt.Printf([]Foo(){}) // []Foo
Run Code Online (Sandbox Code Playgroud)

我注意到,如果我只写Foo(nil)(不写[]),则短绒猫抱怨cannot convert nil to type Foo

因此,我唯一的猜测就是[]Foo(nil)调用某种类型的强制。谁能帮我吗?

arrays struct go

-1
推荐指数
1
解决办法
134
查看次数