C++动态申请数组问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++动态申请数组问题相关的知识,希望对你有一定的参考价值。

动态申请一个一维整型数组,要求用户从键盘输入元素个数,采用new运算符分配空间,从键盘输入数组变量的值.写一个函数void Func(int* A, int nSize),使A把0移至后面,非0整数移至数组前面并保持有序(升序)。将排序后的数组变量的值输出到屏幕上,各数值间用一个空格隔开。
例:输入
10 1 2 3 4 5 0 7 8 0 10
输出
1 2 3 4 5 7 8 10 0 0

参考技术A #include"pch.h"
#include<iostream>
using namespace std;
int main()

int *pi = new int[88];
int a, b, c, i = 0, k = 0, r = 0, x = 0, y = 0;
cin >> a;
for (i; i < a; i++)

cin >> b;
*(pi + i) = b;
;
for (k; k <= a; k++)
if (*(pi + k) == 0)
x++;
for(int q=0,d=0;q<a;q++,d++)
for( int i=0;i<a-1-d;i++)
if ( *(pi+i)>*(pi+i+1))

int w = *(pi + i);
*(pi + i) = *(pi + i + 1);
*(pi + i + 1) = w;


for (int e = 0; e < a; e++)

if (*(pi + e) != 0)
cout << *(pi + e) << " ";

for (x; x > 0; x--)
cout << 0 << " ";
delete []pi;
return 0;
参考技术B //#include "stdafx.h"//vc++6.0加上这一行.#include <iostream>using namespace std;int *new_array(int n) return new int[n];void init_array(int *p, int n,int c) for(int i=0;i<n;*(p+i++)=c);int main(void) int n,c,*p; cout << "Enter n & c(int)...\n"; cin >> n >> c; p=new_array(n); init_array(p,n,c); for(int i=0;i<n;i++) cout << *(p+i) << (i!=n-1 ? ' ' : '\n'); delete [n]p; return 0;

C++函数中,两个自动释放内存的动态内存申请类

最近做一个事情,实现一个流程交互,其中主交互流程函数中,涉及较多的内存申请,

而健康的函数,都是在函数退出前将手动申请不再需要的内存释放掉,

使用很多方法,都避免不了较多的出错分支时,一堆的if free/delete,代码长而且不好管理

因此,利用C++对象离开作用域会自动调用析构函数的特点,在这儿实现了两个自动释放内存的动态内存申请类

第一个类,只管理内存,不并管理对象

#include <vector>

class XAutoFreeMem

protected:
    std::vector<void*> vec_memorys_;

public:
    XAutoFreeMem::XAutoFreeMem() ;

    virtual XAutoFreeMem::~XAutoFreeMem()
    
        //释放对象时,释放管理的内存
        for(auto item : vec_memorys_)
            free(item);
        
    

    //通过此接口来申请内存
    void* malloc_mem(unsigned int nsize) 
    
        void* ptr = malloc(nsize);
        if (nullptr != ptr) 
            vec_memorys_.push_back(ptr);
        
        return ptr;
    
;

第二个类,能够同时支持内存管理、对象管理

typedef void (*delete_obj_func)(void*);

class XAutoFreeObject : public XAutoFreeMem

private:

    typedef struct object_manager_st
    
        void* obj_this;
        delete_obj_func delete_ptr;
    object_manager_st;

protected:
    template<typename T>
    static void free_object(T* p_this)
    
        delete p_this;
    
    template<typename T>
    static void free_objects(T* p_this)
    
        delete []p_this;
    

protected:
    std::vector<object_manager_st> vec_objects_;

public:
    XAutoFreeObject::XAutoFreeObject() ;

    virtual XAutoFreeObject::~XAutoFreeObject()
    
        //释放对象时,释放管理的对象
        for(auto item : vec_objects_)
            (*item.delete_ptr)(item.obj_this);
        
    

    //对象

    //通过此接口来创建对象
    template<typename T>
    void new_object(T** ppObj) 
    
        object_manager_st stObjMan;
        stObjMan.obj_this = new T;
        if (nullptr != stObjMan.obj_this) 
            //取得函数指针
            stObjMan.delete_ptr =(delete_obj_func) & free_object<T>;
            //保存之
            vec_objects_.push_back(stObjMan);
        
        *ppObj = (T*)(stObjMan.obj_this);
        return;
    

    //通过此接口来创建对象
    template<typename T, typename P>
    void new_object_with_param(T** ppObj, P param)
    
        object_manager_st stObjMan;
        stObjMan.obj_this = new T(param);
        if (nullptr != stObjMan.obj_this) 
            //取得函数指针
            stObjMan.delete_ptr = & free_object<T>;
            //保存之
            vec_objects_.push_back(stObjMan);
        
        *ppObj = (T*)(stObjMan.obj_this);
        return;
    

    //通过此接口来创建对象,这几个接口使用会麻烦一些,使用示例:std::string* pstr = stAutoManager.new_object<std::string> ();
    template<typename T>
    T* new_object() 
    
        object_manager_st stObjMan;
        stObjMan.obj_this = new T;
        if (nullptr != stObjMan.obj_this) 
            //取得函数指针
            stObjMan.delete_ptr =(delete_obj_func) & free_object<T>;
            //保存之
            vec_objects_.push_back(stObjMan);
        
        return (T*)(stObjMan.obj_this);
    

    //通过此接口来创建对象
    template<typename T, typename P>
    T* new_object_with_param(P param)
    
        object_manager_st stObjMan;
        stObjMan.obj_this = new T(param);
        if (nullptr != stObjMan.obj_this) 
            //取得函数指针
            stObjMan.delete_ptr = & free_object<T>;
            //保存之
            vec_objects_.push_back(stObjMan);
        
        return (T*)(stObjMan.obj_this);
    

    //对象数组

    //通过此接口来创建对象数组
    template<typename T>
    void new_objects(T** ppObj, int num) 
    
        object_manager_st stObjMan;
        stObjMan.obj_this = new T[num];
        if (nullptr != stObjMan.obj_this) 
            //取得函数指针
            stObjMan.delete_ptr =(delete_obj_func) & free_objects<T>;
            //保存之
            vec_objects_.push_back(stObjMan);
        
        *ppObj = (T*)(stObjMan.obj_this);
        return;
    

    //通过此接口来创建对象数组
    template<typename T, typename P>
    void new_objects_with_param(T** ppObj, int num, P param)
    
        object_manager_st stObjMan;
        stObjMan.obj_this = new T[num](param);
        if (nullptr != stObjMan.obj_this) 
            //取得函数指针
            stObjMan.delete_ptr = & free_object<T>;
            //保存之
            vec_objects_.push_back(stObjMan);
        
        *ppObj = (T*)(stObjMan.obj_this);
        return;
    

    //通过此接口来创建对象数组
    template<typename T>
    T* new_objects(int num) 
    
        object_manager_st stObjMan;
        stObjMan.obj_this = new T[num];
        if (nullptr != stObjMan.obj_this) 
            //取得函数指针
            stObjMan.delete_ptr =(delete_obj_func) & free_object<T>;
            //保存之
            vec_objects_.push_back(stObjMan);
        
        return (T*)(stObjMan.obj_this);
    

    //通过此接口来创建对象数组
    template<typename T, typename P>
    T* new_objects_with_param(int num, P param)
    
        object_manager_st stObjMan;
        stObjMan.obj_this = new T[num](param);
        if (nullptr != stObjMan.obj_this) 
            //取得函数指针
            stObjMan.delete_ptr = & free_object<T>;
            //保存之
            vec_objects_.push_back(stObjMan);
        
        return (T*)(stObjMan.obj_this);
    
;

调用示例如下:

int main(int argc, char* argv[])

    //cwSL3D_test_sum();//测试能否成功调用所有接口
    XAutoFreeObject stAutoManager;

    char* strMem = (char*)stAutoManager.malloc_mem(100);

    std::string* pstr = stAutoManager.new_object<std::string> ();

    std::string* pstr2 = nullptr;
    stAutoManager.new_object(&pstr2);
    
        std::vector<int>* pvec = nullptr;
        stAutoManager.new_object(&pvec);

        std::vector<int>* pvec2 = nullptr;
        stAutoManager.new_objects(&pvec, 2);
    
    return 0;

 

以上是关于C++动态申请数组问题的主要内容,如果未能解决你的问题,请参考以下文章

C++ new申请二维数组整理

c++中的new()申请二维数组

c++中用new给未知大小的数组分配空间怎么弄?

如何在C++中使用动态三维数组

C与C++申请动态内存空间的异同

C++函数中,两个自动释放内存的动态内存申请类