免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6638 | 回复: 15
打印 上一主题 下一主题

malloc(struct A *)问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-27 20:47 |只看该作者 |倒序浏览
struct Node
{
  int ia;
  int ib;
  char ca;
};

typedef struct Node NODE;
typedef NODE * NODEPTR;

main()
{
  NODEPTR malloc_node_ptr,malloc_nodeptr_ptr;

  malloc_node_ptr = malloc (sizeof (NODE));
  malloc_nodeptr_ptr = malloc (sizeof (NODEPTR));

  printf("sizeof(NODE):%d\n", sizeof(NODE));
  printf("sizeof(NODEPTR):%d\n", sizeof(NODEPTR));
  printf("sizeof(malloc_node_ptr):%d\n", sizeof(malloc_node_ptr));
  printf("sizeof(malloc_nodeptr_ptr):%d\n", sizeof(malloc_nodeptr_ptr));
  printf("sizeof(*malloc_node_ptr):%d\n", sizeof(*malloc_node_ptr));
  printf("sizeof(*malloc_nodeptr_ptr):%d\n", sizeof(*malloc_nodeptr_ptr));

  free(malloc_node_ptr);
  free(malloc_nodeptr_ptr);
}
得到这样的结果
sizeof(NODE):12
sizeof(NODEPTR):4
sizeof(malloc_node_ptr):4
sizeof(malloc_nodeptr_ptr):4
sizeof(*malloc_node_ptr):12
sizeof(*malloc_nodeptr_ptr):12
为什么sizeof(NODE) sizeof(NODEPTR)不同,可是malloc分配的内存确实一样的大小。

论坛徽章:
0
2 [报告]
发表于 2005-04-27 22:29 |只看该作者

malloc(struct A *)问题

NODE是结构体类型,所占字节数为4+4+4=12
NODEPTR本身是指向NODE的指针类型,sizeof(NODEPTR)是求一个指针所占的存储空间大小,在32位机中指针存处在一个字长(32位,4字节)的内存空间中。所以结果是4。

论坛徽章:
0
3 [报告]
发表于 2005-04-28 10:23 |只看该作者

malloc(struct A *)问题

这个明白。

我要说明的问题是:

两个malloc语句不同,可是sizeof却一样。

论坛徽章:
0
4 [报告]
发表于 2005-04-28 11:06 |只看该作者

malloc(struct A *)问题

malloc_node_ptr = malloc (sizeof (NODE));
malloc_nodeptr_ptr = malloc (sizeof (NODEPTR));
这两个都是指针
sizeof(malloc_node_ptr):4
sizeof(malloc_nodeptr_ptr):4
计算的是指针本身的长度,不是指针所指内存的大小,这两个不同才怪

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2005-04-28 11:10 |只看该作者

malloc(struct A *)问题

很简单啊!
程序中
>;>;NODEPTR malloc_node_ptr,malloc_nodeptr_ptr;
malloc_nodeptr_ptr前面少了一个*号

论坛徽章:
0
6 [报告]
发表于 2005-04-28 11:13 |只看该作者

malloc(struct A *)问题

sizeof(NODE):12
sizeof(NODEPTR):4
sizeof(NODE)是一个结构的长度,sizeof(NODEPTR)是一个指针的长度,
不晓得对不?

论坛徽章:
0
7 [报告]
发表于 2005-04-28 11:35 |只看该作者

malloc(struct A *)问题

原帖由 "toorq" 发表:
这个明白。

我要说明的问题是:

两个malloc语句不同,可是sizeof却一样。



sizeof()得不到malloc()请求的存储空间大小,要想知道该大小,就记住你申请的时候是多大就行了。

论坛徽章:
0
8 [报告]
发表于 2005-04-28 11:59 |只看该作者

malloc(struct A *)问题

这三个是等效的
sizeof(NODE)
sizeof(*malloc_node_ptr)
sizeof(*malloc_nodeptr_ptr)

就象 int a
sizeof(int)=sizeof(a)

是这样吗?

论坛徽章:
0
9 [报告]
发表于 2005-04-28 22:35 |只看该作者

malloc(struct A *)问题

原帖由 "kernelxu" 发表:



sizeof()得不到malloc()请求的存储空间大小,要想知道该大小,就记住你申请的时候是多大就行了。


如果这么说malloc只给malloc_nodeptr_ptr= malloc (sizeof (NODEPTR));分配了4个字节的内存,这个显然是不对的。

#include <stdio.h>;
#include <stdlib.h>;

