 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Michael Trocken Guest
|
Posted: Mon Nov 17, 2003 2:48 pm Post subject: C++ und C und malloc |
|
|
Bei der Weiterverwertung von in C geschriebenem Quell-Kode stößt man
auf das Problem, dass in C++ - im Unterschied zu C - ein Cast auf das
Ergebnis von malloc erforderlich ist.
Dies liegt daran, dass man in C++ einen void* nicht einem anderen
Zeiger zuweisen darf. Beispiel:
char* str = malloc(10); // Fehler in C++
Ich habe mir Folgendes ausgedacht, um malloc dennoch gefügig zu
machen:
namespace test {
class malloc {
std::size_t sz;
public:
malloc(std::size_t size) : sz(size) {}
template<typename T>
operator T*() { return (T*)std::malloc(sz); }
};
}
void bla() {
#ifdef NOMALLOCTEST
using std ::malloc;
#else
using test::malloc;
#endif
char* str = malloc(10); // ok (wenn test::malloc aktiv)
// ...
}
Fällt jemandem ein Fall ein, wo dies zu Problemen führen könnte?
Gruß,
Michael
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Markus Schaaf Guest
|
Posted: Mon Nov 17, 2003 3:06 pm Post subject: Re: C++ und C und malloc |
|
|
"Michael Trocken" <s1234569 (AT) hotmail (DOT) com> schrieb:
| Quote: | class malloc {
std::size_t sz;
public:
malloc(std::size_t size) : sz(size) {}
template
operator T*() { return (T*)std::malloc(sz); }
};
Fällt jemandem ein Fall ein, wo dies zu Problemen führen könnte?
|
An Schnittstellen von Bibliotheken oder an Modulgrenzen werden in C
oft Zeiger auf Allocatorfunktionen übergeben.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Daniel Albuschat Guest
|
Posted: Tue Nov 18, 2003 7:15 am Post subject: Re: C++ und C und malloc |
|
|
Markus Schaaf wrote:
| Quote: | "Michael Trocken" <s1234569 (AT) hotmail (DOT) com> schrieb:
Fällt jemandem ein Fall ein, wo dies zu Problemen führen könnte?
An Schnittstellen von Bibliotheken oder an Modulgrenzen werden in C
oft Zeiger auf Allocatorfunktionen übergeben.
|
An diesen (zumindest steltener, evtl. garnicht vorkommenden) Stellen,
reicht es dann aber, std::malloc zu schreiben. Namespace sei dank.
Die Idee finde ich uebrigens ziemlich gut... zumindest fuer diesen
Zweck. Und solange man darauf achtet, dass der (neue) C++ Code nicht
auch davon betroffen wird. Kannst du evtl. auch
berichten, ob es problemlos funktioniert hat? :-)
cu, Daniel
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Florian Weimer Guest
|
Posted: Tue Nov 18, 2003 7:21 pm Post subject: Re: C++ und C und malloc |
|
|
[email]s1234569 (AT) hotmail (DOT) com[/email] (Michael Trocken) writes:
| Quote: | Ich habe mir Folgendes ausgedacht, um malloc dennoch gefügig zu
machen:
namespace test {
class malloc {
std::size_t sz;
public:
malloc(std::size_t size) : sz(size) {}
template
operator T*() { return (T*)std::malloc(sz); }
};
}
|
Nette Idee. Ob das auch funktioniert, um die leichten Unterschiede bei
den Prototypen in den POSIX-Headern einiger Systeme glattzubügeln?
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Volker Glave Guest
|
Posted: Tue Nov 18, 2003 9:29 pm Post subject: Re: C++ und C und malloc |
|
|
[email]s1234569 (AT) hotmail (DOT) com[/email] (Michael Trocken) wrote in message news:<91267dba.0311170648.689e92b1 (AT) posting (DOT) google.com>...
| Quote: | Bei der Weiterverwertung von in C geschriebenem Quell-Kode stößt man
auf das Problem, dass in C++ - im Unterschied zu C - ein Cast auf das
Ergebnis von malloc erforderlich ist.
Dies liegt daran, dass man in C++ einen void* nicht einem anderen
Zeiger zuweisen darf. Beispiel:
char* str = malloc(10); // Fehler in C++
Ich habe mir Folgendes ausgedacht, um malloc dennoch gefügig zu
machen:
namespace test {
class malloc {
std::size_t sz;
|
Doch wohl eher
const std::size_t sz;
| Quote: | public:
malloc(std::size_t size) : sz(size) {}
template<typename T
operator T*() { return (T*)std::malloc(sz); }
|
Doch wohl eher
operator T*() const { return static_cast
| Quote: | };
}
void bla() {
#ifdef NOMALLOCTEST
using std ::malloc;
#else
using test::malloc;
#endif
char* str = malloc(10); // ok (wenn test::malloc aktiv)
// ...
}
Fällt jemandem ein Fall ein, wo dies zu Problemen führen könnte?
|
Selbstverständlich. Mal beide Verfahren nebeneinander.
char* str1 = static_cast<char*>(std::malloc(10));
char* str2 = test::malloc(10);
Gut, klappt. Aber:
char* str1a = 1 + static_cast<char*>(std::malloc(10));
char* str2a = 1 + test::malloc(10); // line 29
"trocken.cpp", line 29: Error: Cannot use test::malloc::T* to initialize char*.
Gruß
Volker
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Wed Nov 19, 2003 8:47 am Post subject: Re: C++ und C und malloc |
|
|
Volker Glave wrote:
| Quote: | Gut, klappt. Aber:
char* str1a = 1 + static_cast<char*>(std::malloc(10));
char* str2a = 1 + test::malloc(10); // line 29
"trocken.cpp", line 29: Error: Cannot use test::malloc::T* to initialize char*.
|
Der Ausdruck '1 + malloc(10)' sollte in C aber auch nicht akzeptiert werden,
da 'void*' standardgemäß keine Zeigerarithmetik unterstützt.
Comeau <http://www.comeaucomputing.com/tryitout> verhält sich hier standardkonform,
während mein gcc 3.3.1 als "kreative Erweiterung" die Zeigerarithmetik durchführt
(so als ob sizeof(void) == 1 wäre). Nur mit -pedantic gibt es eine Warnung.
Schlussfolgerung: Das beschriebene Problem sollte bei standardkonformem C-Kode
(um dessen vereinfachte Portierung nach C++ es dem OP ging) nicht auftreten.
Falk
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Michael Trocken Guest
|
Posted: Wed Nov 19, 2003 1:00 pm Post subject: Re: C++ und C und malloc |
|
|
[email]volker.glave (AT) web (DOT) de[/email] (Volker Glave) wrote
| Quote: | s1234569 (AT) hotmail (DOT) com (Michael Trocken) wrote
Bei der Weiterverwertung von in C geschriebenem Quell-Kode
[...] |
| Quote: | char* str1a = 1 + static_cast<char*>(std::malloc(10));
char* str2a = 1 + test::malloc(10); // line 29
"trocken.cpp", line 29: Error: Cannot use test::malloc::T*
to initialize char*.
|
Lässt sich denn
char* str3;
str3 = 1 + malloc(10);
mit einem C-Compiler kompilieren?
Gruß,
Michael
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Michael Trocken Guest
|
Posted: Wed Nov 19, 2003 1:34 pm Post subject: Re: C++ und C und malloc |
|
|
Daniel Albuschat <daniel (AT) viming (DOT) de> wrote
| Quote: | Markus Schaaf wrote:
"Michael Trocken" <s1234569 (AT) hotmail (DOT) com> schrieb:
Fällt jemandem ein Fall ein, wo dies zu Problemen führen könnte?
An Schnittstellen von Bibliotheken oder an Modulgrenzen werden in
C oft Zeiger auf Allocatorfunktionen übergeben.
An diesen (zumindest steltener, evtl. garnicht vorkommenden) Stellen,
reicht es dann aber, std::malloc zu schreiben. Namespace sei dank.
Die Idee finde ich uebrigens ziemlich gut... zumindest fuer diesen
Zweck. Und solange man darauf achtet, dass der (neue) C++ Code nicht
auch davon betroffen wird. Kannst du evtl. auch berichten, ob es
problemlos funktioniert hat?
|
Bei den paar Sachen, die ich bisher verwendet habe, ging es. Das ist
aber wohl nicht repräsentativ. Klar, dass die von Markus erwähnten
Allokatorfunktionen nicht in das Schema passen. Man müsste mal
beobachten, wie häufig das vorkommt.
Gruß,
Michael
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Wed Nov 19, 2003 2:01 pm Post subject: Re: C++ und C und malloc |
|
|
Michael Trocken wrote:
| Quote: | volker.glave (AT) web (DOT) de (Volker Glave) wrote
[email]s1234569 (AT) hotmail (DOT) com[/email] (Michael Trocken) wrote
Bei der Weiterverwertung von in C geschriebenem Quell-Kode
[...]
char* str1a = 1 + static_cast<char*>(std::malloc(10));
char* str2a = 1 + test::malloc(10); // line 29
"trocken.cpp", line 29: Error: Cannot use test::malloc::T*
to initialize char*.
Lässt sich denn
char* str3;
str3 = 1 + malloc(10);
mit einem C-Compiler kompilieren?
|
Abgesehen davon: Wer würde auf die Idee kommen, das zu programmieren?
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Wed Nov 19, 2003 2:11 pm Post subject: Re: C++ und C und malloc |
|
|
[email]s1234569 (AT) hotmail (DOT) com[/email] (Michael Trocken) writes:
| Quote: | Lässt sich denn
char* str3;
str3 = 1 + malloc(10);
mit einem C-Compiler kompilieren?
|
Hoffentlich nicht.
Das Resultat müsste ja sizeof(void) hinter den Rückgabewert von malloc()
zeigen.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Michael Klemm Guest
|
Posted: Wed Nov 19, 2003 4:55 pm Post subject: Re: C++ und C und malloc |
|
|
Hi,
Rolf Magnus wrote:
| Quote: | Lässt sich denn
char* str3;
str3 = 1 + malloc(10);
mit einem C-Compiler kompilieren?
Abgesehen davon: Wer würde auf die Idee kommen, das zu programmieren?
|
Jemand, der str3[-1] schreiben können möchte. Irgendwann in meinem
kurzen Leben stand sowas tatsächlich als Tip für C/C++-Programmierer.
Ist zwar unsauber, scheint aber machmal zu funktionieren :-)
-michael
--
Dept. of Computer Science 2, University of Erlangen-Nuremberg
Martensstrasse 3, D-91058 Erlangen, Germany
phone: ++49 (0)9131 85-28995, fax: ++49 (0)9131 85-28809
web: http://www2.informatik.uni-erlangen.de/~klemm
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Michael Trocken Guest
|
Posted: Thu Nov 20, 2003 7:02 am Post subject: Re: C++ und C und malloc |
|
|
Thomas Maeder <maeder (AT) glue (DOT) ch> wrote:
| Quote: | s1234569 (AT) hotmail (DOT) com (Michael Trocken) writes:
Lässt sich denn
char* str3;
str3 = 1 + malloc(10);
mit einem C-Compiler kompilieren?
Hoffentlich nicht.
Das Resultat müsste ja sizeof(void) hinter den Rückgabewert von malloc()
zeigen.
|
Außer in dem von Falk Tannhäuser beschriebenen Fall (bei gcc 3.3.1 als
"kreative Erweiterung") könnte das vielleicht akzeptiert worden sein,
wenn kein Prototyp für malloc vorlag (so dass als Rückgabewert int
angenommen wurde).
Das könnte man eventuell so behandeln:
namespace test {
class malloc {
const std::size_t sz;
const int offs;
public:
malloc(std::size_t size) : sz(size), offs(0) {}
malloc(const malloc& m, int os) : sz(m.sz), offs(os) {}
template<typename T>
operator T*() const {
return static_cast<T*>(std::malloc(sz)) + offs; }
};
inline const malloc operator+(int val, const malloc& m) {
return malloc(m, val);
}
inline const malloc operator+(const malloc& m, int val) {
return malloc(m, val);
}
// etc.
}
Allerdings würde ich dann eher dazu übergehen, die Stelle mit dem
beschriebenen Code zu überarbeiten.
Gruß,
Michael
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Michael Trocken Guest
|
Posted: Thu Nov 20, 2003 10:24 am Post subject: Re: C++ und C und malloc |
|
|
[email]s1234569 (AT) hotmail (DOT) com[/email] (Michael Trocken) wrote:
[...]
| Quote: | namespace test {
class malloc {
const std::size_t sz;
const int offs;
public:
malloc(std::size_t size) : sz(size), offs(0) {}
malloc(const malloc& m, int os) : sz(m.sz), offs(os) {}
template<typename T
operator T*() const {
return static_cast
|
Statt dessen:
template<typename T>
operator T*() const { return static_cast<T*>(
static_cast<char*>(std::malloc(sz)) + offs); }
| Quote: | };
inline const malloc operator+(int val, const malloc& m) {
return malloc(m, val);
}
inline const malloc operator+(const malloc& m, int val) {
return malloc(m, val);
}
// etc.
}
|
Gruß,
Michael
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Michael Trocken Guest
|
Posted: Thu Nov 20, 2003 10:34 am Post subject: Re: C++ und C und malloc |
|
|
Florian Weimer <fw (AT) deneb (DOT) enyo.de> wrote
| Quote: | s1234569 (AT) hotmail (DOT) com (Michael Trocken) writes:
Ich habe mir Folgendes ausgedacht, um malloc dennoch gefügig zu
machen:
namespace test {
class malloc {
std::size_t sz;
public:
malloc(std::size_t size) : sz(size) {}
template
operator T*() { return (T*)std::malloc(sz); }
};
}
Nette Idee. Ob das auch funktioniert, um die leichten Unterschiede bei
den Prototypen in den POSIX-Headern einiger Systeme glattzubügeln?
|
Um welche Unterschiede geht es dabei? Nur Rückgabe von void* wie bei
malloc oder auch andere Dinge?
Gruß,
Michael
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Volker Glave Guest
|
Posted: Thu Nov 20, 2003 4:50 pm Post subject: Re: C++ und C und malloc |
|
|
Falk Tannhäuser <falk.tannhauser (AT) crf (DOT) canon.fr> wrote
| Quote: | Volker Glave wrote:
Gut, klappt. Aber:
char* str1a = 1 + static_cast<char*>(std::malloc(10));
char* str2a = 1 + test::malloc(10); // line 29
"trocken.cpp", line 29: Error: Cannot use test::malloc::T* to initialize char*.
Der Ausdruck '1 + malloc(10)' sollte in C aber auch nicht akzeptiert werden,
da 'void*' standardgemäß keine Zeigerarithmetik unterstützt.
Comeau <http://www.comeaucomputing.com/tryitout> verhält sich hier standardkonform,
während mein gcc 3.3.1 als "kreative Erweiterung" die Zeigerarithmetik durchführt
(so als ob sizeof(void) == 1 wäre). Nur mit -pedantic gibt es eine Warnung.
Schlussfolgerung: Das beschriebene Problem sollte bei standardkonformem C-Kode
(um dessen vereinfachte Portierung nach C++ es dem OP ging) nicht auftreten.
|
Ok, dann halt so:
int i1 = NULL == static_cast<char*>(std::malloc(10));
int i2 = NULL == test::malloc(10); // line 32
"trocken.cpp", line 32: Error: The operation "int == test::malloc" is illegal.
1 Error(s) detected.
Volker
(2. Versuch, über Google Groups zu posten. 1. Versuch ist anscheinend
verschütt gegangen.)
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|