Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

java the hut

Name: Anonymous 2010-10-13 22:41


int i;
char **x = malloc(sizeof(char *) *600);
for(i=0;i<600;i++)
    x[i] = malloc(sizeof(char) * 500);

//do some work


for(i=0;i<600;i++)
    free(Counter[i]); //error
free(Counter);


what did i do wrong?

Name: Anonymous 2010-10-13 22:58


for(i=0;i<600;i++)
    free(x[i]); //error
free(x);


ignore Counter

Name: Anonymous 2010-10-13 23:27

>>2
ignore counter
Because you want us to guess at the problem without knowing what 'error' you're talking about (or even whether it's compile time or run time) and you don't want us to know that you did int Counter = x; at one point, which is the source of all your problems? Yeah? Well fuck you.

Name: Anonymous 2010-10-13 23:31

>>2
You need to delete in the opposite order that you allocated.
for (int i = 600; i > 0; i--)
     free(x[i-1]);
free(matrix);

Name: Anonymous 2010-10-13 23:31

>>3

HEAP[aids.exe]: Invalid address specified to RtlFreeHeap( 00470000, 00473E58 )
Windows has triggered a breakpoint in aids.exe.

This may be due to a corruption of the heap, which indicates a bug in aids.exe or any of the DLLs it has loaded.


@ free(x[i]); //i==0 triggers it

run time

and no, Counter has nothing to do with it. I just wrote down the wrong name when posting.

Name: Anonymous 2010-10-13 23:37

>>4
[code]
First-chance exception at 0x7762dc9b in aids.exe: 0xC0000374: A heap has been corrupted.
Unhandled exception at 0x7762dc9b in aids.exe: 0xC0000374: A heap has been corrupted.
[code]

that worked till it got to about x[80-70] range then it error'd again

Name: Anonymous 2010-10-13 23:56

>>6
Use delete[] then

Name: Anonymous 2010-10-13 23:58

>>7
there's no delete[] in C

Name: Anonymous 2010-10-14 0:05

>>5
Did you assign anything directly to x[foo] instead of writing it to the memory pointed there? That could certainly do it.

Name: Anonymous 2010-10-14 0:07

>>4
No he needn't.

>>6
The mistake is elsewhere, in a section of code you haven't posted. You're probably changing x somehow.

Name: Anonymous 2010-10-14 0:08

>>9
nope, also this is the only code in between that did anything with it


    for(ix = 0;ix<=Size;ix++)
        for(iy = 0;iy<=Size;iy++){
            x[ix][iy] = rand()%(int)(Map_Size_Z*0.9);
            if (x[ix][iy] < Height_Water)
                x[ix][iy] = (x[ix][iy] + Height_Water*15) / 16;
            if (x[ix][iy] > Height_Water && x[ix][iy] < Height_Grass)
                x[ix][iy] = (x[ix][iy] + Height_Water*80) / 81;
        }

    for(i = 1;i<=Iterations;i++){
        if (i >= Iterations-1)
            RND_Factor = 0;
        else
            RND_Factor = 0.010*(Iterations-i);
       
       
        for(ix = Size;ix>=0;ix--)
            for(iy = Size;iy>=0;iy--)
                x[ix*2][iy*2] = x[ix][iy];

        for(ix = 0;ix<=Size-1;ix++){
            for (iy = 0;iy<=Size-1;iy++){
                if (x[ix*2][iy*2] <= Height_Water)
                    RND_Factor_2 = RND_Factor * 0.5;
                else if (x[ix*2][iy*2] <= Height_Grass)
                    RND_Factor_2 = RND_Factor * 0.3;
                else
                    RND_Factor_2 = RND_Factor;
                x[ix*2][iy*2+1] = (x[ix*2][iy*2] + x[ix*2][iy*2+2]) / 2 + ((rand()%255-128)*RND_Factor_2);
                x[ix*2+2][iy*2+1] = (x[ix*2+2][iy*2] + x[ix*2+2][iy*2+2]) / 2 + ((rand()%255-128)*RND_Factor_2);
               
                x[ix*2+1][iy*2] = (x[ix*2][iy*2] + x[ix*2+2][iy*2]) / 2 + ((rand()%255-128)*RND_Factor_2);
                x[ix*2+1][iy*2+2] = (x[ix*2][iy*2+2] + x[ix*2+2][iy*2+2]) / 2 + ((rand()%255-128)*RND_Factor_2);
               
                x[ix*2+1][iy*2+1] = (x[ix*2][iy*2] + x[ix*2+2][iy*2] + x[ix*2][iy*2+2] + x[ix*2+2][iy*2+2]) / 4 + ((rand()%255-128)*RND_Factor_2);
            }   
        }
        Size = Size * 2;
    }