struct listNode {
        char data;
        struct listNode * nextPtr;
};

typedef struct listNode LISTNODE;
typedef LISTNODE * LISTNODEPTR;

void insert (LISTNODEPTR *, char);
char delete (LISTNODEPTR *, char);
int isEmpty (LISTNODEPTR);
void printList (LISTNODEPTR);
void instructions (void);

main()
{
        LISTNODEPTR startPtr = NULL;
        int choice;
        char item;

        instructions ();
        printf ("?";
        scanf ("%d", &choice);

        while ( choice != 3) {
                switch (choice) {
                case 1:
                        printf ("Enter acharacter:";
                        scanf ("\n%c", &item);
                        insert (&startPtr, item);
                        printList (startPtr);
                        break;
                case 2:
                        if (!isEmpty(startPtr)) {
                                printf ("Enter character to be deleted:";
                                scanf ("\n%c", &item);

                                if (delete (&startPtr, item)) {
                                        printf ("%c deleted.\n",item);
                                        printList (startPtr);
                                } else
                                        printf ("%c not found.\n\n", item);
                        } else
                                printf ("List is empty.\n\n";
                        break;
                default:
                        printf ("Invalid choice.\n\n";
                        instructions ();
                        break;
                }
               
                printf ("?";
                scanf ("%d", &choice);
        }

        printf ("End of run. \n";
        return 0;
}

void instructions (void)
{
        printf ("Enter your choice: \n"
                "1 to insert a element into the list. \n"
                "2 to delete an element from the list. \n"
                "3 to end.\n";
}

void insert (LISTNODEPTR *sPtr, char value)
{
        LISTNODEPTR newPtr, previousPtr, currentPtr;

        newPtr = malloc (sizeof (LISTNODEPTR));
        if (newPtr != NULL) {
                newPtr ->; data = value;
                newPtr ->; nextPtr = NULL;

                previousPtr = NULL;
                currentPtr = *sPtr;

                while (currentPtr != NULL && value>;currentPtr->;data) {
                        previousPtr = currentPtr;
                        currentPtr = currentPtr ->; nextPtr;
                }

                if (previousPtr == NULL) {
                        newPtr->; nextPtr = *sPtr;
                        *sPtr = newPtr;
                }else {
                        previousPtr->;nextPtr = newPtr;
                        newPtr ->; nextPtr = currentPtr;
                }
        } else
                printf ("%c not inserted. NO memory available.\n", value);
}

char delete (LISTNODEPTR *sPtr, char value)
{
        LISTNODEPTR previousPtr, currentPtr, tempPtr;

        if(value == (*sPtr) ->; data) {
                tempPtr  = *sPtr;
                *sPtr = (*sPtr) ->; nextPtr;

                free (tempPtr);
                return value;
        } else {
                previousPtr = *sPtr;
                currentPtr = (*sPtr) ->; nextPtr;
       
                while (currentPtr != NULL && currentPtr->;data != value) {
                        previousPtr = currentPtr;
                        currentPtr = currentPtr ->; nextPtr;
                }

                if (currentPtr != NULL) {
                        tempPtr = currentPtr;
                        previousPtr->;nextPtr = currentPtr ->; nextPtr;
                        free (tempPtr);
                        return value;
                }
        }

        return '\0';
}

int isEmpty (LISTNODEPTR sPtr)
{
        return sPtr == NULL;
}

void printList (LISTNODEPTR currentPtr)
{
        if(currentPtr == NULL)
                printf ("List is empty.\n\n";
        else {
                printf ("The list is:\n";
               
                while (currentPtr != NULL) {
                        printf ("%c -->;", currentPtr->;data);
                        currentPtr = currentPtr->;nextPtr;
                }
                printf ("NULL.\n\n");
        }
}

中的malloc,这个程序确实绝对正确的。
也就是说C(GCC3.3,TC2.0)中有个有意思的地方了(我看了ISO C 99,没有对malloc定义),给malloc()一个sizeof(struct) , sizeof (struct *)是一样的。
大家注意看给两个指针的malloc的参数是不同的。

论坛徽章:
0
10 [报告]
发表于 2005-04-29 01:31 |只看该作者

malloc(struct A *)问题

[quote]原帖由 "toorq"]为什么sizeof(NODE) sizeof(NODEPTR)不同,可是malloc分配的内存确实一样的大小。[/quote 发表:


有点儿不明白,你为什么说malloc分配的内存大小是一样的?根据什么啊?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP