C语言中的动态字符串列表 [英] Dynamic lists of strings in C

查看:76
本文介绍了C语言中的动态字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我非常喜欢Perl和Python的灵活列表,如@a(Perl)和

a [](Python),你在哪里可以很容易地存储很多字符串甚至整个

文本文件。


现在我不是C-professional,只是一个业余爱好程序员试着教它

我自己。没有这些列表我发现C很难(和相应的

字符串函数)。

慢慢进入C字符串,我想,如果我拿一个C字符串怎么办?并且

将其视为一个列表,元素用''\ n''分隔?毕竟它只是在内存中逐字节存储并且关闭''\ 0''的数据。所以这些

字符串可能会变得很长。

然后我根据这个想法编写了一些函数。他们工作了,但我不得不为每个列表操作的整个列表提供
realloc()内存。所以这可能会更快完成
。有人告诉我,我可以用链表来试试。

我读到了这个并重写了这些功能。令我惊讶的是结构

这比字符串版本更容易。

无论如何,下面我发布了我做的一个例子(我保证,如果代码获得比这更长的时间,我会把它作为一个文件下载)。它只定义了

一个或两个列表并打印一些结果,演示列表函数。


请查看main()。随着那里的评论,它应该给你一个

想法我想要的东西。


嗯,你怎么看待它(除了我铸造malloc():)),那是

你怎么看待我的代码,还有一般的想法?


我知道,其他人们之前尝试过类似的东西:

http:// jengelh.hopto.org/f/libHX/
http ://mij.oltrelinux.com/devel/simclist/
https://sourceforge.net/project/show...group_id=97342
http://www.pronix.de/comment/site-10...06/site-1.html


但是为什么例如这个从未进入过C sta ndard库

像std :: vector必须在C ++中使用STL?


见到你


H. br />

/ ************************************** ************ ********************* /

/ *

list.c:提供字符串的链接列表。


编译:

gcc -W -Wall -pedantic -ansi -o listexample list.c


由hlubenow撰写(电子邮件: hl ******* @ gmx。 net ),(C)2007。许可证:LGPL。


该程序是免费软件;你可以根据自由软件基金会发布的GNU图书馆通用公共许可证的条款重新发布它和/或修改它的b
$ b;

许可证的第2版,或(根据您的选择)任何更高版本。


此程序的分发是希望它有用,

但没有任何保证;甚至没有暗示的保证

适销性或特定用途的适用性。有关更多详细信息,请参阅

GNU通用公共许可证。


您应该已收到GNU Library General Public的副本

许可证以及此计划;如果没有,请写信给

自由软件基金会,

59 Temple Place - Suite 330,Boston,MA 02111-1307,USA。

* /


#include< stdio.h>

#include< stdlib.h>

# include< string.h>


struct list

{

char * content;

struct list * next;

};


char * strMalloc(unsigned int s);

char * strRealloc(char * a,unsigned int s);

struct list * listMalloc(unsigned int s);

struct list * newList(void);

struct list * listUnshift(struct list * head,char * b);

struct list * listAssign(struct list * head,int pos,char * b);

struct list * listAppend(struct list * head,char * b);

struct list * listInsert(struct list * head,int pos,char * b);

int countList( struct list * head);

char * getElement(struct list * head,int pos);

struct list * deleteElement(struct list * head,int pos);

struct list * deleteElements(struct list * head,int start,int end);

struct list * strToList(char * a,char * b);

char * listToStr(struct list * head,char * joinstr);

int printList(struct list * head);

struct list * clearList(struct list * head);

int main(void)

{

struct list * a;

struct list * s;

int count;

char * elem;

char * j;

char * x;

int i;


/ *好的,我们走吧:* /


/ *创建清单* /

a = newList();


/ *填写* /

a = listAppend(a," apple");

a = listAppend(a," banana");

a = listAppend(a," cherry");

a = listAppend(a," strawberry");


/ *算上它* /

count = countList(a) ;


printf(\ n列表现在有%d个元素:\ n,coun t);


/ *从中获取单个元素* /

for(i = 0;我<计数; i ++)

{

elem = getElement(a,i);

printf("%d:%s \ n",我,elem);

免费(elem);

}


/ *直接定义元素* /

a = listAssign(a,1," pear");


puts(" \ nList更改:");

printList(a);


/ *甚至超出列表的边界* /

a = listAssign(a,5," lemon");


puts(" \ nList再次更改:");

printList(a);


count = countList(a);

printf(列表中有%d个元素now.\\\
\ n,计数);


/ *你也可以从列表中插入(listInsert())和删除元素

(deleteElements())* /


/ *此外字符串可以是拆分并以列表形式返回:* /


s = strToList(将我拆分为部分,");


count = countList(s);

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

{

elem = getElement(s,i);

printf("%d:%s \ n,我,elem);

免费(elem);

}


s = clearList(s);


/ *并且列表可以加入一个字符串:* /

a = listAssign(a,4," mango");

j = listToStr(a," ");


printf(" \ n字符串:%s \ n",j);


免费(x );

免费(j);


/ *如果你不再需要这个名单,请致电:* /

a = clearList(a);


返回0;

}

char * strMalloc(unsigned int s)

{

/ *为字符串分配内存,测试分配是否已成功获得
。 * /


char * a;

if((a =(char *)malloc(s * sizeof(char)))== NULL)

{

puts(错误分配内存。);

退出(1);

}


返回a;

}

char * strRealloc(char * a,unsigned int s)

{

/ *重新分配字符串的内存,测试重新分配是否已成功获得
。 * /


if((a =(char *)realloc(a,s * sizeof(char)))== NULL)

{

puts(重新分配内存时出错。);

退出(1);

}


返回a;

}

struct list * listMalloc(unsigned int s)

{

/ *为列表分配内存,测试分配是否已成功获得
。 * /


struct list * a;

if((a =(struct list *)malloc(s * sizeof(struct list)))== NULL)

{

puts(Error allocation the list-memory。);

exit(1);

}


返回a;

}

/ *列表功能。 * /


struct list * newList(void)

{

返回NULL;

}


struct list * listUnshift(struct list * head,char * b)

{

/ *在元素处插入一个元素列表的开头,比如

Perl'的'unshift()"。第一个元素必须以这种方式放入

列表中。 listAppend()和listAssign()自动关注

。 * /


struct list * c = listMalloc(1);

c-> content = strMalloc(strlen(b)+ 1);

strcpy(c-> content,b);

c-> next = head;

返回c;

}

struct list * listAssign(struct list * head,int pos,char * b)

{

/ *让你定义或者重新定义list-elements。

如果pos大于列表,则扩展列表并附加新的

元素。 * /


int listlen;

int i;

struct list * a;


if(pos< 0)

{

puts(List index out of range。);

退出(1);

}


if(head == NULL)

{

head = listUnshift(head,b);

返回头;

}


listlen = countList(head);


if(pos> = listlen)

{

for(i = 0; i< pos - listlen; i ++)< br $>
{

head = listAppend(head,"");

}


head = listAppend(head,b);

返回头;

}


a = head;


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

{

a = a-> next;

}


a-> content = strRealloc(a-> content,strlen(b)+ 1);

strcpy(a-> content ,b);

返回头;

}

struct list * listAppend(struct list * head,char * b)

{

struct list * a;

struct list * c;


if(head == NULL)

{

head = listUnshift(head,b);

返回头;

}


c = listMalloc(1);

c-> content = strMalloc(strlen(b)+ 1);

strcpy(c-> content,b );


a = head;


while(a-> next)

{

a = a-> next;

}

a-> next = c;

c-> next = NULL;

返回头;

}

struct list * listInsert(struct list * head,int pos,char * b)

{

/ *将新元素插入扩展列表的列表中。 * /


int listlen;

int i;

struct list * a;

struct list * c;


if(head == NULL || pos == 0)

{

head = listUnshift( head,b);

返回头;

}


listlen = countList(head);


if(pos> = listlen)

{

puts(List index out of range。);

退出(1);

}


c = listMalloc(1);

c-> content = strMalloc (strlen(b)+ 1);

strcpy(c-> content,b);


a = head;

for(i = 0; i< pos - 1; i ++)

{

a = a-> next;

}


c-> next = a-> next;

a-> next = c;

返回头;

}

int countList(struct list * head)

{

/ *返回列表元素的数量。

也在内部使用。 * /


int x = 0;


if(head == NULL)

{

返回x;

}


而(head-> next)

{

x ++;

head = head-> next;

}

x ++;

返回x;

}

char * getElement(struct list * head,int pos)

{

/ *返回列表位置pos处的元素。 * /


int i;

char * a;

int listlen = countList(head);


if(pos> = listlen)

{

puts(List index out of range。);

退出(1);

}


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

{

head = head-> next;

}


a = strMalloc(strlen(head-> content) + 1);

strcpy(a,head->内容);

返回a;

}

struct list * deleteElement(struct list * head,int pos)

{

struct list * a;

struct list * b; < br $>
struct list * c;

int i;

int length = countList(head);


if(pos> = length || pos< 0)

{

puts(List index out of range。);

退出(1);

}


if(length< = 1)

{

如果(长度== 1)

{

免费(头);

}

返回n NULL;

}


a = head;

b = a-> next;


if(length == 2)

{

a-> next = NULL;

free(b);

返回头;

}


for(i = 0;我< pos - 1; i ++)

{

a = a-> next;

}


b = a- >下一个;

c = b->下一个;


a-> next = c;

free(b );


返回头;

}

struct list * deleteElements(struct list * head,int start,int end)

{

/ *删除从位置开始开始的元素定位

" end"从列表中。


如果-1作为结束传递,则列表将被删除

从位置开始"到了最后。 * /


int i;

int length = countList(head);


if(start> = length || end> = length

|| start< 0 || end< -1)

{

puts( 列表索引超出范围。;

退出(1);

}


if(start end )

{

puts(无效值。);

退出(1);

}


if(end == -1)

{

end = length - 1;

}


for(i = start; i< = end; i ++)

{

head = deleteElement(开头);

}


返回头;

}

struct list * strToList (char * a,char * b)

{

/ *在b处分割一个字符串并将结果

转换为列表。 " listUnshift()"而不是listAppend()而不是

用于速度原因(棘手)。 * /


struct list * head;

char * c;

int lenc;

int lenb;

int i;

int u;

int x;


if( strstr(a,b)== NULL)

{

puts(Splitpart not in string to split!);

返回NULL;

}


head = NULL;


c = strMalloc(strlen(a)+ 1) ;

strcpy(c,a);


lenc = strlen(c);

lenb = strlen(b);


for(i = lenc - 1; i> = 0; i--)

{

x = 0;


if(i> = lenb - 1&& *(c + i)== *(b + lenb - 1))

{

for(u = 0; u< lenb; u ++)

{

if(*(c + i - lenb + 1) + u)== *(b + u))

{

x ++;

}

else

{

休息;

}

}


if(b) x == lenb)

{

*(c + i - lenb + 1)=''\ 0'';

if (我!= lenc - 1)

{

head = listUnshift(head,c + i + 1);

}

}

}

}


head = listUnshift(head,c);


免费(c);


返回头;

}

char * listToStr(struct list * head,char * joinstr)

{

/ *将列表加入单个字符串,将

list-elements与joinstr连接起来。 * /


int size;

char * b;

struct list * a = head;


while(a-> next!= NULL)

{

size + = strlen(a-> content);

size + = strlen(joinstr);

a = a-> next;

}


size + = strlen(a-> content);

size ++;


b = strMalloc(size);

strcpy( b,";


a = head;


while(a-> next!= NULL)

{

strcat(b,a-> content);

strcat(b,joinstr);

a = a- >下一个;

}


strcat(b,a->内容);

返回b;

}

int printList(struct list * head)

{

while(head-> next!= NULL)

{

if(*(head-> content + strlen(head-> content) - 1)!=''\ n'')

{

put(head-> content);

}

else

{

printf("%s",他ad-> content);

}


head = head-> next;

}

if(*(head-> content + strlen(head-> content) - 1)!=''\ n'')

{

puts(head-> content);

}

else

{

printf("%) s",head-> content);

}


返回0;

}

struct list * clearList(struct list * head)

{

/ *如果你不再需要你的名单,你应该打电话给

这可以释放每个元素的结构实例的内存。 * /


结构列表* a;

结构列表** b;

int listlen;

int i;


listlen = countList(head);


/ *创建" listlen"指向列表的每个元素(结构)的指针。 * /


if((b =(struct list **)malloc(listlen * sizeof(struct list *)))==

NULL)

{

puts(错误分配内存。);

退出(1);

}


a = head;


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

{

b [i] = a;

a = a-> next;

}


for (i = listlen - 1; i == 0; i--)

{

if(i 0)

{

b [i - 1] - > next = NULL;

}

免费(b [i]);

}


免费(b);


返回NULL;

}

/ ******************* * ********************* /

Hello,

I really like Perl and Python for their flexible lists like @a (Perl) and
a[] (Python), where you can easily store lots of strings or even a whole
text-file.

Now I''m not a C-professional, just a hobby-programmer trying to teach it
myself. I found C rather difficult without those lists (and corresponding
string-functions).
Slowly getting into C-strings, I thought, what if I take a C-string and
treat it as a list, with the elements separated by ''\n'' ? After all it''s
just data stored byte by byte in memory with a closing ''\0''. So these
strings could become very long.
Then I wrote some functions based on this idea. They worked, but I had to
realloc() memory for the whole list every list-operation. So this could be
done faster. Someone told me, I could try it with a "linked list".
I read about that and rewrote the functions. To my surprise with structs
this was easier to do than the string-version.
Anyway, below I post an example of what I did (I promise, if code get''s any
longer than this, I''ll put it up as a file for download). It just defines
one or two list and prints some results, demonstrating the list-functions.

Please take a look at main(). With the comments there, it should give you an
idea of what I''m after.

Well, what do you think of it (besides me casting malloc() :) ), that is
what do you think of my code, but also of the idea in general ?

I know, other people have tried something similar before:

http://jengelh.hopto.org/f/libHX/
http://mij.oltrelinux.com/devel/simclist/
https://sourceforge.net/project/show...group_id=97342
http://www.pronix.de/comment/site-10...06/site-1.html

But why for example has this never made its way to the C standard library
like std::vector has to STL in C++ ?

See you

H.

/************************************************** *********************/
/*
list.c: Provides linked lists of strings.

Compile with:
gcc -W -Wall -pedantic -ansi -o listexample list.c

Written by hlubenow (Email: hl*******@gmx.net), (C) 2007. License: LGPL.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this program; if not, write to the
Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

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

struct list
{
char *content;
struct list *next;
};

char *strMalloc(unsigned int s);
char *strRealloc(char *a, unsigned int s);
struct list *listMalloc(unsigned int s);
struct list *newList(void);
struct list *listUnshift(struct list *head, char *b);
struct list *listAssign(struct list *head, int pos, char *b);
struct list *listAppend(struct list *head, char *b);
struct list *listInsert(struct list *head, int pos, char *b);
int countList(struct list *head);
char *getElement(struct list *head, int pos);
struct list *deleteElement(struct list *head, int pos);
struct list *deleteElements(struct list *head, int start, int end);
struct list *strToList(char *a, char *b);
char *listToStr(struct list *head, char *joinstr);
int printList(struct list *head);
struct list *clearList(struct list *head);
int main(void)
{
struct list *a;
struct list *s;
int count;
char *elem;
char *j;
char *x;
int i;

/* Ok, let''s go: */

/* Create a list */
a = newList();

/* Fill it */
a = listAppend(a, "apple");
a = listAppend(a, "banana");
a = listAppend(a, "cherry");
a = listAppend(a, "strawberry");

/* Count it */
count = countList(a);

printf("\nThe list has %d elements now:\n", count);

/* Get single elements from it */
for (i = 0; i < count; i++)
{
elem = getElement(a, i);
printf("%d: %s\n", i, elem);
free(elem);
}

/* Directly define elements */
a = listAssign(a, 1, "pear");

puts("\nList changed:");
printList(a);

/* Even beyond the borders of the list */
a = listAssign(a, 5, "lemon");

puts("\nList changed again:");
printList(a);

count = countList(a);
printf("The list has %d elements now.\n\n", count);

/* You also can insert (listInsert()) and delete elements
(deleteElements()) from the list */

/* Furthermore strings can be split and returned as a list: */

s = strToList("Split me into parts", " ");

count = countList(s);
for (i = 0; i < count; i++)
{
elem = getElement(s, i);
printf("%d: %s\n", i, elem);
free(elem);
}

s = clearList(s);

/* And lists can be joined to a string: */
a = listAssign(a, 4, "mango");
j = listToStr(a, " ");

printf("\nA string: %s\n", j);

free(x);
free(j);

/* Please call this, if you don''t need the list anymore: */
a = clearList(a);

return 0;
}
char *strMalloc(unsigned int s)
{
/* Allocates memory for a string, testing if allocation has
been successfull. */

char *a;
if ((a = (char *) malloc(s * sizeof(char))) == NULL)
{
puts("Error allocating memory.");
exit(1);
}

return a;
}
char *strRealloc(char *a, unsigned int s)
{
/* Reallocates memory of a string, testing if reallocation has
been successfull. */

if ((a = (char *) realloc(a, s * sizeof(char))) == NULL)
{
puts("Error reallocating memory.");
exit(1);
}

return a;
}
struct list *listMalloc(unsigned int s)
{
/* Allocates memory for a list, testing if allocation has
been successfull. */

struct list *a;
if ((a = (struct list *) malloc(s * sizeof(struct list))) == NULL)
{
puts("Error allocating list-memory.");
exit(1);
}

return a;
}
/* List functions. */

