简体   繁体   English

如何正确地将const char数组分配给结构?

[英]How do I correctly assign a const char array to a struct?

I'm having problems with const char array being stored to a struct, then when calling the value from a struct I don't always get the expected value. 我遇到了将const char数组存储到结构中的问题,然后在从结构中调用该值时,我并不总是得到预期的值。

Below is the code: 以下是代码:

typedef struct
{
  char *update_type;
  char *transaction;
} TickType;

In a thread I have: 在我有一个主题:

const char tx_types[] = "INV/ADD/MOD/DEL/RPL";
const char upd_types[] = "INS/OVR/MOV/DEL";

tick->transaction = &tx_types[4*upd.xpbu_transaction_type];
tick->update_type = &upd_types[4*upd.xpbu_update_type];

This upd.xpbu_transaction_type and this upd.xpbu_update_type return ints (0-4) and (0-3) respectively. upd.xpbu_transaction_type和此upd.xpbu_update_type分别返回ints(0-4)和(0-3)。 In another thread we have printing to file: 在另一个线程中,我们打印到文件:

fprintf(out, "%3.3s/%3.3s:\n",tick->transaction, tick->update_type);
fflush(out);

The problem is when checking out the output file I see the following: 问题是签出输出文件时我看到以下内容:

+MOD/DEL:
+   / Â +:
+MOD/DEL:
+MOD/   :
    /@Ea:
    /<90>Ea:
    /Ã Ea:
    /0Fa:
    /   :

So as you can see it is only right sometimes. 所以你可以看到它有时是正确的。

I'm sure my mistake is in the struct assignment. 我确定我的错误在于结构分配。 Unfortunately I can not offer a better look into the code due to it being proprietary software. 不幸的是,由于它是专有软件,我无法更好地了解代码。

First of all, is this tick shared among threads? 首先,这个tick是在线程之间共享的吗? If so, then you have to protect the assignment to its members in a critical section, otherwise there might be a chance when the variable is being printed, another thread is updating it, causing garbage while printing. 如果是这样,那么你必须在一个关键部分保护其成员的赋值,否则可能有可能在打印变量时,另一个线程正在更新它,导致打印时出现乱码。 I'm not sure about the thread safety of fprintf() and fflush() , you should find it out too because it could be another factor that affect. 我不确定fprintf()fflush()的线程安全性,你也应该发现它,因为它可能是另一个影响因素。

I suspect the problem is where you put the definitions of your arrays. 我怀疑问题出在你放置数组定义的地方。

You should either put them in global address space or declare them static somewhere in order to be able to share the address to the strings between the tasks. 您应该将它们放在全局地址空间中,或者将它们声明为静态,以便能够将地址共享到任务之间的字符串。

Alternatively as BlackBear suggested allocate memory and copy the substring in there. 另外,当BlackBear建议分配内存并复制子字符串。

You have to malloc both update_type and transaction then using strcpy and copy the important part. 你必须malloc update_type和transaction然后使用strcpy并复制重要的部分。 You also have to edit a bit your strings: "INV\\x00ADD\\x00MOD\\x00DEL\\x00RPL" and "INS\\x00OVR\\x00MOV\\x00DEL" 您还需要编辑一下您的字符串:“INV \\ x00ADD \\ x00MOD \\ x00DEL \\ x00RPL”和“INS \\ x00OVR \\ x00MOV \\ x00DEL”

Your code doesn't check that upd.xpbu_transaction_type or upd.xpbu_update_type is within the proper ranges. 您的代码不会检查upd.xpbu_transaction_type或upd.xpbu_update_type是否在适当的范围内。

Also you need to use a Mutex or something for thread safety. 此外,您还需要使用Mutex或其他东西来保证线程安全。

Also it is not sufficiently clear which memory region these strings are actually stored in. Different environments have different rules regarding where const strings are going to be located. 此外,还不清楚这些字符串实际存储在哪个存储区域中。不同的环境对于const字符串的位置有不同的规则。 Make sure it is in global memory that is always accessible by both threads of execution. 确保它位于全局内存中,并且两个执行线程始终可以访问它。 The easiest way to ensure this is to define the strings as const outside any function. 确保这一点的最简单方法是将字符串定义为任何函数之外的const。 If it must be in a function it has to be declared static const. 如果它必须在函数中,则必须将其声明为static const。

Depending upon your environment something as simple as this would suffice: 根据您的环境,这样简单就足够了:

Thread A: 线程A:

/* validate upd values, etc*/
switch (upd.xpbu_transaction_type)
{
...
default:
  xpbu_tt = 0;
}

...
taskLock();
tick->transaction = &tx_types[4*xpbu_tt];
tick->update_type = &upd_types[4*xpbu_ut];
taskUnlock();

Thread B: 线程B:

While (1)
{
...
taskLock();
t = tick->transaction;
u = tick->update_type;
taskUnlock();
fprintf(out, "%3.3s/%3.3s:\n",t,u);
fflush(out);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM