b******n 发帖数: 592 | 1 has anyone experienced pointer overflow in C++. I have a
void * ptr
what I am doing in C++ is :
ptr = reinterpret_cast(ptr)[size]
ugly, but I don't know how to do it prettier.
and the ptr overflowed.
0x7f7ea69f443d is base address
477856 is size
the result is:
0xa69f4440
I found it is very diffult to use raw pointer in C++. For example, if I want
to align the address, I can't do ptr &= ~0xff, something like this.
any suggestion? |
|
b******n 发帖数: 592 | 2 Sorry, I mis-read traces from my program.
My question is: how to do bit operation on pointers in C++?
I am just trying to adjust pointers by:
ptr &= ~0xf; /* C */
can't seem to do it in C++ |
|
b***y 发帖数: 2799 | 3 Interestingly, deque's iterators may be invalidated when insertions are made
only at the ends of the container, deque is the only standard STL container
whose iterators may be invalidated without also invalidating its pointers a
nd references.)
deque里不是直接用iterator吗?这pointer和reference是咋个概念? |
|
t****t 发帖数: 6806 | 4 pointer to function里不准带default parameter [8.3.6]
void g(double (*f)(const double&, const double&));
f的参数个数未知, g当然不能写, 除非你用template
但是即使你用了template, 拿到了(未知参数个数/类型)的function pointer f, 你打
算怎么调用它呢? |
|
d*******n 发帖数: 369 | 5 shared pointer 有个reference count to keep record of how many pointers to an
object. 但是有个地方不明白。比如
std::tr1:shared_ptr int_array(new int);
std::tr1:shared_ptr sec_int_array = int_array;
我看了一下shared_ptr的实现,对于上面的第二句,sec_int_array的reference count
是变成2了,但是好像int_array没变。 对于int_array他仍然认为只有他自己指向这
个int array. 这种inconsistancy 不对吧? 当ini_array变成0是,他会delete the
array too early, right? 因为还有sec_int_array指向它。
谁给解释解释 |
|
d*******n 发帖数: 369 | 6 en.是我看错了。shared_ptr composes a pointer to a 'smart' like pointer |
|
X****r 发帖数: 3557 | 7 That is not the right answer.
In C++, Iterator is a concept. A concept is a set of requirement for
a type. For example, a random access iterator must allow operators
++, +, +=, --, -, -=, [], etc. with the right semantics, while a
forward iterator only needs to have ++. Naked pointers can be
iterators, because they already have these operators built-in.
User-defined types can also be iterators, as long as they overload
required operators correctly.
So the difference between iterator and pointer |
|
t****t 发帖数: 6806 | 8 等等,你这答案根本不对。iterator is not smart pointer. at all.
which iterator is smart pointer (that can manage resource) in STL? none.
and design pattern is not "想让它作甚,就作甚". in addition, the design patt
ern iterator is not what asked here.
recommended book: effective STL...
现在还有标准的争论么? 我这里大致的用一下,就原则性错误了? |
|
z****e 发帖数: 2024 | 9 我说过了,iterator 是 smart pointer 这个不是我先发明的,是书上说的,好几本都
出现过这种说法。
这是一个说法,至于什么才是 “smart pointer ”,就是文字游戏和标注之争了。
我完全同意iterator在STL里边不能resource management.但是不带表不能设计一种具有此工能iterator啊。
我思过去了,不敢和二位恩师争。
我错了!
patt |
|
h****b 发帖数: 157 | 10 想不到大家这么踊跃热情 : )
如果问问什么stl不用pointer (我被问到),可不可以回答 iterator的operator被重载
过,比
pointer更容易使用,比如list的iterator++,直接指向下一个link list element。
另外,请问一下:vector是怎么实现的?我知道是dynamic array, continuous memory
,不
过如果需要resize (e.g. increase size), 怎么能保证还是continuous memory?
谢了 |
|
o*******0 发帖数: 699 | 11 smart pointer 实践是不是真的很有用呢?
比方说 auto_ptr, 你要把它定义在stack上,这样它才会自动消除。
可是知道把object定义在stack上,可能就不要auto_ptr了。
又比如说smart pointer with ref count. 要thread safe, 算错了还会死人。(那是
真查不出来的bug) |
|
f******e 发帖数: 582 | 12 Hi,
I have a C program my.c and I would like to apply some basic ideas from the
following paper to analyze the pointers used in my.c.
My question is: is there a free open source tool available for me to start
with? the tool mentioned in the following paper seems obsolete..
Thanks a lot.
http://portal.acm.org/citation.cfm?id=1062455.1062520
Title: Improving software security with a C pointer analysis
Full Text: Pdf
Authors: Dzintars Avots
Stanford University, Stanford, CA
Michael Dal... 阅读全帖 |
|
E******T 发帖数: 59 | 13
type.
((double (*)[NCA])a)
you are right and a is a pointer, such as definition: double *a;
the confusing part is how we define the array with NCA elements which a
pointer points to.
is this right like ([NCA])a
Thanks |
|
P********e 发帖数: 2610 | 14 that is wrong.
a is a two dimentional array pointer, point to NCA elements of pointer to
double type.
double* a[NCA]; |
|
t****t 发帖数: 6806 | 15 my point is to show what is improper code: you are not to alias and access
an object with imcompatible type through pointer conversion. for int and
float, maybe it is obvious that you shouldn't do it. but for some other
types, it may not be that obvious: for example, you can't change a void*
through pointer/reference to void**, even if you do casting (e.g. when
x is void*, you can't do (*(void***)(&x)) = ...., even with casting). i
personally fell through that trap several years ago, when i buil... 阅读全帖 |
|
a****l 发帖数: 8211 | 16 I don't know why it is so complicated. A pointer is a pointer, as long as
you know where it points to, you can cast it to anything you want. The code
is simply pointing to the binary of a float and try to interpret it as
integer, which surprises me that sometimes it actually would work. |
|
v******l 发帖数: 512 | 17 I don't quite get what you mean by "can not alias it". Your sample code is a
typical aliasing and as long as there is no memory issue (overflow or
something) there is nothing wrong with it.
Look at it this way: You need a buffer by using malloc you get a big chunk
of memory pointed by void *p0. There is nothing wrong you first cast it to
int* and fill it with a bunch of integers, then cast to float* and reads
them out as floats. It is merely a re-interpretation of data and it behaves
just like a... 阅读全帖 |
|
r****t 发帖数: 10904 | 18 以前一直不自觉的用,如果所有 unqualified-id 都默认是通过 this pointer, 是不
是 this pointer 是完全不必要的? |
|
w***g 发帖数: 5958 | 19 不是。iterator是pointer的推广,并不一定需要用pointer来实现。 |
|
g***l 发帖数: 2753 | 20 一个模块,原先是在C中写的,现在要用C++重新写,因为要添加新的功能。
情况是如下:
程序运行中,能够拿到一段数据,存在连续的memory中,uint8_t *data={30,23,.....
...},data[1]用来当tag_id用,不同tag_id要调用不同对象的处理函数,data[2]是表
示数据的长度。
在原来的C中,我是建立了一个 function pointer look up table,用data[1]来搜索,
返回相应的function pointer : void(*pfunc)(uint8_t* in_data_p,uint8_t * out_
result_p).
在C++中,现在每个对象都是一个单独的class,从base class继承来的。
class base_class
{
public:
uint8_t tag_id;
std::vector result;
public:
virtual ProcessData(uint8_t * in_data_p);
};
然后每个对象的class定义如下:
clas... 阅读全帖 |
|
t****t 发帖数: 6806 | 21 1st one is returning pointer/reference to local, which is wrong
2nd one is returning value of local, which is correct.
BTW you don't say "return local", you say "return pointer/reference/address
of local" |
|
t****t 发帖数: 6806 | 22 第二个是pointer to string literal, pointer类型怎么转换都是string literal. |
|
r*******n 发帖数: 3020 | 23 seriously?
C++里不能避免用pointer吧 |
|
|
b*******s 发帖数: 5216 | 25 try boost's smart pointers |
|
N******K 发帖数: 10202 | 26 比较古老的的一些程序 用的是 Intrusive smart pointer 就是 那个 reference
counter 保存在object里面
std里面的改为单独存一个 reference counter,然后每个smart pointer都要指向这个
reference counter和object
各位都用的是哪一种?
哪种好? |
|
r*********r 发帖数: 3195 | 27 returning a pointer that points to local objects?
that's called a dangling pointer. |
|
j******t 发帖数: 788 | 28 This is exactly smart pointer is doing. Call destructor while leaving the
scope, and delete the raw pointer. Am I right?
"所有的资源都在同一对花括号内获取和释放,指针根本不会用
错" |
|
E*******1 发帖数: 3464 | 29 你smart pointer可能没用好吧,怎么说smart pointer是个二流的东西。这玩意主要是
针对执行出错,throw了,结果跳过后面的delete内存释放步骤造成leak的解决方案 |
|
k**********g 发帖数: 989 | 30
The minimum requirement is that at least one of the smart pointer
implementation needs to support a user-provided deleter function (instead of
using the default delete operator of that type).
If this is only satisfied by one side, a one-way wrapping is possible.
Ideally, two-way wrapping is more desirable.
I have implemented a similar interoperable wrapper between OpenCV Mat class
and COM smart pointer. I do not have the source code right now, but it
basically uses the deleter function.
Since I... 阅读全帖 |
|
N******K 发帖数: 10202 | 31 另外 std::shared_ptr等比 intrusive smart pointer好 因为对每一个object 有一个
manager object 然后所有指针都指向 manager object
如果 object被干掉了 可以从manager object判断出来
如果多个raw指针指向一个object。 当这个object被干掉 这些 raw指针都指向垃圾
如果多个shared_ptr/weak_ptr指针通过manager object指向一个object, 当object被
干掉 这些 weak_ptr通过manager object指向Null pointer
如果每一个object都有一个指向自己的weak_ptr为成员变量 那就跟好了 |
|
t*****n 发帖数: 4908 | 32 你到底学啥的?说过多少遍了,smart pointer不是让程序员偷懒,而是让程序更健壮
。比起不负责随便分配内存的某语言来说,smart pointer是相当靠谱。
weak_ptr你再读读boost的网页吧。解释的很清楚。 |
|
l***l 发帖数: 41 | 33 比如写一个链表,是不是使用smart pointer更好一点?是不是只要需要用new和delete
的地方都应该用smart pointer? |
|
n****1 发帖数: 1136 | 34 smart pointer缺点:
1. 多线程下非常麻烦, 因为一般的智能指针都不是线程安全的.
2. 给人错误的安全感,让人放松对内存泄漏的警惕性. 如果程序员一不小心忽略了某个
pointer cycle的话, 内存就漏大发了. |
|
r*g 发帖数: 3159 | 35 应该问,什么时候stack变量加stl容器还不够用,需要用 pointer,然后再问什么时候
用smart pointer。 |
|
|
|
|
|
|
|
|
|
|
|
v***t 发帖数: 27100 | 46 红色激光笔,锁男做演讲必备,放家里当枪用。
巴马儿子晚上来了的话,用laser pointer一照,人家以为你上了AK47呢 |
|
|
m****u 发帖数: 43 | 48 我用kensington的,觉得不错哦。
Kensington Si600 Wireless Presenter with Laser Pointer - Presentation remote
control - radio |
|
g*******y 发帖数: 1930 | 49 change function pointer to a functor should be ok
template class CMP{
public:
operator()(const T &t1, const T &t2){ return t1
};
template class Heap{
public:
Heap(const vector &data, CMP cmp);
};
I |
|
o**********t 发帖数: 406 | 50 you cannot allocate a new block of RAM in sub function if you pass pointer. |
|