我可以信任重新分配的内存中的指针算术吗? [英] Can I Trust Pointer Arithmetic In Re-Allocated Memory?

查看:68
本文介绍了我可以信任重新分配的内存中的指针算术吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请耐心等待,因为我不是专业人士。程序员,但我是在b $ b上工作的程序,它读取了四个文本文件的一部分到一个缓冲区,当我读取每个文件时,我重新分配大小。我从缓冲区的底部读取了一些

的项目,有些来自

自上而下,将底部项目移回新的重新分配

读取每个文件的底部。


然后当我读完所有四个文件时,我对顶部和底部项目进行排序

单独使用qsort(),它指向一个项目列表,

将两个排序列表写入两个新文件。


问题是,我担心如果我只是在qsort()的底部列表中提供指向第一项

的指针,它可能会在

期间指出bozo-land排序因为我认为动态重新分配内存

不一定是连续的。所以我已经做了两个步骤

我将底部列表写到另一个缓冲区来进行排序和写作,

,一切都很好,但我'我想知道我是否在浪费时间

而且一点也不担心......毕竟,如果我不能相信指向

任意点的指针列表,我怎么能相信一个指向列表开头的指针?


任何关于如何动态处理指针的灯光/>
分配的内存很有意思,也很有帮助...谢谢。


---

William Ernest Reid

Bear with me, as I am not a "professional" programmer, but I was
working on part of program that reads parts of four text files into
a buffer which I re-allocate the size as I read each file. I read some
of the items from the bottom up of the buffer, and some from the
top down, moving the bottom items back to the new re-allocated
bottom on every file read.

Then when I''ve read all four files, I sort the top and bottom items
separately using qsort(), which takes a pointer to a list of items, and
write the two sorted lists to two new files.

Problem is, I worry that if I just supply a pointer to the first item
in the bottom list to qsort(), it might point out to bozo-land during
the sort because I thought that dynamically re-allocated memory
is not necessarily contiguous. So I''ve done a little two step where
I write the bottom list to another buffer to do the sorting and writing,
and everything works great, but I''m wondering if I''m wasting time
and worrying about nothing...after all, if I can''t trust a pointer to an
arbitrary point in the list, how can I trust a pointer to the start of
the list?

Any light you can shed on how pointers are handled in dynamically
allocated memory would be interesting and helpful...thanks.

---
William Ernest Reid

推荐答案

2006年8月11日星期五03:54:19 GMT,Bill Reid

< ho ** ******@happyhealthy.netwrote:
On Fri, 11 Aug 2006 03:54:19 GMT, "Bill Reid"
<ho********@happyhealthy.netwrote:

>请耐心等待,因为我不是专业人士。程序员,但是我正在编写部分程序,将四个文本文件的一部分读入一个缓冲区,我在读取每个文件时重新分配大小。我从缓冲区的底部向上读取了一些项目,有些是从上到下读取的,在每个文件读取时将底部项目移回新的重新分配的底部。
>Bear with me, as I am not a "professional" programmer, but I was
working on part of program that reads parts of four text files into
a buffer which I re-allocate the size as I read each file. I read some
of the items from the bottom up of the buffer, and some from the
top down, moving the bottom items back to the new re-allocated
bottom on every file read.



我不太理解这个描述。

I don''t quite follow this description.


>
然后当我已经阅读了所有四个文件,我使用qsort()分别对顶部和底部项目进行排序,它使用指向项目列表的指针,并将两个已排序的列表写入两个新文件问题是,我担心如果我只提供指向qsort()底部列表中第一项
的指针,它可能会指出bozo-land期间
排序因为我认为动态重新分配的内存不一定是连续的。所以我已经做了两步,其中
>
Then when I''ve read all four files, I sort the top and bottom items
separately using qsort(), which takes a pointer to a list of items, and
write the two sorted lists to two new files.

Problem is, I worry that if I just supply a pointer to the first item
in the bottom list to qsort(), it might point out to bozo-land during
the sort because I thought that dynamically re-allocated memory
is not necessarily contiguous. So I''ve done a little two step where





malloc / realloc返回的非NULL地址的内存块/ calloc保证是连续的。你的记忆是从地址到地址+ size-1分配的
。此外,计算

总是允许值地址+大小,但你可能无法解除引用

这个地址。

The block of memory whose non-NULL address is returned from
malloc/realloc/calloc is guaranteed to be contiguous. You memory is
allocated from address to address+size-1. Furthermore, calculating
the value address+size is always allowed but you may not dereference
this address.


>我将底部列表写到另一个缓冲区来进行排序和编写,
并且一切都很好,但我想知道我是否在浪费时间而且不用担心......毕竟,如果我不相信指向列表中任意点的指针,我怎么能相信指向列表开头的指针呢?

你可以在动态分配的内存中处理如何处理指针的任何亮点都会很有趣且有用...谢谢。
>I write the bottom list to another buffer to do the sorting and writing,
and everything works great, but I''m wondering if I''m wasting time
and worrying about nothing...after all, if I can''t trust a pointer to an
arbitrary point in the list, how can I trust a pointer to the start of
the list?

Any light you can shed on how pointers are handled in dynamically
allocated memory would be interesting and helpful...thanks.



上述限制之间的指针值在分配内存的范围内。你必须确保对齐,但如果指针

具有正确的类型,编译器将为你执行此操作。

删除del电子邮件

A pointer value between the limits mentioned above is within range of
the allocated memory. You have to insure alignment but if the pointer
has the correct type the compiler will do this for you.
Remove del for email




Barry Schwarz< sc ****** @ doezl.netwrote in message

news:k9 ******** ************************ @ 4ax.com ...

Barry Schwarz <sc******@doezl.netwrote in message
news:k9********************************@4ax.com...

周五, 2006年8月11日03:54:19 GMT,Bill Reid

< ho ******** @ happyhealthy.netwrote:
On Fri, 11 Aug 2006 03:54:19 GMT, "Bill Reid"
<ho********@happyhealthy.netwrote:

跟我一起承担,因为我不是专业人士。程序员,但我是在b $ b上工作的程序,它读取了四个文本文件的一部分到一个缓冲区,当我读取每个文件时,我重新分配大小。我从缓冲区的底部读取了一些

的项目,有些来自

自上而下,将底部项目移回新的重新分配

读取每个文件的底部。
Bear with me, as I am not a "professional" programmer, but I was
working on part of program that reads parts of four text files into
a buffer which I re-allocate the size as I read each file. I read some
of the items from the bottom up of the buffer, and some from the
top down, moving the bottom items back to the new re-allocated
bottom on every file read.



我不太理解这个描述。


I don''t quite follow this description.



是的,这有点令人困惑,而不是那些与我是什么相关的... b $ b要求...

底线是我想单独排序列表的两个部分...

Yeah, it''s a little confusing, and not that relevant to what I''m
asking...the
bottom line is I want to separately sort two parts of a list...



然后当我读完所有四个文件时,我对顶部和底部项目进行排序

分别使用qsort(),它指向一个项目列表,

将两个排序列表写入两个新文件。


问题是的,我担心如果我只是在qsort()的底部列表中提供指向第一项

的指针,它可能会在

期间指向bozo-land排序因为我认为动态重新分配内存

不一定是连续的。所以我已经做了两步,其中

Then when I''ve read all four files, I sort the top and bottom items
separately using qsort(), which takes a pointer to a list of items, and
write the two sorted lists to two new files.

Problem is, I worry that if I just supply a pointer to the first item
in the bottom list to qsort(), it might point out to bozo-land during
the sort because I thought that dynamically re-allocated memory
is not necessarily contiguous. So I''ve done a little two step where





malloc / realloc返回非NULL地址的内存块/ calloc保证是连续的。


The block of memory whose non-NULL address is returned from
malloc/realloc/calloc is guaranteed to be contiguous.



好​​的,这就是答案,我错误的是内存

可能不是连续的......我可能只读了那个保证

大约1000亿次,但只是忘了它。


我觉得我对重新分配的想法感到困惑br />
块可能与原始malloc的位置不同,

意味着......

OK, that''s the answer, I was just plain wrong that the memory
might not be contiguous...I''ve probably only read that guarantee
about 100000000000 times but just forgot it.

I think I got that confused with the idea that the re-allocated
block may have a different location than the original malloc, which
would mean...


你的记忆

从地址分配到地址+ size-1。此外,计算

总是允许值地址+大小,但你可能不会解除引用

这个地址。
You memory is
allocated from address to address+size-1. Furthermore, calculating
the value address+size is always allowed but you may not dereference
this address.



