理解一段代码...... [英] Understand piece of code.....

查看:55
本文介绍了理解一段代码......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我理解下面代码中发生了什么。一个

简短的解释会很好.....


最好的问候

特里


struct mystruct {

unsigned char testchar;

unsigned short testshort;

unsigned char anothertestchar;

};


void testfunc(unsigned char * mymemlocation){


struct mystruct * thedata =(struct mystruct *)mymemlocation;


thedata-> testchar = 0x55;

thedata-> testshort = 0xFFFF;

thedata-> anothertestchar = 0x11;


}

Could anyone please help me understand what is going on in the code below. A
short explanation would be nice.....

Best Regards
Terry

struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};

void testfunc(unsigned char* mymemlocation) {

struct mystruct *thedata = (struct mystruct*) mymemlocation;

thedata->testchar = 0x55;
thedata->testshort = 0xFFFF;
thedata->anothertestchar = 0x11;

}

推荐答案

" Terry Andersen" < TE ** @ sea.com>在消息中写道

news:bg ********** @ news.net.uni-c.dk ...
"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};
您已经创建了一个结构,类似于
任意类型和相关变量的容器。

void testfunc(unsigned char * mymemlocation){

struct mystruct * thedata =(struct mystruct *)mymemlocation;
这是一个函数,它传递一个指针,用于存储你的

结构的内存位置。这是_nasty code_。永远不要假设unsigned char *等于

除了unsigned char之外的其他任何东西。该参数应为无效*

mymemlocation。最有可能的是,这个函数要求你提供你自己分配的指针来操纵堆上的结构。

thedata-> testchar = 0x55;
指针使用 - >或(* struct)。如果他们是指针,则访问成员。

否则他们使用句号''。''。这一行将testchar设置为字母

''U'',十六进制为0x55。 0x之前的值表示它是

十六进制。你也可以把它等于85.

thedata-> testshort = 0xFFFF;
这将testshort(结构中的unsigned short int)设置为65535.

thedata-> anothertestchar = 0x11;
这将另一个字符设置为17,这是一个不可打印的(不是
字母数字)数字。

}
struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
}; You have created a structure, which is something like a container for
arbitrary types and related variables.
void testfunc(unsigned char* mymemlocation) {

struct mystruct *thedata = (struct mystruct*) mymemlocation; This is a function that passes a pointer for a memory location to store your
struct. This is _nasty code_. Never assume that unsigned char* is equal to
anything else but unsigned char. The parameter should be void*
mymemlocation. Most likely, this function is requiring that you supply your
own allocated pointer for manipulation of the structure on the heap.
thedata->testchar = 0x55; Pointers use -> or (*struct). to access members if they are pointers.
Otherwise they use a period ''.''. This line here sets testchar to the letter
''U'', which is 0x55 in hexadecimal. 0x before a value indicates that it is
hexadecimal. You could have also made it equal to 85.
thedata->testshort = 0xFFFF; This sets testshort, the unsigned short int from the structure, to 65535.
thedata->anothertestchar = 0x11; This sets the other character to 17, which is an unprintable (not
alpha-numeric) digit.
}



这里应该有一个分号。


There should be a semicolon here.


" Greg P." < no@spam.sam>在消息中写道