struct list *newList(void)
{
return NULL;
}

struct list *listUnshift(struct list *head, char *b)
{
/* Inserts an element at the beginning of the list, like
Perl''s "unshift()". The first element has to be put into
the list this way. listAppend() and listAssign() take care
of this automatically. */

struct list *c = listMalloc(1);
c->content = strMalloc(strlen(b) + 1);
strcpy(c->content, b);
c->next = head;
return c;
}
struct list *listAssign(struct list *head, int pos, char *b)
{
/* Lets you define or redefine list-elements.
If pos is greater than the list, the list is extended and the new
element is appended. */

int listlen;
int i;
struct list *a;

if (pos < 0)
{
puts ("List index out of range.");
exit(1);
}

if (head == NULL)
{
head = listUnshift(head, b);
return head;
}

listlen = countList(head);

if (pos >= listlen)
{
for (i = 0; i < pos - listlen; i++)
{
head = listAppend(head, "");
}

head = listAppend(head, b);
return head;
}

a = head;

for (i=0; i < pos; i++)
{
a = a->next;
}

a->content = strRealloc(a->content, strlen(b) + 1);
strcpy(a->content, b);
return head;
}
struct list *listAppend(struct list *head, char *b)
{
struct list *a;
struct list *c;

if (head == NULL)
{
head = listUnshift(head, b);
return head;
}

c = listMalloc(1);
c->content = strMalloc(strlen(b) + 1);
strcpy(c->content, b);

a = head;

while(a->next)
{
a = a->next;
}
a->next = c;
c->next = NULL;
return head;
}
struct list *listInsert(struct list *head, int pos, char *b)
{
/* Inserts a new element into the list extending the list. */

int listlen;
int i;
struct list *a;
struct list *c;

if (head == NULL || pos == 0)
{
head = listUnshift(head, b);
return head;
}

listlen = countList(head);

if (pos >= listlen)
{
puts ("List index out of range.");
exit(1);
}

c = listMalloc(1);
c->content = strMalloc(strlen(b) + 1);
strcpy(c->content, b);

a = head;

for (i=0; i < pos - 1; i++)
{
a = a->next;
}

c->next = a->next;
a->next = c;
return head;
}
int countList(struct list *head)
{
/* Returns the number of elements of the list.
Also used internally. */

int x = 0;

if (head == NULL)
{
return x;
}

while(head->next)
{
x ++;
head = head->next;
}
x ++;
return x;
}
char *getElement(struct list *head, int pos)
{
/* Returns the element at position pos of the list. */

int i;
char *a;
int listlen = countList(head);

if (pos >= listlen)
{
puts ("List index out of range.");
exit(1);
}

for (i=0; i < pos; i++)
{
head = head->next;
}

a = strMalloc(strlen(head->content) + 1);
strcpy(a, head->content);
return a;
}
struct list *deleteElement(struct list *head, int pos)
{
struct list *a;
struct list *b;
struct list *c;
int i;
int length = countList(head);

if (pos >= length || pos < 0)
{
puts ("List index out of range.");
exit(1);
}

if (length <= 1)
{
if (length == 1)
{
free(head);
}

return NULL;
}

a = head;
b = a->next;

if (length == 2)
{
a->next = NULL;
free(b);
return head;
}

for (i=0; i < pos - 1; i++)
{
a = a->next;
}

b = a->next;
c = b->next;

a->next = c;
free(b);

return head;
}
struct list *deleteElements(struct list *head, int start, int end)
{
/* Deletes elements starting from position "start" to position
"end" from the list.

If -1 is passed as "end", the list is deleted
from position "start" to its end. */

int i;
int length = countList(head);

if (start >= length || end >= length
|| start < 0 || end < -1)
{
puts ("List index out of range.");
exit(1);
}

if (start end)
{
puts ("Invalid values.");
exit(1);
}

if (end == -1)
{
end = length - 1;
}

for (i=start; i <= end; i++)
{
head = deleteElement(head, start);
}

return head;
}
struct list *strToList(char *a, char *b)
{
/* Splits a string at "b" and converts the result
into a list. "listUnshift()" instead of "listAppend()" is
used for speed reasons (tricky). */

struct list *head;
char *c;
int lenc;
int lenb;
int i;
int u;
int x;

if (strstr(a, b) == NULL)
{
puts("Splitpart not in string to split !");
return NULL;
}

head = NULL;

c = strMalloc(strlen(a) + 1);
strcpy(c, a);

lenc = strlen(c);
lenb = strlen(b);

for(i = lenc - 1; i >= 0; i--)
{
x = 0;

if(i >= lenb - 1 && *(c + i) == *(b + lenb - 1))
{
for(u = 0; u < lenb; u++)
{
if(*(c + i - lenb + 1 + u) == *(b + u))
{
x++;
}
else
{
break;
}
}

if(x == lenb)
{
*(c + i - lenb + 1) = ''\0'';
if(i != lenc - 1)
{
head = listUnshift(head, c + i + 1);
}
}
}
}

head = listUnshift(head, c);

free(c);

return head;
}
char *listToStr(struct list *head, char *joinstr)
{
/* Join a list to a single string, connecting the
list-elements with "joinstr". */

int size;
char *b;
struct list *a = head;

while(a->next != NULL)
{
size += strlen(a->content);
size += strlen(joinstr);
a = a->next;
}

size += strlen(a->content);
size ++;

b = strMalloc(size);
strcpy(b, "");

a = head;

while(a->next != NULL)
{
strcat(b, a->content);
strcat(b, joinstr);
a = a->next;
}

strcat(b, a->content);
return b;
}
int printList(struct list *head)
{
while(head->next != NULL)
{
if(*(head->content + strlen(head->content) - 1) != ''\n'')
{
puts(head->content);
}
else
{
printf("%s", head->content);
}

head = head->next;
}
if(*(head->content + strlen(head->content) - 1) != ''\n'')
{
puts(head->content);
}
else
{
printf("%s", head->content);
}

return 0;
}
struct list *clearList(struct list *head)
{
/* If you don''t need your list any more, you''re supposed to call
this to free the memory of each element''s struct instance. */

struct list *a;
struct list **b;
int listlen;
int i;

listlen = countList(head);

/* Create "listlen" pointers to each element (structure) of the list. */

if ((b = (struct list **) malloc(listlen * sizeof(struct list *))) ==
NULL)
{
puts("Error allocating memory.");
exit(1);
}

a = head;

for (i=0; i < listlen; i++)
{
b[i] = a;
a = a->next;
}

for (i=listlen - 1; i == 0; i--)
{
if (i 0)
{
b[i - 1]->next = NULL;
}
free(b[i]);
}

free(b);

return NULL;
}
/************************************************** *********************/

