如果我在类构造函数中用malloc
(或new
/new[]
)分配内存,那么这段内存是否会被安全地覆盖?
class stack {
private:
int * stackPointer;
public:
stack (int size) {
stackPointer = (int *) malloc (sizeof(int) * stackSize);
}
int peek (int pos) {
return *(stackPointer + pos); //pos < size
}
}
char str[9] = malloc (9); return str;
。 - texasfloodchar str[9] = "wrong"; return str;
这里没有使用 malloc。 - interjay