在c ++中
class bar
{
int i;
char b;
float d;
};
void foo ( bar arg );
void foo ( bar &arg );
void foo ( bar *arg );
Run Code Online (Sandbox Code Playgroud)
这是一个示例类/结构和函数,
我有一些Q.
我知道什么是"现代编译器和CPU无关紧要",但如果我们谈论旧CPU或编译器呢?
提前致谢
使用C++时,如果有一个类:
class MyClass
{
char memory1bye;
int memory4bytes;
int another4bytes;
};
Run Code Online (Sandbox Code Playgroud)
这个类在内存中总共使用9个字节...所以,如果我做了类似的事情:
MyClass *t1;
Run Code Online (Sandbox Code Playgroud)
这将为我提供Class的可用地址,但是它会分配9个字节吗?它会调用默认构造函数吗?或者我需要将这9个字节malloc给该类吗?如果那时我打电话给:
t1 = (MyClass *)new MyClass;
Run Code Online (Sandbox Code Playgroud)
是否会被视为内存泄漏?换句话说,旧地址会发生什么?
我正在进行内存分配/释放功能,
这很简单
inline void safedealloc ( void *mem )
{
if ( mem ) { free( mem ); mem = NULL; }
}
Run Code Online (Sandbox Code Playgroud)
它工作正常..然而,在使用它几次与程序后,
我注意到它在类似的东西中调用它
safedealloc( (char *)name );
safedealloc( (char *)name );
Run Code Online (Sandbox Code Playgroud)
导致错误.. 第一次调用后名称应该为null.但它没有,第二次使用无效指针ofc导致错误..为什么不将null分配给名称,因为它应该?
PS:使用malloc正确分配名称,并使用有效的大小和内容
下面我有一个在我的大多数简单程序中运行的代码.
我想知道它是好还是坏......以及缺点/专业人士.
.
win32头文件:
win32.h
// example of a not realated code to exaplin the question
// this header have all win32 includes like win/proces/stdarg/string ... etc
#include <windows.h>
#include <process.h>
#include <stdarg.h>
Run Code Online (Sandbox Code Playgroud)
主头文件: inc.h
//this file includes the following
//top : the windows header file
#include "win32.h" // include the win32.h header file
//the extern define which is the question
//the first include cause the INCS to be defined
//any include afterwards causes the DD to go from 'nothing' …
Run Code Online (Sandbox Code Playgroud)