At Thursday 9/11/2008 18:41, you wrote:
> Even better:
>
>-- ~Rick <[EMAIL PROTECTED]> wrote:
> >
> > I am trying to print the address of a variable, not the contents of it.
> >
> > Using printf, I would say:
> >
> > printf("Allocated %ld bytes at %p\n", bsize+1, fbuf);
> >
> > but I want to use the C++ features using cout/cerr. I've tried the
> > following but get garbage:
> >
> > long int bsize = 1023;
> > char *fbuf;
> > fbuf = new char[bsize+1];
> > if (fbuf)
> > {
>
>// Let the compiler work for you:
> cerr << "Allocated " << (bsize+1) << " bytes at " <<
> (void*)fbuf << endl;
>
> > delete [] fbuf;
> > }
> >
Pedro,
Thank you. Casting to a void ptr works.
Tyler,
> just use &, like:
> int i=0;
> cout << &i;
Sure, this will work, because you are SPECIFYING the address of the INT.
> if you have a pointer:
> int i=new int();
> cout << i << endl;
> works great.
This does NOT work. You would need to specify int *i=new int();
int *i=new int();
*i = 5;
cout << *i << endl; // Prints 5 (but you need the indirection)
cout << i << endl; // Prints 0xde1040
cout << (void *)i << endl; // Prints 0xde1040
char * c = new char[5];
strcpy(c, "test");
cout << *c << endl; // Prints t
cout << c << endl; // Prints test
cout << (void *)c << endl; // Prints 0xde6c80
char *ch = new char[1];
*ch = 'A';
cout << *ch << endl; // Prints A
cout << ch << endl; // Prints A (followed by GARBAGE)
cout << (void *)ch << endl; // Prints 0xde3540
So, it appears the char data type can ONLY be dynamically created as
an array of char.
~Rick
[Non-text portions of this message have been removed]