双增量问题。 [英] Double increment question.

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

问题描述

你好,小组。我最近一直在做太多的C ++编程,而且我已经开始在C方式的某些方面变得生疏了。

的东西,尤其是。高效的低级数据副本。


具体来说,我刚写了以下内容,但我不知道这是否安全:


void Func(char * left,char * right)

{

chat temp_left [17] = {''\ 0' '};

chat temp_right [17] = {''\ 0''};

int i;

char * ptr1, * ptr2;


/ * ...做一些事情...... * /


/ *将左边的部分复制到temp_left:* /

ptr1 = temp_left;

ptr2 = left;

while(ptr2< right)* ptr1 ++ = * ptr2 ++; //这个会工作吗???


/ * ...做一些其他的东西...... * /


返回;

}


我很确定这是从概念上复制角色的合理方式

从地址左开始;,最多但不包括地址右,

到数组temp_left。


但我的问题是,将我遇到了这个双倍增量的问题?

是否保证(* ptr2)总是被分配到(* ptr1)之前

要么增量发生???


对不起,如果这看起来像一个愚蠢的问题。我生锈了。


-

欢呼,

Robbie Hatley

East Tustin ,CA,USA

孤独的狼inj at pac bell dot net

(put" [usenet]" in subject to bypass spam filter)

home dot pac bell dot net slant earnur slant

解决方案

" Robbie Hatley" < bo *********** @ no.spamwrites:


Hello,group。我最近一直在做太多的C ++编程,而且我已经开始在C方式的某些方面变得生疏了。

的东西,尤其是。高效的低级数据副本。


while(ptr2< right)* ptr1 ++ = * ptr2 ++; //这会工作吗???


是否保证(* ptr2)总是被分配到(* ptr1)之前

增量是否发生? ??



是的。在处理char指针时与C ++相同不是吗?


Robbie Hatley写道:


Hello,group 。我最近一直在做太多的C ++编程,而且我已经开始在C方式的某些方面变得生疏了。

的东西,尤其是。高效的低级数据副本。


具体来说,我刚写了以下内容,但我不知道这是否安全:


void Func(char * left,char * right)

{

chat temp_left [17] = {''\ 0' '};

chat temp_right [17] = {''\ 0''};

int i;

char * ptr1, * ptr2;


/ * ...做一些事情...... * /


/ *将左边的部分复制到temp_left:* /

ptr1 = temp_left;

ptr2 = left;

while(ptr2< right)* ptr1 ++ = * ptr2 ++; //这个会工作吗???


/ * ...做一些其他的东西...... * /


返回;

}


我很确定这是从概念上复制角色的合理方式

从地址左开始;,最多但不包括地址右,

到数组temp_left。



我假设聊天是char。我假设我在未给出的代码部分也使用了



但我的问题是,我是否会遇到麻烦这个双倍增量?

是否保证(* ptr2)总是被分配到(* ptr1)之前

增量发生?



严格来说,保证的是位置ptr2指向



之前增量将是从位置ptr1分配值
指向增量前的值。但是,在指针被修改之前,这个赋值是否发生了

取决于实现。


Spiros Bousbouras


On Tue,2006年7月25日03:31:33 GMT,Robbie Hatley

< bo *********** @ no .spamwrote:


> Hello,group。我最近做了太多的C ++编程,并且我开始在C方式的某些方面变得生疏,特别是。高效的低级数据副本。

具体来说,我刚写了以下内容,但我不知道这是否安全:

void Func( char * left,char * right)
{

chat temp_left [17] = {''\ 0''};

chat temp_right [17] = {''\ 0''};

int i;

char * ptr1,* ptr2;


/ * ...做一些事情...... * /


/ *将左边的部分复制到temp_left:* /

ptr1 = temp_left;

ptr2 = left;

while(ptr2< right)* ptr1 ++ = * ptr2 ++; //这个会工作吗???


/ * ...做一些其他的东西...... * /


返回;
}

我很确定这是一种概念上复制角色的方式
从地址左开始,最多但不包括地址正确,数组temp_left。

但是我的问题是,这个双倍增量会遇到麻烦吗?
是否保证(* ptr2 )
增量发生之前总会被分配到(* ptr1)???

如果这看起来像一个愚蠢的问题,对不起。我生锈了。



您发现将操作分解为一个更明显的形式,以便b
令人反感?

