This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] CODECOMPOSER:结构内容分配给另一个结构

Guru**** 2554860 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/tools/code-composer-studio-group/ccs/f/code-composer-studio-forum/1006956/codecomposer-structure-content-assignment-to-another-structure

器件型号:CODECOMPOSER

您好!

我正在尝试将结构内容分配给另一个结构、但它给了我一种我不理解的行为。

从等效代码提取:

_________________________________________ "file1.c"_________________________

typedef 结构更小{

uint8_t a;

uint32_t b;

字符 c[8];

}小结构体;

typedef 结构、大于{

小结构*d;

小结构*e;

小结构*f;

} biggerStructure;

biggerStructure * mybiggerStructure1;

biggerStructure * mybiggerStructure2;

biggerStructure * mybiggerStructure3;

________________________________________________________________

_________________________________________ "file2.c"_________________________

#include "file1.c"

void 实例结构(void){

biggerStructure *g = calloc (10、sizeof (smallerStructure));

biggerStructure *h = calloc (10、sizeof (smallerStructure));

biggerStructure *i = calloc (10、sizeof (smallerStructure));

/*I want to work with 10 sigant structures inside the big structures*/

myBiggerStructure1 = g;

myBiggerStructure2 = h;

myBiggerStructure3 = i;

/*Instantitation 起作用,这意味着 file1.c myBiggerStructureX 中的全局变量与 g、h 或 i 共享相同的地址,以及它们内部较小结构的相同地址,但它们为我提供了一个全局句柄来访问它们。*/

void 函数(smallerStructure *测试){

*myBiggerStructure1->d =*测试;//错误。 不分配和 biggerStructure and d、e 和 f、它们会将其地址更改为0x00000000、0x00000005、0x00000400或其他不总是相同的值。

*(myBiggerStructure1->d)=*(test);//相同错误

memcpy (myBiggerStructure1->d、test、sizeof (* myBiggerStructure1);//相同错误

myBiggerStructure1->d->a =测试->a;

myBiggerStructure1->d->b = test->b;//等等... 单个字段分配:相同错误。

memcpy (&(myBiggerStructure1->d->a)、&(test->a)、sizeof (* myBiggerStructure1);//相同错误。

myBiggerStructure1->d = test;//works、但它会将 d 的地址更改为我不想使用的"test"地址。

void higherFunction (void){

smallerStructure * myTest = calloc (1、sizeof (smallerStructure));

char testArray[8]={'q'、'q'、'q'、'q'、'q'、'q'、'q'、'q'};

myTest->A = 0;

myTest->b = 1;

memcpy (myTest->c、testArray、sizeof (testArray));

函数(myTest);

________________________________________________________________

调用时:

函数(myBiggerStructure1->d);

在该函数内适当地更改分配、可以直接处理所考虑的结构、而不会出现任何问题。 我不明白为什么我无法处理不同结构的任务。

您能否解释一下编译器如何管理这些分配?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您的代码中存在多个问题。  现在、我将重点介绍一个错误。   

    这一行...

    [引用 userid="452057" URL"~/support/tools/code-composer-studio-group/ccs/f/code-composer-studio-forum/1006956/codecomposer-structure-content-assignment-to-another-structure ]biggerStructure *g = calloc (10、sizeof (smallerStructure));

    之后是...

    [引用 userid="452057" URL"~/support/tools/code-composer-studio-group/ccs/f/code-composer-studio-forum/1006956/codecomposer-structure-content-assignment-to-another-structure ]myBiggerStructure1 = g;[/quot]

    (笑声) 仅将 myBiggerStructure1->d 初始化为0。  它不会初始化 d 以指向内存中的有用地址,这样一个赋值就像...

    [引用 userid="452057" URL"~/support/tools/code-composer-studio-group/ccs/f/code-composer-studio-forum/1006956/codecomposer-structure-content-assignment-to-another-structure ]* myBiggerStructure1->d =*测试;[/quot]

    (笑声) 可以正常工作。  尝试将*test 的内容写入地址0。  我感到惊讶的是、没有发生某种故障。   

    一般而言... 您对指针和内存分配的理解需要提高。  这个论坛不是进行这种讨论的最佳场所。  我建议您从有关 C 语言编程的书籍、教程等开始。  或者、可能是一名更有经验的同事。   

    谢谢、此致、

    乔治