请快速检查代码 [英] Quick Code Check Please

查看:68
本文介绍了请快速检查代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我用C ++编程以来已经很长时间了,我想通过社区运行一些功能,以确保我的思路正确。



理论上,这些构造函数应该接受大小或类型不同的输入,然后将它们保存到类中要分配的字节数组中。我正努力让他们能够接受8,16,32和64位长度。



< MyClass.h> 

Class MyClass {
private
__ int8 * InputBuffer;
__ int8 * OutputBuffer;
public
MyClass( __ int8 *);
MyClass( __ int16 *);
MyClass( __ int32 *);
MyClass( __ int64 *):
~MyClass();
// 其他功能
}

< ; MyClass.cpp>

MyClass ::〜MyClass()
{
if - > InputBuff!= nullptr ){_ freea( this - > InputBuff) ; }
if this - > OutputBuff!= nullptr ){_ freea( this - > OutputBuff); }
}

MyClass :: MyClass( __ int8 *来源)
{
void * p = nullptr ;
__ int64 size = _msize(来源);

p = _malloca(size);

if (p!= nullptr
memcpy_s(p ,大小,来源,大小);
else
return ;

- > InputBuff =( __ int8 *)p;
}

MyClass :: MyClass( __ int16 *来源)
{
void * p = nullptr ;
__ int64 size = _msize(来源)/ sizeof __ INT16 );

p = _malloca(size * sizeof __ int16 ));

if (p!= nullptr
memcpy_s(p ,size * sizeof __ int16 ),来源,大小);
else
return ;

- > InputBuff =( __ int8 *)p;
}





我只是在这里寻找你的2美分,所以我在写作之前并不是完全搞砸了类的其余部分(以及项目的其余部分)。

解决方案

alloca()变量从堆栈分配,返回的指针最迟超出范围你离开了你调用alloca()函数的函数。你应该使用malloc()。不要初始化构造函数中可能失败的任何内容。如果要将复杂的初始化代码放入构造函数中,只有在本地处理错误或者可以将错误存储到成员变量中并稍后检查它时才执行此操作。更好的替代方法是使用默认值初始化成员变量,然后在对象上调用Init()方法。与构造函数中的初始化解决方案相比,这有另外一个好处:在构造函数中,不鼓励调用虚方法,而在Init()中可以轻松地执行此操作。而不是使用__intXX Visual C ++特定的修复宽度整数类型包括< stdint.h>并使用intXX_t常量,这是一种可移植的方式。使用_msize()也很少见,不要使用此功能。如果我向你传递一个未在堆上分配的指针,或者即使它在堆上,我会通过指向指向已分配块的中间而不是指向开头的指针,该怎么办?在缓冲区中处理二进制数据时,这是标准,而不是传递指针并在(const void *)加上size参数中使用_msize传递。



< pre lang =c ++> #include < < span class =code-leadattribute> stdint.h >

class MyClass
{
public
/ / 不推荐
MyClass( const void * source)
:m_InputBuffer(NULL)
{
// 没有错误检查!
Init(来源);
}

// 不推荐
MyClass ( const void * source, size_t source_size )
:m_InputBuffer(NULL)
{
// 没有错误检查!
Init(source,source_size);
}

// 不推荐
< span class =code-keyword> bool Init( const void * source)
{
return Init(source,_ msize(( void *)source)) ;
}

bool Init( const void * source, size_t source_size)
{
void * p = malloc(source_size);
if (!p)
return ;
memcpy(p,source,source_size);
m_InputBuffer =(int8_t *)p;
return true ;
}

private
int8_t * m_InputBuffer;
};


It's been a long time since I programmed in C++ and I wanted to run a couple of functions by the community just to ensure that my line of thought is correct.

These constructors are supposed to, in theory, accept inputs differing in size or type and then save them to an allocated byte array in the class to be worked on after. I am working towards them being able to accept 8, 16, 32, and 64 bit lengths.

<MyClass.h>

Class MyClass{
private:
__int8* InputBuffer;
__int8* OutputBuffer;
public:
MyClass(__int8*);
MyClass(__int16*);
MyClass(__int32*);
MyClass(__int64*):
~MyClass();
//Other Functions
}

<MyClass.cpp>

MyClass::~MyClass()
{
	if ( this->InputBuff != nullptr ) { _freea(this->InputBuff); }
	if ( this->OutputBuff != nullptr ) { _freea(this->OutputBuff); }
}

MyClass::MyClass(__int8* Source)
{
	void *p = nullptr;
	__int64 size = _msize(Source);

	p = _malloca(size);

	if (p != nullptr) 
		memcpy_s(p, size, Source, size);
	else
		return;
			
	this->InputBuff = (__int8*) p;
}

MyClass::MyClass(__int16* Source)
{
	void *p = nullptr;
	__int64 size = _msize(Source) / sizeof(__int16);

	p = _malloca(size * sizeof(__int16));

	if ( p != nullptr) 
		memcpy_s(p, size * sizeof(__int16), Source, size);
	else
		return;
		
	this->InputBuff = (__int8*) p;
}



I'm just looking for your 2 cents here so that I'm not totally messing this up before writing the rest of the class (and the rest of the project).

解决方案

alloca() variants allocate from the stack and the returned pointer goes out of scope at latest when you leave th function in which you called the alloca() function. you should use malloc(). Don't initialize anything in the constructor that can fail. If you want to put complex initializer code to the constructor then do it only if the error can be handled locally or if you can store the error into a member variable and later you check it. A better alternative to this is just initializing the member variables with default values and calling later an Init() method on the object. This has an additional benefit in contrast to the "initialization in constructor" solution: In the constructor it is discouraged to call virtual methods while in the Init() can do that easily. Instead of using __intXX Visual C++ specific fix-width integer types include <stdint.h> and use intXX_t constants, this is a portable way. Using _msize() is also rare, don't use this function. What if I pass you a pointer that isn't allocated on the heap or even if it is on the heap I pass you a pointer that points to the middle of the allocated block and not to the beginning? Instead of passing a pointer and using _msize pass in a (const void*) plus a size parameter, this is the standard when working with binary data in buffers.

#include <stdint.h>

class MyClass
{
public:
    // not recommended
    MyClass(const void* source)
        : m_InputBuffer(NULL)
    {
        // no error checking!
        Init(source);
    }

    // not recommended
    MyClass(const void* source, size_t source_size)
        : m_InputBuffer(NULL)
    {
        // no error checking!
        Init(source, source_size);
    }

    // not recommended
    bool Init(const void* source)
    {
        return Init(source, _msize((void*)source));
    }

    bool Init(const void* source, size_t source_size)
    {
        void* p = malloc(source_size);
        if (!p)
            return false;
        memcpy(p, source, source_size);
        m_InputBuffer = (int8_t*)p;
        return true;
    }

private:
    int8_t* m_InputBuffer;
};


这篇关于请快速检查代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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