Is there a way to ensure that all un-initialized pointers automatically have the address 0x00000000? Sometimes they're being set to other addresses but still being unitialized, and they pass tests like if(!ptr), causing an unhandled access violation error.

Just set the pointer to NULL.

void *vptr = (void*)NULL;

You should get into a habit of initializing all pointers to NULL and all freed pointers to NULL.

Edited 6 Years Ago by gerard4143: n/a

I'm using a pointer to a pointer, to have an array of pointers, and a new item in the array only gets added when the (!ptr) test returns true, which it doesn't because the address of the pointer isn't 0x00000000. Here, I'll post some code:

if (!m->verts)
	{
		m->verts = malloc (sizeof(mvertex_t));
		Q_memset (m->verts, 0, sizeof(mvertex_t));
	}

	if (!m->verts[m->numverts])
	{
		m->verts[m->numverts] = malloc (sizeof(mvertex_t) * m->numverts+1);
		Q_memset (m->verts[m->numverts], 0, sizeof(mvertex_t));
	}

In this code, if m->verts is NULL (i set it to null before) then it gets initialized and is all good. But I can't set m->verts[m->numverts] to NULL because it hasn't been allocated yet and for some reason it's getting set to 0xfdfdfdfd.

Edited 6 Years Ago by tomtetlaw: n/a

>Sometimes they're being set to other addresses but still being unitialized, and
>they pass tests like if(!ptr), causing an unhandled access violation error.

That's the whole point, genius. If you're trying to use an uninitialized pointer, that's a Bad Thing™, and it should be immediately fatal as it's a programming error.

>But I can't set m->verts[m->numverts] to NULL because it hasn't been
>allocated yet and for some reason it's getting set to 0xfdfdfdfd.

I fail to see why you can't set m->verts[m->numverts] to NULL . Your statement suggests a misunderstanding of the difference between a pointer and the memory it points to.

>That's the whole point, genius.
I know that, it's just sometimes m->verts[m->numverts] is being set to 0x00000000 and sometimes it isn't, and it is still unitialized.

I can't set m->verts[m->numverts] to NULL because at the point before if (!m->verts[m->numverts]) , m->verts[m->numverts] doesn't exist as m->verts is only malloc'd to a size of (sizeof(mvertex_t)*m->numverts-1). Only in that if statement does m->verts[m->numverts] exist.

Edited 6 Years Ago by tomtetlaw: n/a

>I can't set m->verts[m->numverts] to NULL because at the point before
>if (!m->verts[m->numverts]) , m->verts[m->numverts] doesn't exist

You do realize how ridiculous this sounds, right? "I can't set the object to null because the object doesn't exist when I test it for null". If your code is as whack as I'm seeing and you're describing, it's a wonder it works even some of the time.

Oh. I realise now, I feel like an idiot. I fixed the problem anyway though. I just, instead of using a pointer to a pointer as an array of pointers, I use one pointer as an array of values.

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