malloc麻烦 [英] malloc trouble

查看:59
本文介绍了malloc麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


在另一个主题中,我被告知我必须动态分配内存而不是仅仅尝试扩展列表。 (我正在努力学习

C,并且拥有PHP和Python的强大背景)鉴于此,我

一直在尝试学习malloc,realloc但是没有用,但无济于事。


但是由于某种原因,我正在左右分段错误,并且要诚实地说,我是b $ b我真的找不到它为什么没有运气

不起作用。但是,我已经跟踪它并发现

malloc()调用是造成问题的原因。 (通过添加各种

printf()跟踪,然后将malloc移动到它自己的行)


如果有人可以,我们将不胜感激指出哎呀

我做错了。代码snipplet包含在这个

消息的末尾。


提前多谢谢你。


-Wes


/ *在文件中等级* /

#define BUFFLEN 100

char **消息;

int num_messages = 0;

/ * main()内部* /

if((messages [num_messages] =(char *)malloc((size_t)BUFFLEN))

== NULL)

{

printf(无法分配空格。\ n);

返回1;

}

解决方案

ncf写道:

大家好。

另一个话题,我被告知我必须动态分配内存而不是仅仅尝试扩展列表。 (我正在努力学习
C,并且拥有PHP和Python的强大背景)鉴于此,我一直在努力学习malloc,realloc和free,但无济于事。

但出于某种原因,我正在左右分段,并且说实话,我真的没有任何运气真的找到它的原因不工作。但是,我已经跟踪它并发现
malloc()调用是造成问题的原因。 (通过添加各种
printf()跟踪,然后将malloc移动到它自己的行)

如果有人能指出什么,那将是非常感谢的。 >我做错了。代码信息包含在此消息的末尾。

提前感谢您。

-Wes

/ *在文件中等级* /
#define BUFFLEN 100
char **消息;


[以后的帖子*真* *可编译*代码。它使事情变得更容易。]


他的观点'消息'的价值是什么?

int num_messages = 0;
/ *在main()里面* /
if((messages [num_messages] =(char *)malloc((size_t)BUFFLEN))
== NULL)
{
printf(无法分配空格。\ n);
返回1;
}



请参阅常见问题,特别是关于`动态的文章分配

多维数组''。


HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀
http ://goldsays.blogspot.com (新帖子8/5)
http://www.cafepress.com/goldsays

如果你没有什么可隐瞒的,你就不会尝试!


好吧,我刚检查了一个comp.lang.c faq(找到d
http://www.faqs。 org / faqs / C-faq / faq /),说实话,他们使用的代码

对我来说没什么意义,但我会给它一个拍摄。


并且,即使它有可能或可能没有必要,直到我有机会

添加指针分配,代码目前如下:

/ *

* malloc / realloc /免费测试

* /

#include< string。 h>

#include< stdio.h>

#include< stdlib.h>


#define BUFFLEN 10


char **消息;

int num_messages = 0;


int main(int argc,char * * argv){

char tmpmsg [] [BUFFLEN] = {a,b,c,d,e};

int tmpnum = sizeof(tmpmsg)/ BUFFLEN;


printf(要复制%d messages.\ n,tmpnum);


/ *复制循环* /

int x;

for(x = 0; X< tmpnum; x ++){

printf(分配空间为%d。\ n,x);

if((messages [num_messages] =(char *)malloc ((size_t)BUFFLEN))

== NULL)

{

printf(无法分配space.\) ;

返回1;

}


strcpy(messages [num_messages],tmpmsg [x]);

num_messages ++;

}


/ *打印所有值* /

for(x = 0; x< ; num_messages; x ++){

printf("%02d =>%s \ n",x,messages [x]);

}


/ *清除并释放所有内存位置* /

for(x = num_messages-1; x> = 0; x--){

memset(messages [x],''\''',sizeof(messages [x]));

免费(messages [x]);

}


/ *测试结束...最后:P * /

返回0;

}


好的,哇,我想我现在就知道了,但如果

,我会很感激我可以查看我的更新/修改过的代码,以确保它看起来应该是这样的。感谢你指出明显的

(faq) - 有些日子我应该被打耳光:P


-Wes

>
/ *

* malloc / realloc /免费测试

* /

#include< string.h>

#include< stdio.h>

#include< stdlib.h>


#define BUFFLEN 10


char **消息;

int num_messages = 0;


int main(int argc,char ** argv){

char tmpmsg [] [BUFFLEN] = {a,b,c,d,e};

int tmpnum = sizeof(tmpmsg)/ BUFFLEN;


printf(要复制%d messages.\ n,tmpnum);


/ *首先,malloc 0 * sizeof(char *)字节用于消息,因为我们还没有

指针* /

messages = malloc(num_messages * sizeof(char *));


/ *复制循环* /

int x;

for(x = 0; x< tmpnum; x ++){

printf(" Allocating s步伐为%d。\ n",x);

if((messages = realloc(messages,(num_messages + 1)* sizeof(char *)))

== NULL)

{

printf(无法再为一个指针分配空间。\ n);

返回1;

}

if((messages [num_messages] =(char *)malloc((size_t)BUFFLEN))

== NULL )

{

printf(无法分配空格。\ n);

返回1;

}


strcpy(messages [num_messages],tmpmsg [x]);

num_messages ++;

}


/ *打印所有值* /

for(x = 0; x< num_messages; x ++){

printf(" %02d => %s \ n",x,messages [x]);

}


/ *清除并释放所有内存位置* /

for(x = num_messages-1; x> = 0; x--){

memset(messages [x],''\''',sizeof(message [x]));

免费(消息[x]);

}

免费(消息);


/ *测试结束......最后:P * /

返回0;

}


Hi all.

In another topic, I was informed that I had to dynamically allocate
memory instead of just trying to expand on a list. (I''m trying to learn
C, and have a strong background in PHP and Python) In light of that, I
have been trying to learn malloc, realloc, and free, but to no avail.

But for some reason, I''m getting segfaults right and left, and to be
honest, I am not having any luck at all really in finding out why it
isn''t working. However, I have traced it and discovered that the
malloc() call is what is causing the problem. (traced by adding various
printf()s and then moving the malloc to it''s own line)

It would be greatly appreciated if anyone can point out what the heck
I''m doing so wrong. Code snipplets are included at the end of this
message.

Thank you soo much in advance.

-Wes

/* at the "file" level */
#define BUFFLEN 100
char **messages;
int num_messages=0;
/* inside of main() */
if ( (messages[num_messages] = (char *)malloc((size_t)BUFFLEN))
==NULL)
{
printf("Could not allocate space.\n");
return 1;
}

解决方案

ncf wrote:

Hi all.

In another topic, I was informed that I had to dynamically allocate
memory instead of just trying to expand on a list. (I''m trying to learn
C, and have a strong background in PHP and Python) In light of that, I
have been trying to learn malloc, realloc, and free, but to no avail.

But for some reason, I''m getting segfaults right and left, and to be
honest, I am not having any luck at all really in finding out why it
isn''t working. However, I have traced it and discovered that the
malloc() call is what is causing the problem. (traced by adding various
printf()s and then moving the malloc to it''s own line)

It would be greatly appreciated if anyone can point out what the heck
I''m doing so wrong. Code snipplets are included at the end of this
message.

Thank you soo much in advance.

-Wes

/* at the "file" level */
#define BUFFLEN 100
char **messages;
[In the future post *real* *compilable* code. It makes things easier.]

What''s the value of `messages'' at his point?
int num_messages=0;
/* inside of main() */
if ( (messages[num_messages] = (char *)malloc((size_t)BUFFLEN))
==NULL)
{
printf("Could not allocate space.\n");
return 1;
}


Please see the FAQ, particularly the piece on `dynamic allocation of
multidimensional arrays''.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"


Alrighty, I just checked one comp.lang.c faq (located at
http://www.faqs.org/faqs/C-faq/faq/), and to be quite honest, the code
they''re using is making little sense to me, but I will give it a shot.

And, even though it may or may not be necessary until I get the chance
to add the pointer allocation, the code is currently as follows:
/*
* malloc/realloc/free test
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define BUFFLEN 10

char **messages;
int num_messages=0;

int main(int argc, char **argv) {
char tmpmsg[][BUFFLEN] = {"a","b","c","d","e"};
int tmpnum = sizeof(tmpmsg)/BUFFLEN;

printf("Going to copy %d messages.\n",tmpnum);

/* Do the copy loop */
int x;
for (x=0; x<tmpnum; x++) {
printf("Allocating space for %d.\n",x);
if ( (messages[num_messages] = (char *)malloc((size_t)BUFFLEN))
==NULL)
{
printf("Could not allocate space.\n");
return 1;
}

strcpy(messages[num_messages], tmpmsg[x]);
num_messages++;
}

/* print all the values */
for (x=0;x<num_messages;x++) {
printf("%02d => %s\n",x,messages[x]);
}

/* clear and free all the positions of memory */
for (x=num_messages-1;x>=0;x--) {
memset(messages[x], ''\0'', sizeof(messages[x]));
free(messages[x]);
}

/* the end of the test...finally :P */
return 0;
}


Ok, wow, I think I get it now, but it would be grealy appreciated if
someone could look over my updated/modified code to make sure that it
does as it seems it should. Thank you for pointing out the obvious
(faq)--some days I deserve to be slapped :P

-Wes

/*
* malloc/realloc/free test
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define BUFFLEN 10

char **messages;
int num_messages=0;

int main(int argc, char **argv) {
char tmpmsg[][BUFFLEN] = {"a","b","c","d","e"};
int tmpnum = sizeof(tmpmsg)/BUFFLEN;

printf("Going to copy %d messages.\n",tmpnum);

/* first, malloc 0*sizeof(char *) bytes for messages, as we have no
pointers yet */
messages = malloc(num_messages*sizeof(char *));

/* Do the copy loop */
int x;
for (x=0; x<tmpnum; x++) {
printf("Allocating space for %d.\n",x);
if ( (messages = realloc(messages, (num_messages+1)*sizeof(char *)))
== NULL)
{
printf("Could not allocate space for one more pointer.\n");
return 1;
}
if ( (messages[num_messages] = (char *)malloc((size_t)BUFFLEN))
==NULL)
{
printf("Could not allocate space.\n");
return 1;
}

strcpy(messages[num_messages], tmpmsg[x]);
num_messages++;
}

/* print all the values */
for (x=0;x<num_messages;x++) {
printf("%02d => %s\n",x,messages[x]);
}

/* clear and free all the positions of memory */
for (x=num_messages-1;x>=0;x--) {
memset(messages[x], ''\0'', sizeof(messages[x]));
free(messages[x]);
}
free(messages);

/* the end of the test...finally :P */
return 0;
}


这篇关于malloc麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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