news:oc ***************** @ newsread3.news.pas.earthl ink.net ...
"Greg P." <no@spam.sam> wrote in message
news:oc*****************@newsread3.news.pas.earthl ink.net...
Terry Andersen < TE ** @ sea.com>在消息中写道
新闻:bg ********** @ news.net.uni-c.dk ...
"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};您已经创建了一个结构,类似于任意类型和相关变量的容器。
struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
}; You have created a structure, which is something like a container for
arbitrary types and related variables.
void testfunc(unsigned char * mymemlocation){

struct mystruct * thedata =(struct mystruct *)mymemlocation;
void testfunc(unsigned char* mymemlocation) {

struct mystruct *thedata = (struct mystruct*) mymemlocation;


这是一个传递存储器位置指针的函数


This is a function that passes a pointer for a memory location to store



你的struct 。这是_nasty code_。永远不要假设unsigned char *等于
除了unsigned char之外的任何其他内容。该参数应无效*
mymemlocation。最有可能的是,这个函数要求你提供
你自己分配的指针来操纵堆上的结构。


your struct. This is _nasty code_. Never assume that unsigned char* is equal to
anything else but unsigned char. The parameter should be void*
mymemlocation. Most likely, this function is requiring that you supply your own allocated pointer for manipulation of the structure on the heap.

thedata-> testchar = 0x55;指针使用 - >或(* struct)。如果他们是指针,则访问成员。
否则他们使用句号''。''。这一行将testchar设置为
thedata->testchar = 0x55; Pointers use -> or (*struct). to access members if they are pointers.
Otherwise they use a period ''.''. This line here sets testchar to the



字母''U'',十六进制为0x55。 0x之前的值表示它是十六进制的。你也可以把它等于85.


letter ''U'', which is 0x55 in hexadecimal. 0x before a value indicates that it is
hexadecimal. You could have also made it equal to 85.

thedata-> testshort = 0xFFFF;
thedata->testshort = 0xFFFF;


这设置testshort,结构中的unsigned short int,至> 65535.


This sets testshort, the unsigned short int from the structure, to 65535.

thedata-> anothertestchar = 0x11;
thedata->anothertestchar = 0x11;


这将另一个字符设置为17,这是一个不可打印的(不是
alpha -numeric)数字。


This sets the other character to 17, which is an unprintable (not
alpha-numeric) digit.

}


这里应该有分号。


There should be a semicolon here.




非常感谢。这是否意味着testchar,testshort和anothertestchar

是从内存位置mymemlocation存储还是进一步?如果mymemlocation

将是这样的:


unsigned char TheBuffer [100];

//并且您使用以下命令调用testfunc:


testfunc(TheBuffer [50]);


/ *

然后TheBuffer会是这样的:

TheBuffer [50] = 0x55

TheBuffer [51] = 0xFF

TheBuffer [52] = 0xFF

TheBuffer [ 53] = 0x11

???

* /


最好的问候

特里



Thanks a lot. Would this mean that testchar, testshort and anothertestchar
are stored from memory location mymemlocation and further? If mymemlocation
would be like:

unsigned char TheBuffer[100];
//and you called the testfunc with:

testfunc(TheBuffer[50]);

/*
Would TheBuffer then look like:
TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11
???
*/

Best Regards
Terry


" Terry Andersen" < TE ** @ sea.com>在留言中写道

news:bg ********* @ news.net.uni-c.dk ...
"Terry Andersen" <te**@sea.com> wrote in message
news:bg*********@news.net.uni-c.dk...
非常感谢。这是否意味着testchar,testshort和anothertestchar
是从内存位置mymemlocation存储还是进一步?如果
mymemlocation就像:

unsigned char TheBuffer [100];
//你用testfunc调用了testfunc:

testfunc(TheBuffer [ 50]);
No.首先,你不会将TheBuffer传递给带有

下标的testfunc()。它会被传递为


testfunc(TheBuffer);


。数组的名称是指向第一个值的指针,你可以稍后迭代它。

然后TheBuffer看起来像:
TheBuffer [50] = 0x55
TheBuffer [51] = 0xFF
TheBuffer [52] = 0xFF
TheBuffer [53] = 0x11
Thanks a lot. Would this mean that testchar, testshort and anothertestchar
are stored from memory location mymemlocation and further? If mymemlocation would be like:

unsigned char TheBuffer[100];
//and you called the testfunc with:

testfunc(TheBuffer[50]); No. First of all you would not pass TheBuffer to testfunc() with the
subscript on it. It would be passed as

testfunc(TheBuffer);

instead. The name of an array is a pointer to the first value, which you
could later iterate over.
Would TheBuffer then look like:
TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11



编号我认为你的意思:缓冲区[index]是否将

函数的值保存为数组索引。没有。


使用此代码,您现在覆盖数组的边界,

结束于49.请记住数组从0开始,然后去长度为1。所以如果我

有一个类似的数组:


char数组[45];


我只能访问array [0]到array [44]。


如果你访问数组范围之外的任何东西,它是未定的

行为,但大多数可能你会覆盖另一个程序(运行在机器上的b
)数据导致崩溃或更糟糕的东西。


回到你的问题结构中的成员是否是数组的一部分,没有。您试图将unsigned char作为内存预留的形式传递给

函数。而是使用:


struct mystruct p_yourstruct * =(struct mystruct)malloc(sizeof(mystruct));


来获取内存。例如:

--------------------------------------- ------

struct mystruct

{

unsigned char testchar;

unsigned short testshort;

unsigned char anothertestchar;

};


struct mystruct * mem_location =(struct mystruct)malloc(sizeof(mystruct));


void testfunc(struct mystruct * mymemlocation)

{

thedata-> testchar = 0x55;

thedata-> testshort = 0xFFFF;

thedata-> anothertestchar = 0x11;

};

----- -------------------------------------------------- ----------------

malloc()为你的struct分配内存。你需要在程序终止之前调用

free(mem_location)来释放内存,或者

否则你会得到内存泄漏。


要使用你传递的代码:


testfunc(mem_location);


然后你可以用变量搞定。


尽量不要混合苹果和橘子。你应该传递给

结构的唯一东西是另一个相同类型的结构。这就像将一辆汽车

变量传递给一个使用汽车来维持豪宅的功能。


还有问题吗? =)


No. I think you mean: would the buffer[index] hold the values from the
function as array indexes. No.

With this code, you are now overwriting the boundary of your array, which
ends at 49. Remember that arrays start from 0, and go to length-1. So if I
had an array like:

char array[45];

I can only access array[0] to array[44].

If you access anything outside of your array''s range, it is undetermined
behavior, but most likely you will overwrite another program''s (running on
your machine) data causing a crash or something nastier.

To get back to your question about whether the members from the struct are
part of the array, no. You are attempting to pass an unsigned char to your
function as a form of memory reservation. Instead use:

struct mystruct p_yourstruct* = (struct mystruct) malloc(sizeof(mystruct));

to get the memory. For example:
---------------------------------------------
struct mystruct
{
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};

struct mystruct* mem_location = (struct mystruct) malloc(sizeof(mystruct));

void testfunc(struct mystruct* mymemlocation)
{
thedata->testchar = 0x55;
thedata->testshort = 0xFFFF;
thedata->anothertestchar = 0x11;
};
-----------------------------------------------------------------------
malloc() allocates memory for your struct. You need to call
free(mem_location) to free up the memory before your program terminates, or
else you will get memory leak.

To use the code you would pass:

testfunc(mem_location);

and then you could goof off with the variables.

Try not to mix apples and oranges. The only thing you should pass to hold a
struct is another struct of the same type. It would be like passing a car
variable to a function that uses the car to hold a mansion.

Any more questions? =)


这篇关于理解一段代码......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