Hi Narue,

Ive started your tutorial on pointers and had a few questions about it if you don't mind.

1) You talked about the fact that a function has an adres itself and that you can call a function threw it's adres. This I understand, as I tried it out wether I could print the adres of a function a did this as follow:

void function(int, int)
{
...
}

int main()
{
...
cout<< function <<endl;
...
}

This worked as it should, thing is, I tried this out aswell with &function and *function and those two gave me the exact adres.

Question is, are all three correct possibilities to obtain the functions adres?

2) I tried out the example you showed about pointers to functions and had a problem that I got two errors, they stated the following:

C:\Program Files\Microsoft Visual Studio\MyProjects\VoorbeeldenInternet\PointersTutorialByNarue\Testing.cpp(39) : error C2440: 'initializing' : cannot convert from 'const void *' to 'const int *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast

wich is related to this piece of code:

int compare ( const void *a, const void *b )
{
  const int *ia = a;
  const int *ib = b;
  ...
}

Now, I understand what the error message is saying and I changed the code so that the parameter const void *a, get's cast like this:

const int *ia = static_cast <const int*> (a);

This way it works, but, I kept wondering wether this was not related towards either the compiler (MS VC 6.00) I'm using or some other reason :?:

Anyway, thanks for the superb tutorial, Ive allready learned alot ;)

>are all three correct possibilities to obtain the functions adres?
You can only do two things with a function: call it, and take its address. If you're not calling it, then the compiler assumes you're taking its address.

>I tried out the example you showed about pointers to functions and had a problem that I got two errors
The example (and all examples unless otherwise stated) was written in C. You've stumbled across one of the differences between C and C++, where C++ requires an explicit cast when converting back from a pointer to void. C performs this conversion implicitly.

Ok, thanks Narue :!:

Ive got a few more if you don't mind ;)

Ive read the part about incrementing and subtracting pointers, but I don't see the difference in the examples you show for those two, they're actually both the same :confused:
Also, I don't understand what the idea of these are, meaning, when would you use them?

The example:

#include <stdio.h>

int main ( void )
{
  int a[] = {1,2,3,4,5};
  int *p = a, *q = a + 5;

  printf ( "%td\n", p - q );
  printf ( "%td\n", q - p );

  return 0;
}

Also, when I execute the two examples for incrementing and subtracting, they both give me 'td' as result :confused:

But, when executing the one in wich you cast the result to (long) (atleast I think it's a cast :?: ) I get a result of -5 and 5. The same result is shown when I use cout?

Is this again related towards being C code?

The results of -5 and 5, are those the amount of pointers that have been used, or what are they?

Thanks for the help and explanation!

>when would you use them?
It's an easy way to determine the distance between two pointers. You use them when you need just that, but in general you won't need that feature often.

>they both give me 'td' as result
Your compiler doesn't support C99. ;)

>I get a result of -5 and 5. The same result is shown when I use cout?
That's the correct result, and cout would give you the same thing.

>The results of -5 and 5, are those the amount of pointers that have been used, or what are they?
It's the distance between the two pointers. The equivalent with indices is saying 5 - 0 and 0 - 5.

Hi Narue,

Ive tried to alter your example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *reverse ( const char *s )
{
  int i = 0, j = strlen ( s );
  char *save = malloc ( j + 1 );

  if ( save == NULL )
    return NULL;

  while ( j > 0 )
    save[i++] = s[--j];
  save[i] = '\0';
  
  return save;
}

int main ( void )
{
  char *p;

  p = reverse ( "J. Random Guy" );
  puts ( p );
  free ( p );

  return 0;
}

into this:

#include <iostream>

using namespace std;

char *reverse ( const char *s )
{
  int i = 0, j = strlen ( s );
  char *save = new (char) ( j + 1 );

  if ( save == NULL )
    return NULL;

  while ( j > 0 )
    save[i++] = s[--j];
  save[i] = '\0';
  
  return save;
}

int main ( void )
{
  char *p;

  p = reverse ( "J. Random Guy" );

  cout<< p <<endl;

  delete p;

  return 0;
}

I'm getting an error though wich seems to be related towards deleting p :?:

What am I doing wrong there?

Could you tell me also how I can cast the C example

char *save = malloc ( j + 1 );

so I can execute your example.

Thanks.

>new (char) ( j + 1 );
Beware that new is very flexible. This is a valid expression, but it doesn't do what you think it does. To allocate more than a single char, use new[]:

char *save = new char[j + 1];

And then be sure to use the corresponding delete[]:

delete [] p;

>What am I doing wrong there?
You're only allocating space for a single char, then initializing its value to j + 1. The result is that the subscripting done later trashes memory that you don't own, which is probably used for the memory manager's bookkeeping, and when you try to release the memory, the memory manager panics because it doesn't find what it's looking for.

>Could you tell me also how I can cast the C example
The easiest way is to just use a C-style cast:

char *save = (char*)malloc ( j + 1 );

Hey Narue,

Your tutorial on 'Pointers and dynamic memory'.

I executed your example in wich you use this piece of code

char *p = malloc ( sizeof "This is a test" );

Since I wanted to see how I had to write this in C++, I tried it out this way

char *p = new char[sizeof "This is a test"];

Though it worked, I wanted to ask wether, this is the correct way?

Hey Narue,

Your tutorial on 'Pointers and dynamic memory'.

I executed your example in wich you use this piece of code

char *p = malloc ( sizeof "This is a test" );

Since I wanted to see how I had to write this in C++, I tried it out this way

char *p = new char[sizeof "This is a test"];

Though it worked, I wanted to ask wether, this is the correct way?

Additional questions:
C code in your example:

base = malloc ( 2 * 3 * sizeof *base );
  p = malloc ( 2 * sizeof *p );

Altering this into C++ code, is this correct?

base = new int[2];
  p = new int*[3];

:?:

Also, how do I delete this double array?

Like this:

delete []base;

  for (i = 0; i < 3; i++)
	  delete [i] p;

I think I got the solution to define, initialize and delete the two dimensional array with pointers in C++ Narue.

#include <iostream>

using namespace std;

int main ()
{
	int **p;
	int i, j;

	p = new int*[2];

	for ( i = 0; i < 2; i++ ) 
	{
		p[i] = new int[3];
		for ( j = 0; j < 3; j++ )
			*( *( p + i ) + j ) = i * j;
	}

	for ( i = 0; i < 2; i++ )
	{
		for ( j = 0; j < 3; j++ )
		             cout<< p[i][j];
	cout<<endl;
	}

	for (i = 0; i < 2; i++)
		delete [] p[i];

	delete [] p;

	return 0;
}

Correct :?:

Also, to free the memory in the double array example written in C, is this correct then:

free (base);
free (p);

>char *p = new char[sizeof "This is a test"];
Yes, that will work. However, it's silly outside of an example like that, because if you have the string literal, you might as well just create a static array:

char a[] = "This is a test";

>base = new int[2];
>p = new int*[3];

base = new int[2 * 3];
p = new int*[2];

Remember that base holds all of the data, and p only points to the proper blocks.

>Also, how do I delete this double array?
Releasing the memory is simplicity itself:

delete [] p;
delete [] base;

Since p and base were allocated memory independently and neither relies on the other, there's no need for a loop.

>Correct?
Correct.

This article has been dead for over six months. Start a new discussion instead.