Results 1 to 12 of 12

Thread: Any C gurus here? Variables interacting in funny ways.

  1. #1
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Any C gurus here? Variables interacting in funny ways.

    I'm writing a program that simulates kinetic growth. You start off with a single "seed" in a grid and you add particles with random walks from the grid edge and if they collide, they add to it. Each time a particle collides and joins with the whole blob, another particle is emitted. Result is a pretty fractal type picture.

    I've got that bit done, it gives nice pretty pictures when the output file is put into graphing software. You note i don't say Excel explicitly because i had problems with it hanging when it tried to make a scatter graph with 10000 data points. I had much better luck in Origin and the result is below:



    Parameters are grid size 501 with i think 20000 iterations.

    I'm reluctant to post the source code because this is an active project for uni (there are three optional projects for the module, so there'll be lots of people solving this same problem) and i don't want to get plagiarised - it's not due in until the end of Jan. So on the off chance that someone googles and finds this page However, hopefully this can be solved without need for too much code.

    Aaaaaannnyyway...

    There are some more requirements for the code spec, like working out the fractal dimension of the image. This is fine, i know how to do it and i know how i want to code it.

    At the moment, i've calculated the centre of mass of the "blob" and it prints in the terminal window so i can keep track of it (what we physicists would call a sanity check). Trouble is, as soon as I declare another variable, be it called k, dist, fgkafklfsak - whatever. The values for the centre of mass change massively (normally to some huge number in the tens, hundreds of thousands).
    Example:

    Vanilla -

    With an added variable -


    The variable name, as I said, makes no difference. Nothing happens besides the variable being declared.

    Any idea what could cause it?

  2. #2
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: Any C gurus here? Variables interacting in funny ways.

    That shouldn't happen, unless you're doing something naughty in your decl/alloc/memory management.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  3. #3
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: Any C gurus here? Variables interacting in funny ways.

    I did wonder if malloc had something to do with it, but i can't see how, all i've got is:

    unsigned int **lattice,lattice_size;
    unsigned int **particletable;

    lattice = (unsigned int**)malloc((lattice_size+1)*sizeof(unsigned int));

    for(i = 0; i < lattice_size;i++)
    lattice[i] = (unsigned int*)malloc((lattice_size+1)*sizeof(unsigned int));

    particletable = (unsigned int**)malloc((max_iteration+1)*(sizeof(unsigned int)));

    for(i = 0; i < max_iteration;i++)
    particletable[i] = (unsigned int *)malloc((max_iteration+1)*sizeof(unsigned int));

    And then:

    for(i = 0; i < lattice_size;i++)
    free(lattice[i]);

    for(i = 0; i < max_iteration;i++)
    {
    free(particletable[i]);
    }

    free(lattice);
    free(particletable);

    At the end of the program

  4. #4
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts

    Re: Any C gurus here? Variables interacting in funny ways.

    Why are you adding 1 to the size of lattice and particletable when using malloc? All your for loops ignore the last element. Also it might be worth using a tool such as valgrind or gdb to see if anything funny is going on with memory.

  5. #5
    Senior Member manwithnoname's Avatar
    Join Date
    Dec 2005
    Posts
    1,050
    Thanks
    17
    Thanked
    26 times in 25 posts

    Re: Any C gurus here? Variables interacting in funny ways.

    Quote Originally Posted by Whiternoise View Post
    I did wonder if malloc had something to do with it, but i can't see how, all i've got is:

    unsigned int **lattice,lattice_size;
    unsigned int **particletable;

    lattice = (unsigned int**)malloc((lattice_size+1)*sizeof(unsigned int));

    for(i = 0; i < lattice_size;i++)
    lattice[i] = (unsigned int*)malloc((lattice_size+1)*sizeof(unsigned int));

    particletable = (unsigned int**)malloc((max_iteration+1)*(sizeof(unsigned int)));

    for(i = 0; i < max_iteration;i++)
    particletable[i] = (unsigned int *)malloc((max_iteration+1)*sizeof(unsigned int));

    And then:

    for(i = 0; i < lattice_size;i++)
    free(lattice[i]);

    for(i = 0; i < max_iteration;i++)
    {
    free(particletable[i]);
    }

    free(lattice);
    free(particletable);

    At the end of the program
    bit rusty on pointers to pointers, if I understand correctly the 2 lines highlighted in red should be allocating memory for a set of pointers so you should be allocating lattice_size* size of the pointer, you are doing lattice_size * size of unsigned int

    if unsigned int is 4 bytes in size and the size of your pointers are 8 bytes (eg 64bit address space) you will be allocating only half the required memory.

    (may output the sizeof(unsigned int*) and sizeof(unsigned int) and see if they are different sizes)

    try:
    lattice = (unsigned int**)malloc((lattice_size+1)*sizeof(unsigned int*));

    or this might be a bit more readable:
    unsigned int *ptr_unsigned_int;
    ...
    lattice = (unsigned int**)malloc((lattice_size+1)*sizeof(ptr_unsigned_int));

  6. #6
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: Any C gurus here? Variables interacting in funny ways.

    Ok i've got it to work by, i'll admit - shotgun editing and a fudge. I basically rewrote a few chunks and removed some redundant code to make it more efficient but somewhere it seems to have worked.

    I'll have a look at what's going on, in the mean time it works and i get a nice output

    I did change the mallocs to allocate memory for pointers to ints rather than just ints - but even without malloc i had the same problem.
    Last edited by Whiternoise; 05-12-2009 at 10:38 PM.

  7. #7
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts

    Re: Any C gurus here? Variables interacting in funny ways.

    Didn't even spot you were doing sizeof(unsigned int)! To be fair I was tired when reading that code and I'm tired now I'm still not understanding why you're doing:
    Code:
    lattice = (unsigned int**)malloc((lattice_size+1)*sizeof(unsigned int*));
    Should it not be:
    Code:
    lattice = (unsigned int**)malloc((lattice_size)*sizeof(unsigned int*));
    The way you're doing it you're getting an (N+1)xN 2D array instead of an NxN 2D array which I presume is what you want. The N+1 element will have garbage in it but fortunately you're not calling free on it since your for loop is as follows:
    Code:
    for(i = 0; i < lattice_size;i++)
        free(lattice[i]);
    This does only N frees which is what you want to be doing. I hope that makes sense

    Out of interest, were any of your functions returning pointers to local variables?

  8. #8
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: Any C gurus here? Variables interacting in funny ways.

    But if i don't do +1, when i run through that for loop i get a bus error...

  9. #9
    Senior Member manwithnoname's Avatar
    Join Date
    Dec 2005
    Posts
    1,050
    Thanks
    17
    Thanked
    26 times in 25 posts

    Re: Any C gurus here? Variables interacting in funny ways.

    Quote Originally Posted by Whiternoise View Post
    But if i don't do +1, when i run through that for loop i get a bus error...
    I think you must have got some of your array calculations wrong.
    Normally the index would be from 0 to ("index bound" -1), the index has "index bound" values so you would allocate memory based on this value.

    Your code matches this idea, ie for(i = 0; i < lattice_size;i++)
    So you should be allocate lattice_size * size of data type.

    If you need to allocate extra memory to prevent an error then you are probably writting past the array size.

    Do you have a debugger?
    Yes, let it crash, and have a look at the index used to access the array
    No:
    - write debug infomation in the loops so when the program crashes you can see what's happening
    (you might be able to use assert but i've never used this)

  10. #10
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: Any C gurus here? Variables interacting in funny ways.

    Sorry, I'm not being very constructive or helpful here, but:

    God C is a horrible language.
    I like C++ quite a lot, as it seems to have evolved quite a bit. But C is just a horror.
    Are you allowed to change to using Python?

  11. #11
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: Any C gurus here? Variables interacting in funny ways.

    I don't mind it too much, i only 'properly' use it for embedded design (i.e AVRs) and it's far preferable to ASM for the moment.

    I want to learn Python because it seems pretty useful, but the module was specifically C based so no. As for C++, i've dabbled in it, but i've never needed to use it yet so perhaps later.

  12. #12
    Senior Member manwithnoname's Avatar
    Join Date
    Dec 2005
    Posts
    1,050
    Thanks
    17
    Thanked
    26 times in 25 posts

    Re: Any C gurus here? Variables interacting in funny ways.

    Quote Originally Posted by Whiternoise View Post
    I don't mind it too much, i only 'properly' use it for embedded design (i.e AVRs) and it's far preferable to ASM for the moment.

    ...
    Bring it on, I've many happy memories of reordering code for multiple loops so I could add short jumps ... honest!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •