realloc()问题 [英] realloc() question

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

问题描述

你好,我正在阅读一本我从图书馆得到的书。不可否认,这本书

已经很久了 - 它起源于1994年,自那以后发生了很多变化。但是,我想从书中学到尽可能多的东西,因为它涵盖了一些非常有趣的话题。

我有一个对我来说有点混乱的错误,以及我从中获得的帮助
按F1就可以得到很多帮助。以下是给我错误的代码片段,然后是错误:


#include< stdio.h>

#include< stdlib.h>


#define NAME_SIZE 35


void main(无效)

{

long student_ctr = 0;

long ctr;

char * student_name = NULL;

while((student_name =

realloc(student_name,

(NAME_SIZE *(student_ctr + 1))))!= NULL)

{< br $> b $ b / * ...这里的Buncha东西...... yada yada yada

}


-------- ------------配置:SamListing01_05 - Win32

调试--------------------

编译......

SamListing01_05.cpp

C:\Program Files \ Microsoft Visual />
Studio \ MyProjects \\ \\SamListing01_05\SamListing01_05。 cpp(19):错误C2440:''=''

:无法从''void *''转换为''char *''

转换自'' void *''指向非'''void''需要一个

显式转换

C:\Program Files \ MicrosoftMicrosoft Visual

Studio \ MyProjects \SamListing01_05 \SamListing01_05。 cpp(19):致命错误

C1903:无法从之前的错误中恢复;停止编译

执行cl.exe时出错。


SamListing01_05.exe - 2个错误,0个警告

....我当然主要感兴趣的是如何修改这个程序,所以我可以让它编译并运行没有错误。如果你需要剩下的代码,我会很乐意提供它......但是它指的是

的最后一行是否为错误位置的while语句。我将非常感谢

任何帮助...甚至提示如何不这样做,例如,符合ANSI C,

或C之间的差异'' 94和今天。谢谢! :)


- Eric A. Johnson

解决方案

Eric A. Johnson写道:

我正在读一本我从图书馆得到的书。不可否认,这本书很古老 - 它起源于1994年,自那以后发生了很多变化。但是,我希望尽可能多地从本书中学习,因为它涵盖了一些非常有趣的话题。
我有一个错误,对我来说有点混乱,帮助我来自
按F1就可以得到很多帮助。以下
是给我错误的代码片段,后面跟着错误:

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

#define NAME_SIZE 35


{
long student_ctr = 0;
long ctr;
char * student_name = NULL;
while((student_name =
realloc(student_name,
(NAME_SIZE *(student_ctr + 1))))!= NULL)
{
/ * ...... Buncha的东西在这里... yada yada yada
}

--------------------配置: SamListing01_05 - Win32
调试--------------------
编译......
SamListing01_05.cpp
C:\\ \\ Program Files \ Microsoft Visual /
Studio \ MyProjects \SamListing01_05 \SamListing01_05。 cpp(19):错误C2440:
''=''
:无法从''void *''转换为''char *''
转换为''void *''指向非''void''需要一个明确的演员
C:\Program Files\Microsoft Visual
Studio \ MyProjects \SamListing01_05 \SamListing01_05。 cpp(19):致命错误
C1903:无法从之前的错误中恢复;停止编译
执行cl.exe时出错。

SamListing01_05.exe - 2个错误,0个警告

...我主要感兴趣当然,在如何修改这个程序,以便我可以让它编译和运行没有错误。如果您需要代码的其余部分,我很乐意提供它......但它指出了错误位置的while语句的最后一行。我将非常感激
任何帮助...甚至提示如何不这样做,例如,符合ANSI
C,
或'94'和今天的C之间的差异。谢谢! :)




C和C ++之间的差异如何?

您最有可能使用的是c ++编译器(.cpp文件扩展名?!)并且

a void *与C ++中的char *不兼容。您应该使用c $ / $
编译器来编译代码并进行编译。至于工作

我更怀疑(取决于你在循环体中做了什么):当你退出这个特定的循环时你需要分配直到
realloc失败,并且

然后你继续泄漏所有的记忆....不要做ax = realloc(x,n)

但是一个


tmp = realloc(x,n);

if(tmp)

x = tmp

else

处理问题

Tobias。






Eric A. Johnson写道:

我正在读一本我从图书馆得到的书。不可否认,这本书很古老 - 它起源于1994年,自那以后发生了很多变化。但是,我希望尽可能多地从本书中学习,因为它涵盖了一些非常有趣的话题。
我有一个错误,对我来说有点混乱,帮助我来自
按F1就可以得到很多帮助。以下是给我错误的代码片段,后面跟着错误:

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

#define NAME_SIZE 35

无主要(无效)


int main(无效),自1989年以来。

{
long student_ctr = 0;
long ctr;
char * student_name = NULL;
while((student_name =
realloc(student_name,
(NAME_SIZE *(student_ctr + 1))))!= NULL)
{
/ * ...这里的Buncha东西...... yada yada yada
}

--------------------配置:SamListing01_05 - Win32
调试---------------- ----
编译......
SamListing01_05.cpp
C:\ Program Files \ Microsoft Visual /> Studio \ MyProjects \SamListing01_05 \SamListing01_05。 cpp(19):错误C2440:''=''
:无法从''void *''转换为''char *''
从''void *''转换为指向非' - ''void''需要一个明确的演员
C:\Program Files\Microsoft Visual
Studio \ MyProjects \SamListing01_05 \SamListing01_05。 cpp(19):致命错误
C1903:无法从之前的错误中恢复;停止编译
执行cl.exe时出错。

SamListing01_05.exe - 2个错误,0个警告




看起来编译器认为你的代码应该是C ++而不是C代码。尽管他们名字的第一个部分有相似之处,但这两种语言却截然不同。

(我的名字和中间名首字母也是Eric A,但我是

肯定你会同意你,而我却很不一样...)

尝试将文件从blah.cpp重命名为blah.c;如果那不是答案,请检查编译器的文档中的

适当的咒语,告诉它这是C,而不是C ++。


-
Er ******** *@sun.com




" Eric A. Johnson" <无***** @ dontlookforme.com>写了

char * student_name = NULL;
while((student_name =
realloc(student_name,
(NAME_SIZE *(student_ctr + 1))))!= NULL )

Studio \ MyProjects \SamListing01_05 \SamListing01_05。 cpp(19):错误C2440:
''='':无法从''void *''转换为''char *''
从''void *''转换为指向非' - ''void''需要一个
显式强制转换



在C中,将void *转换为另一种没有
$ b $的指针类型是合法的b cast,而在C ++中它不是。


由于你给你的文件扩展名为.cpp,MSVC ++将它视为C ++。


关于是否应该编写C代码以便在C ++下编译或者编译它是否存在争论。我们没有必要进入这个,激发你的

两个选项是添加演员

student_name =(char *)realloc(。 ..)


并在C ++下编译,或将文件名更改为.c扩展名并编译为

为C.


Hi, I''m going through a book I got from the library. Admittedly, the book
is old -- it hails from 1994, and much has changed since then. However, I
would like to learn as much as possible from the book, as it covers some
very interesting topics.
I have an error that is a bit confusing to me, and the help I get from
pressing F1 on it is to general to be of much assistance. The following is
the code snippet that is giving me the error, followed by the error:

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

#define NAME_SIZE 35

void main(void)
{
long student_ctr = 0;
long ctr;
char *student_name = NULL;
while( (student_name =
realloc( student_name,
(NAME_SIZE * (student_ctr+1)))) != NULL )
{
/* ...Buncha stuff here... yada yada yada
}

--------------------Configuration: SamListing01_05 - Win32
Debug--------------------
Compiling...
SamListing01_05.cpp
C:\Program Files\Microsoft Visual
Studio\MyProjects\SamListing01_05\SamListing01_05. cpp(19) : error C2440: ''=''
: cannot convert from ''void *'' to ''char *''
Conversion from ''void*'' to pointer to non-''void'' requires an
explicit cast
C:\Program Files\Microsoft Visual
Studio\MyProjects\SamListing01_05\SamListing01_05. cpp(19) : fatal error
C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

SamListing01_05.exe - 2 error(s), 0 warning(s)

....I am primarily interested, of course, in how to modify this program so
that I can get it to compile and run without errors. If you need the rest
of the code, I will gladly supply it... but it pointed to the last line of
the while statement for the location of the error. I will be grateful for
any help... even tips on how this may not, for example, conform with ANSI C,
or differences between the C of ''94 and of today. Thank you! :)

-- Eric A. Johnson

解决方案

Eric A. Johnson wrote:

Hi, I''m going through a book I got from the library. Admittedly, the book
is old -- it hails from 1994, and much has changed since then. However, I
would like to learn as much as possible from the book, as it covers some
very interesting topics.
I have an error that is a bit confusing to me, and the help I get from
pressing F1 on it is to general to be of much assistance. The following
is the code snippet that is giving me the error, followed by the error:

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

#define NAME_SIZE 35

void main(void)
{
long student_ctr = 0;
long ctr;
char *student_name = NULL;
while( (student_name =
realloc( student_name,
(NAME_SIZE * (student_ctr+1)))) != NULL )
{
/* ...Buncha stuff here... yada yada yada
}

--------------------Configuration: SamListing01_05 - Win32
Debug--------------------
Compiling...
SamListing01_05.cpp
C:\Program Files\Microsoft Visual
Studio\MyProjects\SamListing01_05\SamListing01_05. cpp(19) : error C2440:
''=''
: cannot convert from ''void *'' to ''char *''
Conversion from ''void*'' to pointer to non-''void'' requires an
explicit cast
C:\Program Files\Microsoft Visual
Studio\MyProjects\SamListing01_05\SamListing01_05. cpp(19) : fatal error
C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

SamListing01_05.exe - 2 error(s), 0 warning(s)

...I am primarily interested, of course, in how to modify this program so
that I can get it to compile and run without errors. If you need the rest
of the code, I will gladly supply it... but it pointed to the last line of
the while statement for the location of the error. I will be grateful for
any help... even tips on how this may not, for example, conform with ANSI
C,
or differences between the C of ''94 and of today. Thank you! :)



How about differences between C and C++ ?
You most likely are using a c++ compiler (.cpp file extension?!) and
a void * isn''t compatible with a char * in C++. You should use a c
compiler to compile your code and it will compile. As far as working
I''m more doubtfull (depending on what you do in the body of the loop): When
you exit this particular loop you have allocate until realloc fails, and
then you go ahead and leak all that memory.... Don''t do a x = realloc(x,n)
but a

tmp = realloc(x,n);
if (tmp)
x = tmp
else
deal with problem
Tobias.





Eric A. Johnson wrote:

Hi, I''m going through a book I got from the library. Admittedly, the book
is old -- it hails from 1994, and much has changed since then. However, I
would like to learn as much as possible from the book, as it covers some
very interesting topics.
I have an error that is a bit confusing to me, and the help I get from
pressing F1 on it is to general to be of much assistance. The following is
the code snippet that is giving me the error, followed by the error:

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

#define NAME_SIZE 35

void main(void)
int main(void), ever since 1989.
{
long student_ctr = 0;
long ctr;
char *student_name = NULL;
while( (student_name =
realloc( student_name,
(NAME_SIZE * (student_ctr+1)))) != NULL )
{
/* ...Buncha stuff here... yada yada yada
}

--------------------Configuration: SamListing01_05 - Win32
Debug--------------------
Compiling...
SamListing01_05.cpp
C:\Program Files\Microsoft Visual
Studio\MyProjects\SamListing01_05\SamListing01_05. cpp(19) : error C2440: ''=''
: cannot convert from ''void *'' to ''char *''
Conversion from ''void*'' to pointer to non-''void'' requires an
explicit cast
C:\Program Files\Microsoft Visual
Studio\MyProjects\SamListing01_05\SamListing01_05. cpp(19) : fatal error
C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

SamListing01_05.exe - 2 error(s), 0 warning(s)



It looks like the compiler thinks your code is supposed
to be C++ rather than C. Despite similarities in the first
parts of their names, the two languages are rather different.
(My first name and middle initial are also "Eric A," yet I''m
sure you''ll agree you and I are rather different ...)

Try renaming your file from blah.cpp to blah.c; if that''s
not the answer, check your compiler''s documentation for the
proper incantation to tell it "This is C, not C++."

--
Er*********@sun.com



"Eric A. Johnson" <no*****@dontlookforme.com> wrote

char *student_name = NULL;
while( (student_name =
realloc( student_name,
(NAME_SIZE * (student_ctr+1)))) != NULL )

Studio\MyProjects\SamListing01_05\SamListing01_05. cpp(19) : error C2440: ''='' : cannot convert from ''void *'' to ''char *''
Conversion from ''void*'' to pointer to non-''void'' requires an
explicit cast


In C it is legal to convert from a void * to another pointer type without a
cast, whilst in C++ it is not.

Since you gave your file a .cpp extension, MSVC++ is treating it as C++.

There''s a debate on about whether C code should be written so that it
compiles under C++ or not. We don''t have to get into this, excpet that your
two options are to add the cast

student_name = (char *) realloc( ... )

and compile under C++, or change the filename to a .c extension and compile
as C.


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

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