我有一个带有realloc的printf的奇怪行为.了解堆腐败我做了一个简单的程序测试.
void testFct(){
char *buffer;
buffer = (char *)malloc( 8 ); //Allocate 8 bytes
strcpy(buffer,""abcdefghijklm"); //Generate memory overwrite
fprintf(stdout,"\nOrginal buffer = %s\t",buffer);
fprintf(stdout,"%d bytes\n",_msize(buffer) );
buffer = (char *)realloc(buffer,512); //Reallocate more bytes
fprintf(stdout,"Buffer after reallocation = %s\t",buffer);
fprintf(stdout,"%u bytes\n",_msize(buffer) );
free(buffer); //Free the buffer
fprintf(stdout,"Buffer after freed = %s\t\t",buffer);
fprintf(stdout,"%u bytes\n\n",_msize(buffer) );
}
void main(){
printf("something\n");
testFct();
}
Run Code Online (Sandbox Code Playgroud)
当我从main中删除printf时,程序运行并显示:
Orginal buffer = abcdefghijklm 8 bytes
Buffer after reallocation = abcdefgh 512 bytes
Buffer after …Run Code Online (Sandbox Code Playgroud) 在制作这个节目期间,我发生了很多事情,我觉得最好问问你们.
例如,如果我有一个调用向量的特定结构的循环,那么更好地反复调用向量,如下所示:
FP_list[n].callsign=...
FP_list[n].de_airport=...
FP_list[n].ar_airport=...;
FP_list[n].aircraft_type=...
FP_list[n].trueairspeed=...
FP_list[n].FL_route.push_back(Aircraft.GetClearedAltitude());
FP_list[n].last_WP=...
FP_list[n].next_WP=...
Run Code Online (Sandbox Code Playgroud)
...
或者声明一个临时变量并从这一点开始使用它,如下所示:
FP temp=FP_list[n];
temp.callsign=...
...
temp.next_WP=...
Run Code Online (Sandbox Code Playgroud)
哪一个在内存消耗和运行时间方面更好?
先感谢您
我为内存泄漏压力开发了一个小应用程序:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int period = 0;
int size = 40000001;
char *buf = NULL;
if (argc > 1) period = atoi(argv[1]);
if (period == 0) period = 21;
for (;;) {
buf = malloc(size);
if (buf == NULL) printf ("malloc return NULL\n");
sleep(period);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
应用程序每20秒进行一次~40M的内存分配(无空闲).我的系统内存是~256M.
我使用top命令检查了压力应用程序消耗的内存:
$top | grep "stress"
873 5689 root S 39892 16% 0% ./stress_test
873 5689 root S 39892 16% 0% ./stress_test
873 5689 …Run Code Online (Sandbox Code Playgroud) 我对粘贴的代码有疑问.我为int分配了内存并删除了(像往常一样).但在某处,我看到这个sysntax(第1行)分配了一个匿名的int空间.如何释放这个空间,这不是内存泄漏的结果吗?
main(){
int *p = new int;
new int;
if(p)
delete p;
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有以下课程:
@implementation MyUICollectionViewCell {
CAShapeLayer *layr;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.layer.mask = [CAShapeLayer layer];
layr = [CAShapeLayer layer];
}
return self;
}
- (void)initMask {
self.layer.mask = [CAShapeLayer layer];
}
- (void)updateMask {
CAShapeLayer *maskLayer = (CAShapeLayer*) self.layer.mask;
maskLayer.frame = ...;
maskLayer.path = ...;
}
@end
Run Code Online (Sandbox Code Playgroud)
如果我self.layer.mask在构造函数中赋值,则它nil在updateMask方法中.但是,如果赋值是在initMask方法中完成的(从类外部调用),一切正常.
谁能解释一下这里发生了什么?
编辑:
尝试将对象存储在ivar(即layr)中,但无济于事 - 它仍然nil存在updateMask.
假设你有一些像这样的代码:
void myFunction()
{
myClass * mine = new myClass();
// body of function
delete mine;
}
Run Code Online (Sandbox Code Playgroud)
如果在函数体内抛出异常,delete则永远不会调用异常并导致内存泄漏.除了使用其中的任何托管指针之外,缓解此问题的最佳方法是什么<memory>.
c++ pointers memory-leaks memory-management exception-handling
我编写了一个函数,通过使用HeapAlloc()和HeapFree()来连接字符串,我想检查这些函数的返回.但是,如果分配失败,我必须再次尝试分配,直到它工作.怎么做 ?
我将这个问题与这个问题联系起来.
void add_to_buffer(LPTSTR* buffer, LPCTSTR msg) {
// Determine new size
int newSize = 0;
// Allocate new buffer
if (*buffer == NULL)
newSize = _tcsclen(msg) + 1;
else
newSize = _tcslen(*buffer) + _tcsclen(msg) + 1;
// Do the copy and concat
if (*buffer == NULL)
{
*buffer = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newSize * sizeof(*buffer));
_tcsncpy_s(*buffer, newSize, msg, _tcsclen(msg));
}
else
{
*buffer = (LPTSTR)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *buffer, newSize * sizeof(*buffer));
_tcsncat_s(*buffer, newSize, msg, _tcsclen(msg));
}
}
Run Code Online (Sandbox Code Playgroud) 我遇到了这个我今天无法解释的问题.
int main(){
vector<int> vec = subroutine();
...
delete(&vec);
}
vector<int>& subroutine(){
vector<int>* vec = new vector<int>();
//Init the vec
for(int i=0;i<vec.size();i++){
...
}
return *vec;
}
Run Code Online (Sandbox Code Playgroud)
它出现了一个错误:
double free or corruption (out): 0X00007fffa145ff50
Run Code Online (Sandbox Code Playgroud)
这绝对是该行的问题:
delete(&vec);
Run Code Online (Sandbox Code Playgroud)
但我无法解释,为什么有双免费?
我正在看这个显示移动构造函数的答案:
#include <cstring>
#include <algorithm>
class string
{
char* data;
public:
string(const char* p)
{
size_t size = strlen(p) + 1;
data = new char[size];
memcpy(data, p, size);
}
~string()
{
delete[] data;
}
string(const string& that)
{
size_t size = strlen(that.data) + 1;
data = new char[size];
memcpy(data, that.data, size);
}
};
Run Code Online (Sandbox Code Playgroud)
然后介绍一个移动构造函数:
string(string&& that) // string&& is an rvalue reference to a string
{
data = that.data;
that.data = 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我们分配指向data该值的指针that.data,肯定这会导致内存泄漏,该new …
class App{
int[] a;
private void firstFunction(){
int[] b = {1, 2, 3, 4};
a = new int[4];
a = b;
}
private void secondFunction(){
for(int i=0; i<a.length; a++) System.out.println(a[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
双方a并b都朝着同一个内存指针.当b超出范围时,应该释放分配的内存并且a应该变为空,对吧?或者它是基于引用计数方法,b被删除但内存仍然存在?