....你不想取消引用一个地址,对吧。

....you wouldn''t want to dereference an address, right.


我把底部列表写到另一个缓冲区做分类和写作,

,一切都很好,但我想知道我是否在浪费时间

并且无所事事......毕竟... ,如果我不能相信指向列表中某个

任意点的指针,我怎么能相信一个指向

列表开头的指针?


任何关于如何动态处理指针的任何亮点

分配的内存将是有趣和有用的...谢谢。
I write the bottom list to another buffer to do the sorting and writing,
and everything works great, but I''m wondering if I''m wasting time
and worrying about nothing...after all, if I can''t trust a pointer to an
arbitrary point in the list, how can I trust a pointer to the start of
the list?

Any light you can shed on how pointers are handled in dynamically
allocated memory would be interesting and helpful...thanks.



上述限制之间的指针值在分配内存的范围内。你必须确保对齐,但如果指针

具有正确的类型,编译器将为你执行此操作。


A pointer value between the limits mentioned above is within range of
the allocated memory. You have to insure alignment but if the pointer
has the correct type the compiler will do this for you.



好​​的,所以这应该是完全合法和完美的:


/ *按字母顺序排序符号列表* /

qsort((void *)curr_instrs,num_symbols,128,sort_alpha_list);


然后......


/ *按字母顺序排列无符号列表* /

qsort(( void *)curr_instrs + num_symbols,num_no_symbols,128,sort_ alpha_list);


首先qsort()排序到列表符号部分的末尾,

,第二个从

列表的无符号部分开始到列表末尾。我想这是(无效*)演员害怕

我...谢谢。


---

威廉欧内斯特·里德

OK, so this should be completely legal and flawless:

/* sort the symbol list alphabetically */
qsort((void *)curr_instrs,num_symbols,128,sort_alpha_list);

then...

/* sort the no-symbol list alphabetically */
qsort((void *)curr_instrs+num_symbols,num_no_symbols,128,sort_ alpha_list);

First qsort() sorts down to the end of the symbols part of the list,
the second sorts down from the start of the no-symbols part of the
list to the end of the list. I guess it was the (void *) cast that scared
me...thanks.

---
William Ernest Reid


" Bill Reid" < ho ******** @ happyhealthy.netwrites:
"Bill Reid" <ho********@happyhealthy.netwrites:

Barry Schwarz< sc ****** @ doezl.netwrote in message

新闻:k9 ******************************** @ 4ax.com ...
Barry Schwarz <sc******@doezl.netwrote in message
news:k9********************************@4ax.com...

>星期五,2006年8月11日03:54:19 GMT,Bill Reid
< ho ******** @ happyhealthy.netwrote:
>On Fri, 11 Aug 2006 03:54:19 GMT, "Bill Reid"
<ho********@happyhealthy.netwrote:

>请耐心等待,因为我不是专业人士。程序员,但是我正在编写部分程序,将四个文本文件的一部分读入一个缓冲区,我在读取每个文件时重新分配大小。我从缓冲区的底部向上读取了一些项目,有些是从上到下读取的,在每个文件读取时将底部项目移回新的重新分配的底部。
>Bear with me, as I am not a "professional" programmer, but I was
working on part of program that reads parts of four text files into
a buffer which I re-allocate the size as I read each file. I read some
of the items from the bottom up of the buffer, and some from the
top down, moving the bottom items back to the new re-allocated
bottom on every file read.


我不太理解这个描述。


I don''t quite follow this description.



是的,这有点令人困惑,而不是那个与我是什么相关

问...

底线是我想单独排序列表的两个部分...

Yeah, it''s a little confusing, and not that relevant to what I''m
asking...the
bottom line is I want to separately sort two parts of a list...


>
然后,当我读完所有四个文件时,我使用qsort分别对顶部和底部项目进行排序( ),它指向一个项目列表,并将两个排序列表写入两个新文件。

问题是,我担心如果我只提供一个指向第一个项目
在qsort()的底部列表中,它可能会指出在分类期间的bozo-land因为我认为动态重新分配的内存
不一定是连续的。所以我已经做了两步,其中
>
Then when I''ve read all four files, I sort the top and bottom items
separately using qsort(), which takes a pointer to a list of items, and
write the two sorted lists to two new files.

