www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/09/04/07:00:11

Path: news.mv.net!news.shore.net!newsfeed.mathworks.com!europa.netcrusader.net!194.176.220.129!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!news.netcologne.de!news.rwth-aachen.de!!broeker
From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: ok, I have more info on problem at hand
Date: 4 Sep 2000 10:40:46 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 40
Message-ID: <8ovu7e$9p2$1@nets3.rz.RWTH-Aachen.DE>
References: <Mtg9LD_00Uw9QI9Fw3 AT andrew DOT cmu DOT edu>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 968064046 10018 137.226.32.75 (4 Sep 2000 10:40:46 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 4 Sep 2000 10:40:46 GMT
Originator: broeker@
Xref: news.mv.net comp.os.msdos.djgpp:103152

James W Sager Iii <sager+@andrew.cmu.edu> wrote:
[...]
> int *i;
> i=malloc(20);
> i=realloc(100);

> errors:
>  Error: ANSI C++ forbids implicit conversion from `void *' in assignment
>  Error: ANSI C++ forbids implicit conversion from `void *' in assignment

This is an example of one of the areas in which modern ANSI/ISO
standardized C++ will _refuse_ to compile programs that are perfectly
valid in ANSI C, and used to be valid in many C++ compilers that were
released before the C++ language finally managed to become
standardized.

I seem to remember there's some gcc command line option (-relax or
something similarly named) that will reduce such errors to warnings.

Setting that aside, you have these alternatives:

1) "proper C++ coding" would have you use 'new' instead of malloc().
   The backside is that there is no equivalent of realloc(), for a
   C++ array.
2) use advanced C++ classes like the STL vector<int> thing to model
   dynamically growable/shrinkable arrays.
3) stick to malloc()/realloc()/free(). In this case, you'll have to
   *cast* the pointer returned by them. I.e., you have to write:
	
	i = (int *) malloc(20);
	i = (int *) realloc(100);

    The same idiom is considered a bad idea by the C gurus, but in
    C++, it's a necessity. The underlying change is that due to 
    classes and polymorphism, C++ had to do away with the concept of
    fully automatic pointer conversion to and from 'void *'.

-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019