Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / problem with integer array being corrupted

Author
Message
draknir_
17
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 22nd Mar 2008 18:14 Edited at: 22nd Mar 2008 18:17


the function that calls log_CalcVis outputs the contents of the 2d array vis to the screen. Its called like this:



It should be all 0, but I get this:



what on earth is going on?? some kind of memory leak? When I move my mouse the values flicker to other numbers and then back.

(using express edition visual c++/DarkGDK)
(ps: map is another [mapw][maph] size array, dont know if that matters..)
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 22nd Mar 2008 18:39
Are you sure the compiler will handle this:



I don't think it work that way, unless you give values to mapw,maph at compilation time.
draknir_
17
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 22nd Mar 2008 18:58
yea sorry i should have mentioned, mapw and maph are const ints defined at the top of the program.
Abzero
16
Years of Service
User Offline
Joined: 22nd Mar 2008
Location:
Posted: 22nd Mar 2008 20:42
hmm, your function prototype and call is slightly wrong.

What you are outputing is random data; this is because the function you have created is not returning the array its zero'd; This function





only returns one integer; this happens to be the entry 1 past the end of the map; (since arrays start at 0.)

If you meant to return the array, your'd need to dynamically alloacte the array and return it as a pointer.



You then call this like:



but you need to free the array properlly. A better approach would be to require to be put into the function:



and call like:

int vis[maph][mapw];

log_CalcVis(map, px, py, vis);

Hope this helps
Rye
21
Years of Service
User Offline
Joined: 30th May 2003
Location: United Kingdom, Blackrod
Posted: 22nd Mar 2008 21:14
you can use a function in windows.h

#include "windows.h"

// then where you want to zero it.
SecureZeroMemory(vis);

think thats the one. google it.

very useful command in my experience.
draknir_
17
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 23rd Mar 2008 07:09 Edited at: 23rd Mar 2008 07:15
ok thanks for your answers guys, but I got it working just by zeroing the vis array in the parent function, then passing it to log_CalcVis. Its not beautiful code, but it works and thats all i need for now. Next time ill do it by dynamically assigning the array, but it seems like a lot of trouble..

heres an image of the vis function doing what its meant to do XD
Abzero
16
Years of Service
User Offline
Joined: 22nd Mar 2008
Location:
Posted: 23rd Mar 2008 09:51
just be careful;

as I said before the way you are calling those functions are not working the way you think they are.

It works when you zero the array in the parent function because this array is not touched by the second function; therefore you had uninitised values in the array for the parent function. If you do it again I would stongly suggest you try the second method which involves passing the array in as a parameter to the function (a in/out parameter.)
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 24th Mar 2008 18:24
You just cheat and make the array global... fixed... via a namespace or just in the top of your MAIN (Namespace would span multiple source files though easy enough) and just work it like that.

I personally have a little class I wrote for dynamic arrays - but its single array MyArray->ar[?]

But I just make that array single array as long as one dimension, and have the values be not integers - but pointers to another instance of the same class for the other dimension. Sounds hardthan it is:



My Header file - jfc_ar.h


My jfc_ar.cpp file


Best of luck to ya

draknir_
17
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 25th Mar 2008 13:40
@AbZero: You're saying that this:

return vis[mapw][maph];

is returning only a single value from the array, but thats impossible, because thats what im using right now and it works fine.. the array is passed into the function and then returned, the only difference with my previous code being that the array is initialized before it is passed.

@Jason: Thanks for the intricate array code Ill probably try something like that for a bigger project.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Abzero
16
Years of Service
User Offline
Joined: 22nd Mar 2008
Location:
Posted: 26th Mar 2008 21:19
@draknir_

i'm afriad not.

You see before calling the function you create the array:

int map[mapw][maph];

then your calling the function:

int cal(int map[mapw][maph], int x, int y);

inside that function you are creating another array. You then think your returning that created array, but your not; your returning a single value; this value has no consequence because you are assigned it to an area of the array which is not inside the array itself (and at risk of a SIGFAULT.)

The array outside of the function is different to the array inside the function and the two never meet.

What you need to do to convince yourself that the arrays are
not the same; is this; inside the calc function make it initialse each of the elements to 1. Then check the values of the array after the function call. My bet is that the vis array in the parent function is still all zero'd.

Login to post a reply

Server time is: 2024-09-29 15:28:18
Your offset time is: 2024-09-29 15:28:18