动态地和“正确地”动态地分配内存 [英] Dynamically and "correctly" allocating memory

查看:85
本文介绍了动态地和“正确地”动态地分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想为没有最大元素数量的链接

列表(或复杂结构等)动态分配内存。

我能做的一切记住是我必须将已分配的变量指向

指向主程序中的链表,并且我必须有一些

类的初始化列表的方法(加上其他方法来支付
操纵列表,当然)。 "正确"意味着应该有

指针,可能还有双指针或三指针指针。这个很久以前我已经做了很多,但是我根本不记得如何做到这一点......任何

想法或链接来帮助我立即(和是的) ,我知道malloc和

realloc基础知识)? Thx提前。

For example I would like to dynamically allocate memory for linked
list (or complex struct etc.) that has not maximum number of elements.
All that I can remember is that I have to have allocated variable to
point to the linked list in main program and that I have to have some
kind of a method that initializes the list (plus other methods to
manipulate list, of course). "Correctly" means that there should be
pointers involved, and possibly double or triple pointers. I have do
this long ago, but I just can''t remember how to do this neatly... Any
ideas or links to help me rembember (and yes, I know malloc and
realloc basics)? Thx in advance.

推荐答案

请将主题留在邮件正文中

主题:动态地和正确地分配内存


8月29日,12:23,filia& sofia< in_tyran ... @ hotmail.comwrote:
please leave the subject in the body of your message as well
Subject: "Dynamically and "correctly" allocating memory"

On 29 Aug, 12:23, filia&sofia <in_tyran...@hotmail.comwrote:

例如,我想为没有最大元素数的链接

列表(或复杂结构等)动态分配内存。
For example I would like to dynamically allocate memory for linked
list (or complex struct etc.) that has not maximum number of elements.



或许在数据结构书中查找链表。或者在线。

Wiki可能有一个很好的描述。

perhaps look up linked lists in a data structures book. Or online.
Wiki probably has a good description.


所有我能记住的是我必须将变量分配给

指向主程序中的链表
All that I can remember is that I have to have allocated variable to
point to the linked list in main program



no。你必须根链接列表在哪里,但它没有

成为主程序。它可以在库中实现链接列表或全局数据的
。它可能是静态的或动态的。

no. You have to "root" the linked list somewhere, but it doesn''t have
to be the main program. It could be inside the library that implements
the linked list or global data. It could be static or dynamic.


并且我必须有一些

类的初始化列表的方法(加上其他方法来支付
操纵列表,当然)。 "正确"意味着应该有

指针,
and that I have to have some
kind of a method that initializes the list (plus other methods to
manipulate list, of course). "Correctly" means that there should be
pointers involved,



我也不确定正确是什么?意味着指针。虽然我会这样做。

方式。

I''m nor sure how "correctly" implies pointers. Though I''d do it that
way.


以及可能是双指针或三指针。
and possibly double or triple pointers.



为什么?

why?


我很久以前就已经做过
,但是我根本不记得如何做到这一点......任何

想法或链接,以帮助我rembember(是的,我知道malloc和

realloc基础知识) ? Thx提前。
I have do
this long ago, but I just can''t remember how to do this neatly... Any
ideas or links to help me rembember (and yes, I know malloc and
realloc basics)? Thx in advance.



#include< stdlib.h>

#include< assert.h>


typedef struct node_tag

{

char * data; / *你想要什么数据* /

struct node_tag * next;

}节点;


typedef struct

{

节点*头;

节点*尾部;

}列表;


int main(void)

{

List list = {NULL,NULL};

Node * node;


node = create_node(" red");

add(& list,node);

assert(find(& list) ; red));

断言(!find(& list" blue"));


node = create_node(" blue") ;

add(& list,node);

assert(find(& list" red"));

assert( find(& list" blue"));


node = remove(& list," red");

destroy_node(node) ;

断言(!find(& list" red"));

断言(找(& list" blue"));


reurn 0;

}


现在只执行上述功能。 assert()如果其

参数的计算结果为false(零)则停止。

-

Nick Keighley

我当然会在1999年12月31日坐飞机。

你们科学家们不会愚蠢地建造不那么美好的东西。 $ b work。

除了计算机与飞机有什么关系吗?

(Kerry Nov 1998)

#include <stdlib.h>
#include <assert.h>

typedef struct node_tag
{
char* data; /* whatever data you want */
struct node_tag *next;
} Node;

typedef struct
{
Node *head;
Node *tail;
} List;

int main (void)
{
List list = {NULL, NULL};
Node *node;

node = create_node("red");
add (&list, node);
assert (find (&list "red"));
assert (!find (&list "blue"));

node = create_node("blue");
add (&list, node);
assert (find (&list "red"));
assert (find (&list "blue"));

node = remove (&list, "red");
destroy_node (node);
assert (!find (&list "red"));
assert (find (&list "blue"));

reurn 0;
}

now just implement the functions above. assert() halts if its
argument evaluates to false (zero).
--
Nick Keighley
"Of course I''m going to be in an aeroplane on 31st December 1999.
You scientists wouldn''t be stupid enough to build things that don''t
work.
Besides what have computers got to do with aeroplanes anyway?"
(Kerry Nov 1998)




" filia& sofia" < in ********* @ hotmail.comwrote in message

news:11 ********************* *@r29g2000hsg.googlegr oups.com ...

"filia&sofia" <in*********@hotmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...

