Redian新闻
>
iphone 6s plus插上原来旧手机的sim卡激活之后显示no service?
avatar
iphone 6s plus插上原来旧手机的sim卡激活之后显示no service?# Apple - 家有苹果
h*m
1
一般什么时候需要用malloc()呢?一般情况下不都可以直接定义么?比如说:
int *a = malloc(10*sizeof(int));
我直接定义 int a[10]不就行了。其他的数据结构也差不多巴,
谁能给个例子什么时候必须用malloc呢?感觉很麻烦啊,用完了还要free。
avatar
l*h
2
为什么?
avatar
M7
3
你在不知道需要多大的数组的时候,就要用malloc.
比如数组的大小是user输入的一个整数。

【在 h********m 的大作中提到】
: 一般什么时候需要用malloc()呢?一般情况下不都可以直接定义么?比如说:
: int *a = malloc(10*sizeof(int));
: 我直接定义 int a[10]不就行了。其他的数据结构也差不多巴,
: 谁能给个例子什么时候必须用malloc呢?感觉很麻烦啊,用完了还要free。

avatar
l*h
4
搞明白了,原来激活之后旧sim卡不管用了,汗,换上新给的那个就好了
avatar
D*3
5
malloc is dynamic
int a[10] is static
Read more about memory allocation, this is pretty basic question
avatar
h*m
6
哎,非科班的就是缺乏这些基本概念阿,只能东学一点西学一点,没有一个系统的
knowledge。

【在 D*****3 的大作中提到】
: malloc is dynamic
: int a[10] is static
: Read more about memory allocation, this is pretty basic question

avatar
i*d
7
我给个例子吧。。。
再次之前先更正一下。。
int *a = malloc(10*sizeof(int)); 是不对的

int *a = (int *)malloc(10*sizeof(int));
另外,因为malloc分配的内存在堆上,需要手动free。但是a[10]分配的内存在stack上
,会自动被销毁的。。所以,你设想有这么一个函数。。
int *randomArray(int n);
让你生成一个大小为n的array,里面都是随机数,返回。你用啥?

【在 h********m 的大作中提到】
: 一般什么时候需要用malloc()呢?一般情况下不都可以直接定义么?比如说:
: int *a = malloc(10*sizeof(int));
: 我直接定义 int a[10]不就行了。其他的数据结构也差不多巴,
: 谁能给个例子什么时候必须用malloc呢?感觉很麻烦啊,用完了还要free。

avatar
h*m
8
关于这个casting,我刚刚看了wiki:
http://en.wikipedia.org/wiki/Malloc
用不用都行啊,各有优缺点。我写了个test没用,也没报错。

【在 i****d 的大作中提到】
: 我给个例子吧。。。
: 再次之前先更正一下。。
: int *a = malloc(10*sizeof(int)); 是不对的
: 得
: int *a = (int *)malloc(10*sizeof(int));
: 另外,因为malloc分配的内存在堆上,需要手动free。但是a[10]分配的内存在stack上
: ,会自动被销毁的。。所以,你设想有这么一个函数。。
: int *randomArray(int n);
: 让你生成一个大小为n的array,里面都是随机数,返回。你用啥?

avatar
M7
9
这些概念太基本了,如果你一定要用C, 那你还是要补习一下这些东西。
如果不一定要用C, 还是用C++的vector,省去你malloc/free的麻烦。

【在 h********m 的大作中提到】
: 哎,非科班的就是缺乏这些基本概念阿,只能东学一点西学一点,没有一个系统的
: knowledge。

avatar
c*u
10
大哥你这个都算不上科班非科班的区别了。。。。。
我敢说你写过的程序不超过200行

【在 h********m 的大作中提到】
: 哎,非科班的就是缺乏这些基本概念阿,只能东学一点西学一点,没有一个系统的
: knowledge。

avatar
h*m
11
我还是不太清楚,比如说:如果我要动态生成一个size n的什么data type,我就直接
用定义,比如说 datatype[n]不就行了么,为啥要malloc(n*sizeof(datatype))呢?

【在 D*****3 的大作中提到】
: malloc is dynamic
: int a[10] is static
: Read more about memory allocation, this is pretty basic question

avatar
h*m
12
我以前用java写过不少,呵呵。最近才开始学习一些C,结果觉得好麻烦啊 :(

【在 c*******u 的大作中提到】
: 大哥你这个都算不上科班非科班的区别了。。。。。
: 我敢说你写过的程序不超过200行

avatar
D*3
13

Try to do it. Then come back again.
Don't tell me you don't even have a c compiler

【在 h********m 的大作中提到】
: 我还是不太清楚,比如说:如果我要动态生成一个size n的什么data type,我就直接
: 用定义,比如说 datatype[n]不就行了么,为啥要malloc(n*sizeof(datatype))呢?

avatar
M7
14
只有当n是const的时候,datatype[n]才行。
对于一般变量n, datatype[n]不能通过编译,因为compiler对n的大小一无所知,所以无
法在stack里生成数组。
malloc的数组在heap里.

【在 h********m 的大作中提到】
: 我还是不太清楚,比如说:如果我要动态生成一个size n的什么data type,我就直接
: 用定义,比如说 datatype[n]不就行了么,为啥要malloc(n*sizeof(datatype))呢?

avatar
h*m
15
果然是这样,我试了一个简单的程序:
void main()
{
int i, n;
int a[n];
n=3;
for(i=0; ia[i] = i;
printf("%d, ", a[i]);
}
}
编译没报错,但是运行的时候segamentation fault.

以无

【在 M7 的大作中提到】
: 只有当n是const的时候,datatype[n]才行。
: 对于一般变量n, datatype[n]不能通过编译,因为compiler对n的大小一无所知,所以无
: 法在stack里生成数组。
: malloc的数组在heap里.

avatar
M7
16
gcc竟让这个通过编译,有没有高人解释一下。
visual studio里通不过编译。

【在 h********m 的大作中提到】
: 果然是这样,我试了一个简单的程序:
: void main()
: {
: int i, n;
: int a[n];
: n=3;
: for(i=0; i: a[i] = i;
: printf("%d, ", a[i]);
: }

avatar
r*e
17
http://www.gnu.org/s/libc/manual/html_node/Memory-Allocation-an
In GNU C, the size of the automatic storage can be an expression that varies
. In other C implementations, it must be a constant.

【在 M7 的大作中提到】
: gcc竟让这个通过编译,有没有高人解释一下。
: visual studio里通不过编译。

avatar
M7
18
interesting.
把n=3放到int a[n]前面,连运行都没问题了。

varies

【在 r*******e 的大作中提到】
: http://www.gnu.org/s/libc/manual/html_node/Memory-Allocation-an
: In GNU C, the size of the automatic storage can be an expression that varies
: . In other C implementations, it must be a constant.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。