推荐答案

hlubenow说:


< snip>
hlubenow said:

<snip>

嗯,你怎么看待它(除了我铸造malloc():) ),
Well, what do you think of it (besides me casting malloc() :) ),



因为你显然知道铸造ma lloc在这里被认为是一个不好的主意,但无论如何都要做到这一点,我看不出有任何好处。

指出其他不好的想法你的代码。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

Since you evidently are aware that casting malloc is considered to be a
bad idea here, and yet do it anyway, I don''t see any gain to be had in
pointing out other bad ideas in your code.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


3月31日15:54,hlubenow< hluben ... @ gmx.netwrote:
On 31 Mar, 15:54, hlubenow <hluben...@gmx.netwrote:

char * strMalloc(unsigned int s);

char * strRealloc(char * a,unsigned int s);

struct list * listMalloc(unsigned int s);
char *strMalloc(unsigned int s);
char *strRealloc(char *a, unsigned int s);
struct list *listMalloc(unsigned int s);



以上参数的类型应为size_t,

不是未签名的。


<剪断>

The above parameters should be of type size_t,
not unsigned.

<snip>


/ *如果您不再需要该列表,请致电:* /

a = clearList(a);
/* Please call this, if you don''t need the list anymore: */
a = clearList(a);



也许clearList被错误命名。这是否释放了与某个相关的内存

