本文介绍了省略号函数中动态分配的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我是C的新手,所以请提前接受我的道歉:-)


我'我试图在while循环中为一个指向字符串的指针数组分配空间

(它们被接受为省略号),并且在

分配之后,当我断言"分配,断言失败!!!


void printStrings(s1,...){//省略函数

....

....

void * myList;

int count = 1;

char * pArr = s1;

va_start(myList,s1);

while(myList){

pArr =(char *)realloc(pArr,++ count * sizeoff( char *));

断言(pArr);

pArr [count-1] = va_arg(myList,char *);

}


但是,如果我从

中取出2行的分配和断言,那么它可以工作!!!

ie - 以下是好的:


void * myList;

int count = 1;

char * pArr = s1 ;

va_start(myList,s1);


pArr =(char *)realloc(pArr,++ count * sizeoff(char *));

断言(pArr); //这没关系


即使我将这两行放在一个只运行一次的for循环中,它也会失败!

失败!!!


int count = 1;

char * pArr = s1;

va_start(myList,s1);

for(i = 0; i< 1; i ++){// LOOP RUNS JUST ONCE

pArr =(char *)realloc(pArr,++ count * sizeoff(char *));

断言(pArr); // ASSERTION FAILS HERE !!!

}


我做错了什么以及为什么分配失败在

内while / for循环


非常感谢

Shiron

Hello all

I''m pretty new to C, so please accept my apologies in advance :-)

I''m trying to allocate space for an array of pointers to strings
(which are accepted as ellipses) inside a while loop, and after the
allocation, when i "assert" the allocation, the assertion fails!!!

void printStrings(s1, ...){ //ellipses function
....
....
void *myList;
int count=1;
char *pArr=s1;
va_start(myList, s1);
while( myList ){
pArr = (char *) realloc(pArr, ++count * sizeoff(char*));
assert(pArr);
pArr[count-1] = va_arg(myList, char*);
}

However, if i take the 2 lines of the allocation and assertion out of
he while, it works!!!
i.e. - the following is OK :

void *myList;
int count=1;
char *pArr=s1;
va_start(myList, s1);

pArr = (char *) realloc(pArr, ++count * sizeoff(char*));
assert(pArr); //THIS IS OK

even if i put these two lines in a for loop that run just once, it
fails!!!

int count=1;
char *pArr=s1;
va_start(myList, s1);
for(i =0; i <1; i++){ // LOOP RUNS JUST ONCE
pArr = (char *) realloc(pArr, ++count * sizeoff(char*));
assert(pArr); //ASSERTION FAILS HERE!!!
}

what Am i doing wrong and why is the allocation failing inside the
while/for loops

Thanks a lot
Shiron

推荐答案



确保你的源文件顶部有#include< stdlib.hnear,

输掉演员,使用temp,并获得正确的大小:


char * tmp = realloc(pArr,++ count * sizeof * pArr);

if(tmp!= NULL)

{

pArr = tmp;

Make sure you have #include <stdlib.hnear the top of your source file,
lose the cast, use a temp, and get sizeof right:

char *tmp = realloc(pArr, ++count * sizeof *pArr);
if(tmp != NULL)
{
pArr = tmp;



在C90中,断言需要一个整数表达式:


断言(pArr!= NULL);


但这不是检查分配是否成功的好方法,因为如果未定义NDEBUG,它将在失败时中止应用程序,并且

将忽略失败!

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999


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

In C90, assert requires an integer expression:

assert(pArr != NULL);

but this is not a good way to check whether the allocation succeeded, as
it will abort the application on failure if NDEBUG is not defined, and
will ignore the failure otherwise!
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.




确保你的源文件顶部有#include< stdlib.hnear,

输掉演员,使用temp,并获得正确的大小:


char * tmp = realloc(pArr,++ count * sizeof * pArr);

if(tmp!= NULL)

{

pArr = tmp;


Make sure you have #include <stdlib.hnear the top of your source file,
lose the cast, use a temp, and get sizeof right:

char *tmp = realloc(pArr, ++count * sizeof *pArr);
if(tmp != NULL)
{
pArr = tmp;



在C90中,断言需要一个整数表达式:


断言(pArr!= NULL);


但这不是检查分配是否成功的好方法,因为如果未定义NDEBUG,它将在失败时中止应用程序,并且
$ b否则$ b将忽略失败!


-

Richard Heathfield

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

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


In C90, assert requires an integer expression:

assert(pArr != NULL);

but this is not a good way to check whether the allocation succeeded, as
it will abort the application on failure if NDEBUG is not defined, and
will ignore the failure otherwise!

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



对不起 - 它不起作用.....

无论如何....为什么我会输掉演员表? realloc返回void

*和pArr是char *,所以需要演员......或者我错了吗?

Sorry - it doesn''t work ....
in any case .... why should I lose the cast? realloc returns void
* and pArr is char * , so a cast is required ... or am I wrong?




我的猜测:


您正在尝试重新分配pArr指向的内存。

第一次循环,pArr == s1。


如果您打电话的话如下:


printStrings("这是s1指向的字符串,

等等),...


.. ..然后我觉得很有可能字符串这是

字符串将被s1指向将有静态存储/是

由于字符串池/等等只读*所以我不认为

realloc''这个记忆是有效的。


* I 毫无疑问,这个术语是错误的,但是给它十分钟就可以了。

你很快就会看到一百个人纠正我了!


希望帮助,

道格


My guess:

You are attempting to realloc the memory pointed to by pArr. The
first time through the loop, pArr == s1.

If you are calling with something like:

printStrings("This is the string which will be pointed to by s1",
etc., etc., ...

....then I think there''s a good chance that the string "This is the
string which will be pointed to by s1" will have static storage/be
read-only due to string pooling/etc.* So I don''t think that
realloc''ing this memory is valid.

* I''ve no doubt got the terminology wrong, but give it ten minutes and
you''ll soon see a hundred people correct me!

Hope that helps,
Doug


这篇关于省略号函数中动态分配的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 14:37