while(ptr2< right){

* ptr1 = * ptr2;

ptr1 ++;

ptr2 ++;

}


Hello, group. I''ve been doing too much C++ programming lately, and
I''m starting to become rusty at some aspects of the C way of doing
things, esp. efficient low-level data copies.

Specificially, I just wrote the following, but I don''t know if this
is safe:

void Func(char* left, char* right)
{
chat temp_left [17] = {''\0''};
chat temp_right [17] = {''\0''};
int i;
char *ptr1, *ptr2;

/* ... do some stuff ... */

/* Copy the left part to temp_left: */
ptr1 = temp_left;
ptr2 = left;
while (ptr2 < right) *ptr1++ = *ptr2++; // WILL THIS WORK???

/* ... do some other stuff ... */

return;
}

I''m pretty sure that''s a conceptually sound way of copying the characters
starting at address "left", up-to-but-not-including address "right",
to array "temp_left".

But my question is, will I get into trouble with that double increment?
Is it guaranteed that (*ptr2) will always get assigned to (*ptr1) before
either increment occurs???

Sorry if this seems like a dorky question. I''m rusty.

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
home dot pac bell dot net slant earnur slant

解决方案

"Robbie Hatley" <bo***********@no.spamwrites:

Hello, group. I''ve been doing too much C++ programming lately, and
I''m starting to become rusty at some aspects of the C way of doing
things, esp. efficient low-level data copies.

while (ptr2 < right) *ptr1++ = *ptr2++; // WILL THIS WORK???

Is it guaranteed that (*ptr2) will always get assigned to (*ptr1) before
either increment occurs???

Yes. Same as in C++ when dealing with char pointers isnt it?


Robbie Hatley wrote:

Hello, group. I''ve been doing too much C++ programming lately, and
I''m starting to become rusty at some aspects of the C way of doing
things, esp. efficient low-level data copies.

Specificially, I just wrote the following, but I don''t know if this
is safe:

void Func(char* left, char* right)
{
chat temp_left [17] = {''\0''};
chat temp_right [17] = {''\0''};
int i;
char *ptr1, *ptr2;

/* ... do some stuff ... */

/* Copy the left part to temp_left: */
ptr1 = temp_left;
ptr2 = left;
while (ptr2 < right) *ptr1++ = *ptr2++; // WILL THIS WORK???

/* ... do some other stuff ... */

return;
}

I''m pretty sure that''s a conceptually sound way of copying the characters
starting at address "left", up-to-but-not-including address "right",
to array "temp_left".

I assume chat was meant to be char. I assume also i is used
in the part of the code not given.

But my question is, will I get into trouble with that double increment?
Is it guaranteed that (*ptr2) will always get assigned to (*ptr1) before
either increment occurs???

Strictly speaking , what is guaranteed is that the location ptr2 points
to
before the increment will be assigned the value from the location ptr1
points to before the increment. But whether this assignment occurs
before the pointers have been inceremented is up to the implementation.

Spiros Bousbouras


On Tue, 25 Jul 2006 03:31:33 GMT, "Robbie Hatley"
<bo***********@no.spamwrote:

>Hello, group. I''ve been doing too much C++ programming lately, and
I''m starting to become rusty at some aspects of the C way of doing
things, esp. efficient low-level data copies.

Specificially, I just wrote the following, but I don''t know if this
is safe:

void Func(char* left, char* right)
{
chat temp_left [17] = {''\0''};
chat temp_right [17] = {''\0''};
int i;
char *ptr1, *ptr2;

/* ... do some stuff ... */

/* Copy the left part to temp_left: */
ptr1 = temp_left;
ptr2 = left;
while (ptr2 < right) *ptr1++ = *ptr2++; // WILL THIS WORK???

/* ... do some other stuff ... */

return;
}

I''m pretty sure that''s a conceptually sound way of copying the characters
starting at address "left", up-to-but-not-including address "right",
to array "temp_left".

But my question is, will I get into trouble with that double increment?
Is it guaranteed that (*ptr2) will always get assigned to (*ptr1) before
either increment occurs???

Sorry if this seems like a dorky question. I''m rusty.


You find breaking up the operations into a form that''s more obvious to
be objectionable?

while (ptr2 < right) {
*ptr1 = *ptr2;
ptr1++;
ptr2++;
}


这篇关于双增量问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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