@related: CPlusPlus ptr

allocation functions seems to work even if null pointer address passed as reference ?

#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);
}

misc

address.txt · Last modified: 2022/04/16 12:22 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki