malloc(size)

begintransaction 2011-10-02 10:16:14
这个是返回一个指针,还是返回一个结点
...全文
239 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
AnYidan 2011-10-03
  • 打赏
  • 举报
回复
void *malloc(size_t size)
malloc returns a pointer to space for an object of size size, or NULL if the request cannot be satisfied. The space is uninitialized.

void * 通用型指针,可以cast 为各种指针
cao_julians 2011-10-03
  • 打赏
  • 举报
回复
显然LZ提出的问题涉及两个层面,一个是问题层(链表或树结构中的结点),一个是实现层(语言中的指针),硬要用一个技术术语去描述两个层面的东西是不现实的.
lida2003 2011-10-03
  • 打赏
  • 举报
回复
请参考:
#include <stdlib.h>

void *malloc(size_t size);

malloc() allocates size bytes and returns a pointer to the allocated
memory. The memory is not cleared. If size is 0, then malloc()
returns either NULL, or a unique pointer value that can later be suc‐
cessfully passed to free().


the value returned is a pointer to the allo‐
cated memory, which is suitably aligned for any kind of variable, or
NULL if the request fails.
AndyZhang 2011-10-03
  • 打赏
  • 举报
回复
返回的是void *类型的指针也就是地址,你可以转换为你想要的类型,其实这也是告诉系统要为你开辟多大的空间
modicum_lf 2011-10-02
  • 打赏
  • 举报
回复

返回 void* !

modicum_ck 2011-10-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 veron_04 的回复:]
返回一个指针

你可以强制的转换其为你所需要的类型的指针,如:

int intPtr;
intPtr=(int * )malloc(100*sizeof(int)); //分配一个大小为100的int类型控件给intPtr指针
[/Quote]

++
Athenacle_ 2011-10-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 begintransaction 的回复:]

引用 2 楼 veron_04 的回复:
返回一个指针

你可以强制的转换其为你所需要的类型的指针,如:

int intPtr;
intPtr=(int * )malloc(100*sizeof(int)); //分配一个大小为100的int类型控件给intPtr指针

例如:p=malloc(size);
返回了一个指针p,怎么可以用p->data访问其结构里的data呢?……
[/Quote]

在这种情况下,malloc返回的指针可以叫成一个节点,这两者是一样的。
但malloc实质上返回的是一个指针
Qyee16 2011-10-02
  • 打赏
  • 举报
回复
p=malloc(size);

p是指向其申请size大小的空间地址,比如定义了class A{int num;}
p = (A*)malloc(sizeof(A); p指向其A对象的指针,因为malloc返回void*,所以进行了类型转换(A*)malloc(sizeof(A))

此时,可以获取num值 p->num
begintransaction 2011-10-02
  • 打赏
  • 举报
回复
我这是在数据结构导论里看到的,用的是类C语言
begintransaction 2011-10-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 veron_04 的回复:]
返回一个指针

你可以强制的转换其为你所需要的类型的指针,如:

int intPtr;
intPtr=(int * )malloc(100*sizeof(int)); //分配一个大小为100的int类型控件给intPtr指针
[/Quote]
例如:p=malloc(size);
返回了一个指针p,怎么可以用p->data访问其结构里的data呢?p只是一个指针,怎么可以调用结点里的data呢,p因和结点->next相等吧
尘缘udbwcso 2011-10-02
  • 打赏
  • 举报
回复
malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.

如果分配成功则返回指向被分配内存的指针,否则返回NULL。

贝隆 2011-10-02
  • 打赏
  • 举报
回复
返回一个指针

你可以强制的转换其为你所需要的类型的指针,如:

int intPtr;
intPtr=(int * )malloc(100*sizeof(int)); //分配一个大小为100的int类型控件给intPtr指针
Jake443403168 2011-10-02
  • 打赏
  • 举报
回复
void *malloc( size_t size );

很好知道的把,查查MSDN都行

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