array pointer

Started by
7 comments, last by jagguy 17 years, 11 months ago
what is the reason why you would have this code, as I don't see the memory benefit of this. D3DXCreateTextureFromFile( , , &g_pMeshTextures ) why would a function reqire an address of an array because an array is a pointer , why couldn't this do as a parameter g_pMeshTextures
Advertisement
g_pMeshTextures is a value , &g_pMeshTextures is a pointer to that value. it might be possible to just do g_pMeshTextures+i instead.

From DX sdk:

HRESULT D3DXCreateTextureFromFile(
LPDIRECT3DDEVICE9 pDevice,
LPCTSTR pSrcFile,
LPDIRECT3DTEXTURE9 * ppTexture
);


Where LPDIRECT3DTEXTURE9 is of type "idirect3dtexture9 *"

The function expects to get a pointer to a pointer as the third parameter.
Yes, it could be a pointer to an array also, but in this case it isn't.

If the ppTexture would be just a pointer, the function wouldn't be able to modify the actual pointer, just the data it is pointing to.

Consider that you have

LPDIRECT3DTEXTURE9 pTexture = NULL;

and then you send this pointer as a pointer to a function. The function will get only a pointer which has value of NULL. The function wouldn't be able to modify the value of the pointer since it wouldn't know the location of the pointer.

So, a pointer to a pointer means "the address where the pointer itself is located"

Best regards


Quote:Original post by Demus79

From DX sdk:

HRESULT D3DXCreateTextureFromFile(
LPDIRECT3DDEVICE9 pDevice,
LPCTSTR pSrcFile,
LPDIRECT3DTEXTURE9 * ppTexture
);


Where LPDIRECT3DTEXTURE9 is of type "idirect3dtexture9 *"

The function expects to get a pointer to a pointer as the third parameter.
Yes, it could be a pointer to an array also, but in this case it isn't.

If the ppTexture would be just a pointer, the function wouldn't be able to modify the actual pointer, just the data it is pointing to.

Consider that you have

LPDIRECT3DTEXTURE9 pTexture = NULL;

and then you send this pointer as a pointer to a function. The function will get only a pointer which has value of NULL. The function wouldn't be able to modify the value of the pointer since it wouldn't know the location of the pointer.

So, a pointer to a pointer means "the address where the pointer itself is located"

Best regards


q)i got a little confused, could you put this into a form of say a 2d array of chars, a simpler datatype.

q)g_pd3dDevice->SetMaterial( &g_pMeshMaterials );
g_pd3dDevice->SetTexture( , g_pMeshTextures );
both parameters for each are supposed to be a pointer but as you can see 1 is a **

[Edited by - jagguy on May 30, 2006 5:56:15 AM]
The clue here is that C++ makes a copy of all function arguments, no matter what.

So if you write a function that takes a pointer as argument, C++ will make a copy of this pointer, and use that copy inside the function.
When the function exits this copy is deleted.
Example function:void load_texture(texture_t* tex2){  tex2 = load_texture_from disk();}Example usage:texture_t* tex1;load_texture( tex1 );

As you probably know, a pointer is the same as a variable in the sense that it has its own memory address, and its own size.
Lets say that the pointer tex1 get memory address 1000 when it is created.

When we call the load_texture function, C++ makes a copy of the argument (tex1), called tex2.
Lets say that the copy (tex2) gets memory address 2000.

Inside the function, the address returned from load_texture_from_disk is placed in address 2000 (tex2), not address 1000 (tex1).

You see the problem?

When the function returns, tex2 is deleted. More importantly, address 1000 is unchanged. Infact it has never been used, so the address returned from load_texture_from_file is lost.

The work-around for this problem is to write the function like this:
void load_texture(texture_t** tex2){  *tex2 = load_texture_from disk();}And use it like this:texture_t* tex1;load_texture( &tex1 );

If you examine whats going on here the same way as I just did, you will see that the expression *tex2 is a pointer at address 1000. In other words, it is tex1.

Explaining this is not easy. I remember accepting this "work-around" for quite some time before I suddenly understood it.

You can check out my last attempt to explain this here

Quote:
g_pd3dDevice->SetMaterial( &g_pMeshMaterials );

Why a pointer to pointer is used by the SetMaterial function beats me.
I cant see why SetMaterial would need to change the address stored in g_pMeshMaterials
thanks for the replies. Pointer to pointer isn't that difficult to use under normal circumstances (not all!)but I wanted to know why you need to do it this way.
The problem with directx is that part of it you just work it, and not really understand how it works.
Poeple can write software by being more complicated than they need to.

If you ask me, MS never wanned you to understand whats going on without doing a MS speciffic course about the subject. And the amount of MS speciffic courses you need to learn DirectX is vast.

Its feels like beeing fed with white lies over and over.
Im gonna divorce from MS one day....
Quote:Original post by pulpfist
If you ask me, MS never wanned you to understand whats going on without doing a MS speciffic course about the subject. And the amount of MS speciffic courses you need to learn DirectX is vast.

Its feels like beeing fed with white lies over and over.
Im gonna divorce from MS one day....


1) DirectX has some of the best (and it's free!) documentation out there. Compared to the official OpenGL documentation, it's light-years ahead. There's no need to take any course whatsoever.

2) However, if you mean, learn what DirectX does "under the hood", there'll be no course, because it'll change from driver to driver. And the software side is proprietary. So become an employee, or a MVP, and you might stand a chance. As it is, there's no need to (and you could take a general 3D mathematics and animation course, which will cover all the general areas, such as rasterisation, etc).

@jagguy

The reason DirectX always requests the address of the structure you're initialising is because it's built on the COM (COM+) model, where everything is mandated to returl HRESULT, no ifs or buts. So the only way to return data is via an "out" parameter, a la the address of structures.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
I thought it might be a bit amateurish of me just using directX code without really knowing how it works under the hood. I take it so long as you can get it to work , and you know maths then that's all you want.

This topic is closed to new replies.

Advertisement