【总结】动态数组小结

比较好介绍动态和静态数组的文章:

https://blog.csdn.net/u013921430/article/details/79514972

https://blog.csdn.net/u013921430/article/details/79601429

c++创建动态数组方法的文章(new和vector):

https://blog.csdn.net/u013921430/article/details/79601429

https://blog.csdn.net/duan19920101/article/details/50991897

 

C arrays

template class std::array

template class std::vector

Description

 

 

 

Fixed size

Y

Y (size is also part of the type).

N

Shallow Copy (a copy of the object will refer to the same data)

N

N

N

Bounds checking

N

Y (only when using at() function)

Y (only when using the at() function)

Controls the lifecycle of the underlaying array

N

Y

Y

Array Memory

 

Inlined in the class (not suitable to keep in stack variables).

Allocated from the heap.

When to use

to be avoided

The size is constant and known at compile time.  Avoid to use as local varible (allocated from stack).

 

Can be passed by value (if needed)

N/A

Not suitable

Not suitable, can be returned by value.

Use std::vector when:

  • The container size is big. The array allocates its memory inline, so a datatype like this std::array<C, BIG_NUMBER> cannot be used as a local variable in a function (allocated from the stack).
  • The size is not known at compile time or is not fixed.

Use std::array if:

  • The size is known at compile time and small.
  • Static initialized containers with fixed size. Static initialized vectors are allocated in the global initialized space and copied to the heap (double allocation).
  • From <https://blog.csdn.net/thinkerleo1997/article/details/80415059>
  • 简单来说,std::array除了有传统数组支持随机访问、效率高、存储大小固定等特点外,还支持迭代器访问、获取容量、获得原始指针等高级功能。而且它还不会退化成指针T *给开发人员造成困惑。
  • https://blog.csdn.net/hddryjv/article/details/86756987
  • 1.array相比于数组最大的优势就是可以将一个array对象赋值给另一个array对象
  • 2.不退化
  • 3.使用at(),将在运行期间捕获非法索引的,默认将程序中断。(防止越界)
  • 4.data()返回T* (不会隐式转成指针(要指针请显式调用 data() ))  https://en.cppreference.com/w/cpp/container/array
  • 5.可以方便地按值传递、按值返回、赋值。
  • 另外 C++14~C++17 中 std::array 逐渐变得比内建数组更适合配合 constexpr 。
  • std::array 基本上是,也只不过是,把内建数组设计上的诡异之处堵上的东西。

c语言:

【原因】

很多人在编写C语言代码的时候很少使用动态数组,不管什么情况下通通使用静态数组的方法来解决,在当初学习C语言的时候我就是一个典型的例子,但是现在发现这是一个相当不好的习惯,甚至可能导致编写的程序出现一些致命的错误。尤其对于搞嵌入式的人来所,嵌入式系统的内存是宝贵的,内存是否高效率的使用往往意味着嵌入式设备是否高质量和高性能,所以高效的使用内存对我们来说是很重要的。那么我们在自己编写C语言代码的时候就应该学会使用动态数组,这也就是我这篇博客要给大家讲的,我尽我所能的用一些简单的代码来讲解动态数组,希望我所讲的对你有所帮助。

那么我们首先来看看什么是动态数组,动态数组是相对于静态数组而言,从“动”字我们也可以看出它的灵活性,静态数组的长度是预先定义好的,在整个程序中,一旦给定大小后就无法改变。而动态数组则不然,它可以随程序需要而重新指定大小。动态数组的内存空间是从堆动态分配的。是通过执行代码而为其分配存储空间。当程序执行到我们编写的分配语句时,才为其分配。对于静态数组,其创建非常方便,使用完也无需释放,要引用也简单,但是创建后无法改变其大小是其致命弱点!对于动态数组,其创建麻烦,使用完必须由程序员自己释放,否则将会引起内存泄露。但其使用非常灵活,能根据程序需要动态分配大小。所以相对于静态数组的来说我们对于使用动态数组有很大的自由度。

在创建动态数组的过程中我们要遵循一个原则,那就是在创建的时候从外层往里层,逐层创建;而释放的时候从里层往外层,逐层释放。这个话你读了可能理解并不深刻,不过不要急,接下来我们看看两段代码。
一维动态数组的创建(base c):


#include <stdio.h>

#include <stdlib.h>

int main()

{

int n1,i;

int *array;

printf("请输入所要创建的一维动态数组的长度:");

scanf("%d",&n1);

array=(int*)calloc(n1,sizeof(int));

for(i=0;i<n1;i++)

{

 printf("%d\t",array[i]);

}

printf("\n");

for(i=0;i<n1;i++)

{

 array[i]=i+1;

 printf("%d\t",array[i]);

}

 free(array);//释放第一维指针 

return 0;

}

二维动态数组的创建:


#include <iostream>

#include <stdlib.h>

using namespace std;

 

int main(){

	int num1,num2;

	cout<<"请输入动态二维数组的第一个维度:";

	cin>>num1;

	cout<<"请输入动态二维数组的第二个维度:";

	cin>>num2;

	int **array = (int **)calloc(num1,sizeof(int));

	for(int i=0;i<num1;i++) {

		array[i] = (int*)calloc(num2,sizeof(int));

	}

	for(int i=0;i<num1;i++){

		for(int j=0;j<num2;j++){

			array[i][j] =i*num2+j+1;

			printf("%d\t",array[i][j]);

		}

		cout<<endl;

	}

	for(int i=0;i<num1;i++)	free(array[i]);

	free(array);

	return 0;

}

 

有了上面的代码我们再来说动态数组的建立就简单了,以二维为例,先说创建,还记得我们上面说的创建的原则嘛:从外层往里层,逐层创建。

array=(int**)malloc(n1*sizeof(int*)); //第一维

以上是我们创建二维动态数组的最外层,创建好了最外层那么我们接下来就是要创建次外层了。这里使用了二级指针。

array[i]=(int*)malloc(n2* sizeof(int));//第二维

在创建次外层的过程中我们使用了一个for语句,千万别忘了使用for循环语句,这是绝大多数人的一个易错点。

创建好了接下来我们该讲到释放了,而释放的时候从里层往外层,逐层释放。刚刚与我们上面的创建相反,在以上代码中我们首先使用了下面一个for循环来释放里层。

for(i=0;i<n1;i++) 

free(array[i]);//释放第二维指针 
}

在通过以下语句来释放外层。
free(array);//释放第一维指针

一维数组的扩展或删减


#include <stdio.h>

#include <stdlib.h>

int main()

{

int*n,*p;

int i,n1,n2;

printf("请输入所要创建的动态数组的长度:");

scanf("%d",&n1); 

n=(int*)calloc(n1,sizeof(int));

printf("请输入所要扩展的动态数组的长度:");

scanf("%d",&n2); 

p=(int*)realloc(n,(n2)*sizeof(int));//动态扩充数组

for(i=0;i<n2;i++)

{

p[i]=i+1;

if(i%5==0)

printf("\n");

printf("%d\t",p[i]);

}

free(p);

return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值