?鉴于名称,我希望它不会,但你正在使用它,就好像它一样。

Perhaps clearList is misnamed. Does this free the memory
associated with a? Given the name, I would expect it
not to, but you are using it as if it does.


>

char * strMalloc(unsigned int s)

{

/ *为字符串分配内存,测试分配是否为
成功了。 * /


char * a;

if((a =(char *)malloc(s * sizeof(char)))== NULL)

{

puts(错误分配内存。);

退出(1);

}
>
char *strMalloc(unsigned int s)
{
/* Allocates memory for a string, testing if allocation has
been successfull. */

char *a;
if ((a = (char *) malloc(s * sizeof(char))) == NULL)
{
puts("Error allocating memory.");
exit(1);
}



1)错误消息应该发送到stderr,而不是stdout。

2)如果你退出,你应该退出EXIT_FAILURE,

但是你不应该离开这里。您应该

而不是返回错误值。 (例如NULL)。

3)Sizeof(char)总是1.调用

" malloc(s * sizeof * a)是有意义的,但是用mmof(char)称它为b $ b毫无意义。

-

Bill Pursell

1) Error messages should go to stderr, not stdout.
2) If you exit, you should exit with EXIT_FAILURE,
but you shouldn''t exit here. You should
instead return an error value. (eg NULL).
3) Sizeof(char) is always 1. It makes sense to call
"malloc( s * sizeof *a)", but it pointless to call it
with sizeof(char).
--
Bill Pursell

" Richard Heathfield" < rj*@see.sig.invalidwrote in message
"Richard Heathfield" <rj*@see.sig.invalidwrote in message

hlubenow说:


< snip>
hlubenow said:

<snip>

>嗯,您如何看待它(除了我铸造malloc():)),
>Well, what do you think of it (besides me casting malloc() :) ),



既然你明显知道铸造malloc在这里被认为是一个坏主意,但无论如何都要这样做,我看不出任何收获在

在你的代码中指出其他坏主意。


Since you evidently are aware that casting malloc is considered to be a
bad idea here, and yet do it anyway, I don''t see any gain to be had in
pointing out other bad ideas in your code.



如果你强制转换malloc(),代码可能会在C ++下编译。这是合法的,即使你碰巧不同意,也要等于b $ b。另一方面,出口(1)是不可移动的。

无异议它应该退出(EXIT_FAILURE)。


-

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

If you cast malloc() the code might compile under C++. That''s legitimate,
even if you happen to disagree. On the other hand exit(1) is non-portable.
No dispute that it should be exit(EXIT_FAILURE).

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


这篇关于C语言中的动态字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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