Problem is, I worry that if I just supply a pointer to the first item
in the bottom list to qsort(), it might point out to bozo-land during
the sort because I thought that dynamically re-allocated memory
is not necessarily contiguous. So I''ve done a little two step where


从malloc / realloc / calloc返回非NULL地址的内存块保证是连续的。


The block of memory whose non-NULL address is returned from
malloc/realloc/calloc is guaranteed to be contiguous.



好​​的,这就是答案,我错误地认为记忆

可能不是连续的......我可能只读了那个保证

大约1000亿次,但只是忘了它。


我觉得我对重新分配的想法感到困惑br />
块可能与原始malloc的位置不同,

意味着...


OK, that''s the answer, I was just plain wrong that the memory
might not be contiguous...I''ve probably only read that guarantee
about 100000000000 times but just forgot it.

I think I got that confused with the idea that the re-allocated
block may have a different location than the original malloc, which
would mean...



一件事我发现你的原始邮件有点令人困惑的是你所谈到的重新分配的消息。记忆,但你没有提到

realloc功能。您的描述越具体,我们就可以提供更多的帮助。


[...]

One thing that I found a little confusing in your original message is
that you talked about "re-allocated" memory, but you didn''t mention
the "realloc" function. The more specific your description, the more
likely it is that we can help.

[...]


好​​的,所以这应该是完全合法和完美的:


/ *按字母顺序排序符号列表* /

qsort(( void *)curr_instrs,num_symbols,128,sort_alpha_list);


然后......


/ *按字母顺序排序无符号列表* /

qsort((void *)curr_instrs + num_symbols,num_no_symbols,128,sort_ alpha_list);
OK, so this should be completely legal and flawless:

/* sort the symbol list alphabetically */
qsort((void *)curr_instrs,num_symbols,128,sort_alpha_list);

then...

/* sort the no-symbol list alphabetically */
qsort((void *)curr_instrs+num_symbols,num_no_symbols,128,sort_ alpha_list);



嗯,没有。


不要害怕空白。我把空白放在大多数操作符

符号周围,并在每个逗号后面。如果我必须将某些东西拆分成

行,那没关系。所以我会写你的qsort电话:


qsort((void *)curr_instrs + num_symbols,

num_no_symbols,

128,

sort_alpha_list);


第三个参数128是一个幻数。

很难说出它意味着什么,或者它是否正确。定义一个常数:

#define WHATEVER 128

所以你只需要在一个地方改变它(但选择一个更好的名字,

当然)。


qsort的第一个参数是:


(void *)curr_instrs + num_symbols


你不能对void *值进行指针运算。 (有些编译器可以

允许它;如果你正在使用gcc,请尝试-ansi -pedantic -Wall -W,或者

替换-ansi ;使用-std = c99)。


如果你想获得curr_instrs指向的地址加上num_symbols的

偏移量字节,你需要使用

char *:


qsort((char *)curr_instrs + num_symbols,

/ *其他args * /);


假设curr_instrs不是char *。请注意,我没有将
表达式转换为void *;任何指向对象的指针类型都可以将
转换为void *,反之亦然。


-

Keith Thompson (The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

Um, no.

Don''t be afraid of whitespace. I put blanks around most operator
symbols, and after every comma. If I have to split something across
lines, that''s ok. So I''d write your qsort call as:

qsort((void *)curr_instrs + num_symbols,
num_no_symbols,
128,
sort_alpha_list);

The third argument, 128, is a "magic number". It''s very difficult to
tell what it means or whether it''s even correct. Define a constant:
#define WHATEVER 128
so you only need to change it in one place (but pick a better name, of
course).

The first argument to qsort is:

(void *)curr_instrs + num_symbols

You can''t do pointer arithmetic on a void* value. (Some compilers may
allow it; if you''re using gcc, try "-ansi -pedantic -Wall -W", or
replace "-ansi" with "-std=c99").

If you''re trying to get the address pointed to by curr_instrs plus an
offset of num_symbols bytes, you''ll need to to the arithmetic using
char*:

qsort((char*)curr_instrs + num_symbols,
/* other args */);

assuming that curr_instrs isn''t already a char*. Note that I didn''t
cast the expression to void*; any pointer-to-object type can be
converted to void*, or vice versa.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于我可以信任重新分配的内存中的指针算术吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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