例如我想为链接的

列表(或复杂的)动态分配内存结构等)没有最大数量的元素。

所有我记得的是我必须分配变量

指向主程序中的链表而且我必须要有一些初始化列表的方法(加上其他方法来支持
操作列表,当然)。 "正确"意味着应该有

指针,可能还有双指针或三指针指针。这个很久以前我已经做了很多,但是我根本不记得如何做到这一点......任何

想法或链接来帮助我立即(和是的) ,我知道malloc和

realloc基础知识)? Thx提前。
For example I would like to dynamically allocate memory for linked
list (or complex struct etc.) that has not maximum number of elements.
All that I can remember is that I have to have allocated variable to
point to the linked list in main program and that I have to have some
kind of a method that initializes the list (plus other methods to
manipulate list, of course). "Correctly" means that there should be
pointers involved, and possibly double or triple pointers. I have do
this long ago, but I just can''t remember how to do this neatly... Any
ideas or links to help me rembember (and yes, I know malloc and
realloc basics)? Thx in advance.



typedef struct node

{

struct node * prev;

struct node * next;

void * ptr; / *挂钩以挂起任意数据* /

} NODE;


从一个节点开始,其中prev和next都为零。这是一个

项目的清单。


NODE * list = malloc(sizeof(NODE));

list- > next = 0;

list-> prev = 0;

list-> ptr = strdup(" item one");

(strdup()是一个复制字符串的非标准函数。如果你没有
有它,写一个并调用它来mystrdup以避免名字冲突)。


添加


NODE * newnode = malloc(sizeof(NODE));

newnode-> ptr = stdup (第二项);

newnode-> next = list;

list-> prev = newnode;

list = newnode;


打印清单


void printlist(NDOE * head)

{

while(head)

{

printf(" ***%s *** \ n",(char *)head-> ptr);

head = head-> next;

}

}


删除更难。


void del(NODE *节点)

{

NODE * prev = node-> pre v;

NODE * next = node-> next;


if(prev)

prev-> next =下一个;

if(next)

next-> prev = prev;

free(node-> ptr);

免费(节点);

}

-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

typedef struct node
{
struct node *prev;
struct node *next;
void *ptr; /* hook to hang arbitrary data off */
} NODE;

Start with a node where prev and next are both zero. This is a list of one
item.

NODE *list = malloc(sizeof(NODE));
list->next = 0;
list->prev = 0;
list->ptr = strdup("item one");
(strdup() is a non-standard function that duplicates a string. If you don''t
have it, write one and call it mystrdup to avoid name clashes).

To add

NODE *newnode = malloc(sizeof(NODE));
newnode->ptr = stdup("item two");
newnode->next = list;
list->prev = newnode;
list = newnode;

to print your list

void printlist(NDOE *head)
{
while(head)
{
printf("***%s***\n", (char *) head->ptr);
head = head->next;
}
}

Deletion is harder.

void del(NODE *node)
{
NODE *prev = node->prev;
NODE *next = node->next;

if(prev)
prev->next = next;
if(next)
next->prev = prev;
free(node->ptr);
free(node);
}
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


filia& sofia skrev:
filia&sofia skrev:

例如我想动态分配内存链接

列表(或复杂的结构等)没有最大数量的元素。

所有我能记住的是我必须将变量分配给

指向主程序中的链表,并且我必须有一些

类的方法来初始化列表(加上其他方法来实现
操作列表,当然)。 "正确"意味着应该有

指针,可能还有双指针或三指针指针。这个很久以前我已经做了很多,但是我根本不记得如何做到这一点......任何

想法或链接来帮助我立即(和是的) ,我知道malloc和

realloc基础知识)? Thx提前。
For example I would like to dynamically allocate memory for linked
list (or complex struct etc.) that has not maximum number of elements.
All that I can remember is that I have to have allocated variable to
point to the linked list in main program and that I have to have some
kind of a method that initializes the list (plus other methods to
manipulate list, of course). "Correctly" means that there should be
pointers involved, and possibly double or triple pointers. I have do
this long ago, but I just can''t remember how to do this neatly... Any
ideas or links to help me rembember (and yes, I know malloc and
realloc basics)? Thx in advance.



以下是一个例子。

八月

#include< stdio.h>

#include< stdlib.h>


typedef struct List {

int item;

struct List *下一个;

}列表;


int main(无效)

{

列表* p,* q;

int i;


/ *初始化列表* /

p = NULL;

for(i = 0; i< 10; i ++){

q = malloc(sizeof * q); q-> item = i; q-> next = p;

p = q;

}


/ *打印清单* /

q = p;

while(q!= NULL){

printf("%d \ n",q-> item);

q = q-> next;

}


/ *免费清单* /

while(p!= NULL){

q = p-> next;

free(p);

p = q;

}


返回0;

}

Below is a an example.
August
#include <stdio.h>
#include <stdlib.h>

typedef struct List {
int item;
struct List *next;
} List;

int main(void)
{
List *p, *q;
int i;

/* initialize the list */
p = NULL;
for (i = 0; i < 10; i++) {
q = malloc(sizeof *q); q->item = i; q->next = p;
p = q;
}

/* print the list */
q = p;
while (q != NULL) {
printf("%d\n", q->item);
q = q->next;
}

/* free the list */
while (p != NULL) {
q = p->next;
free(p);
p = q;
}

return 0;
}


这篇关于动态地和“正确地”动态地分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