This shows you the differences between two versions of the page.
address [2011/07/15 10:42] |
address [2022/04/16 12:22] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | @related: [[CPlusPlus]] [[ptr]] | ||
+ | |||
+ | allocation functions seems to work even if [[null]] pointer [[address]] passed as [[reference]] ? | ||
+ | |||
+ | |||
+ | <code> | ||
+ | #include <cstdio> | ||
+ | #include <cstdlib> | ||
+ | #include <iostream> | ||
+ | #include <string> | ||
+ | using namespace std; | ||
+ | |||
+ | #define cdebug cout | ||
+ | #define TRACE(x) { cdebug<<#x<<"="<<(x)<<endl; } | ||
+ | |||
+ | |||
+ | int falloc(char*& p, int & l) | ||
+ | { | ||
+ | cdebug<<__FILE__ <<":" <<__LINE__ << " ("<< __PRETTY_FUNCTION__<<")"<<endl; | ||
+ | cdebug<<(hex)<<(void*)&p<<endl; // non null address : ok | ||
+ | l=1; | ||
+ | p = new char[l]; | ||
+ | p[0]='1'; | ||
+ | } | ||
+ | |||
+ | int main(int argc, char* argv[]) | ||
+ | { | ||
+ | { | ||
+ | char* p = 0; int l=1; | ||
+ | TRACE( falloc(p,l) ); | ||
+ | cdebug<<p[0]<<endl; // ok:1 | ||
+ | } | ||
+ | { | ||
+ | char* p = (char*) "0"; int l=1; | ||
+ | TRACE( falloc(p,l) ); | ||
+ | cdebug<<p[0]<<endl; // ok:1 | ||
+ | } | ||
+ | exit(0); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== misc ==== | ||
+ | |||
+ | * http://www.frogatto.com/?p=3 | ||
+ | * http://cslibrary.stanford.edu/104/ | ||
+ | * http://www.penspinning.fr/s777-at-thetournament-2011-3544 | ||