Name: Anonymous 2010-10-14 0:17

>>11
Okay fuck this shit.

Name: Anonymous 2010-10-14 0:27

>>11
Size better be less than 250 / 2Iterations.

Name: Anonymous 2010-10-14 0:35

Thanks for the help guys. . . i found my mistake and fixed it.

Name: Anonymous 2010-10-14 1:16

>>14
DON'T HELPyourself

Name: Anonymous 2010-10-14 1:50

Here is a ``better'' way to construct a dynamic n-by-m array. Assume n and m are of type size_t. Handling of arithmetic wrap-around (if necessary) and malloc failure are omitted for brevity.

type **x = malloc(n * sizeof *x);
x[0] = malloc(n * m * sizeof *x[0]);
for (size_t i = 1; i < n; i++)
  x[i] = x[i-1] + m;

// ...

free(x[0]);
free(x);

Name: Anonymous 2010-10-14 2:30

>>16
He sure should be using something other than literals for n,m but wow, n * m * sizeof *x[0] sure is hideous.

sizeof(type[n*m]) is tight.

Name: Anonymous 2010-10-14 2:43

>>17
You can still go one step further: sizeof (type[n][m]).

Name: Anonymous 2010-10-14 6:39

If the text rows are always of the same size {tt m}, then just make one allocation.

{code C

char **x = malloc (n * sizeof *x + m * sizeof **x);
char *y = (char*)(x + n);
for (i = 0; i < n; i++)
    x[i] = y + i*m;
}

Name: Anonymous 2010-10-14 8:16

>>19
You're missing a multiplication by n in your size calculation. Regardless, it's a bad idea to store multiple types inside one allocated area.

Name: Anonymous 2010-10-14 8:55

Just use a vector of vectors

Name: Anonymous 2010-10-14 11:43

std::vector< std::vector< double > > m;

sepples

Name: Anonymous 2010-10-14 19:38

for(ix = 0;ix<=Size;ix++)
Given that OP's probably a sepples coder, I'm guessing he hasn't spotted the more than likely off-by-one error right there

Name: Anonymous 2010-10-14 19:46

>>23
It's not an off-by-one. Size is clearly supposed to be much smaller than the array bounds in any direction.

Name: Anonymous 2010-10-14 20:08

>>24
If you say so, Mr ICanReadOP'sFilesFromHere

Name: Anonymous 2010-10-14 20:29

>>25
If you looked at any other part of the code or read >>13 it would have been obvious to you too.

Name: Anonymous 2010-10-14 22:23

>>1

you programmed in c

Name: Haxus the Greedo 2010-10-14 23:39

Java has put a price on your head so large, every bounty hunter in the galaxy will be looking for you.

Name: Anonymous 2010-10-15 0:28

>>23
as i stated above i solved the issue. Here's the whole method if anyone cares
http://pastebin.com/bKbpG5tK C version
http://pastebin.com/CtdaKcEC Lua version


I still need to fix up the C version though because it generates really shitty terrain compared to the Lua version which works great when i embed it.

Name: Anonymous 2010-10-15 0:56

>>29
That's some daunting looking code, but surely you want RND_Factor and RND_Factor_2 to be of type float, not int. Maybe that's the problem.

Name: Anonymous 2010-10-15 1:34

>>30
uh... Thanks for pointing that out, that should explain the shitty generation using the C version

Name: Anonymous 2010-10-15 23:14

>>29
Ok, get the fuck out then.

Name: Anonymous 2010-10-16 1:01

>>32
youve just been HURT FEELINGS AND BUTT RANGED go drink ur moms bredt milk u fart commander i bet u hav a fetish FOR MEN LMAO ur just so made all the time its 2 easy 23 own u "i like to drnikj sperm from my spermbottle while waring my sperm necklace" - u

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List