Thread: Strange memory corruption

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    Strange memory corruption

    Hi,

    I have a strange case of "memory corruption", if I can call it that way...

    I have a struct:

    Code:
    typedef struct
    {
    	uint64_t localizacion;
    	uint64_t tipo;
    	float azimuth[64];
    	float altura[64];
    	float tilt[64];
    } individuo;
    And then, I have two arrays of that type:

    Code:
    individuo *padres, *hijos;
    Which I initialize witch calloc:

    Code:
    padres = calloc ( 64, sizeof ( individuo ) );
    hijos = calloc ( 64, sizeof ( individuo ) );
    I initialize them with random values:

    Code:
    	for ( i = 0; i < 64; i++ )
    	{
    		// Localizaciones
    		srand ( rand() );
    		padres[i].localizacion = ( uint64_t ) ( ( pow ( 2, 32 ) * rand() ) / RAND_MAX ) << 32;
    		srand ( rand() );
    		padres[i].localizacion = padres[i].localizacion | ( uint64_t ) ( ( pow ( 2, 32 ) * rand() ) / RAND_MAX );
    
    		// Tipo de antena
    		srand ( rand() );
    		padres[i].tipo = ( uint64_t ) ( ( pow ( 2, 32 ) * rand() ) / RAND_MAX ) << 32;
    		srand ( rand() );
    		padres[i].tipo = padres[i].tipo | ( uint64_t ) ( ( pow ( 2, 32 ) * rand() ) / RAND_MAX );
    
    		//printf("Individuo %d: %llx - %llx\n",i,padres[i].localizacion,padres[i].tipo);
    
    		for ( j = 0; j < 64;j++ )
    		{
    			// Altura
    			srand ( rand() );
    			padres[i].altura[j] = 1 + 14 * ( ( float ) rand() / ( float ) RAND_MAX );
    
    			// Azimuth
    			srand ( rand() );
    			padres[i].azimuth[j] = 5 * ( ( float ) rand() / ( float ) RAND_MAX );
    
    			// Tilt
    			srand ( rand() );
    			padres[i].tilt[j] = 15 - 30 * ( ( float ) rand() / ( float ) RAND_MAX );
    		}
    	}
    As you can see, altura, tilt and azimuth, have a range of valid values, which I check out every time I modified it (I add a smaller random value).

    The problem is that, sometimes, some strange values appears, sometimes 0, sometimes big positive or negative values, or values very close to zero.

    I tried to debug the binary, with no results, and using Valgrind, it throws this:

    Code:
    ==7468== Invalid read of size 1
    ==7468==    at 0x4023EF1: memcpy (mc_replace_strmem.c:406)
    ==7468==    by 0x8049B01: main (dios2.c:415)
    ==7468==  Address 0x41B6977 is 1 bytes before a block of size 50,176 alloc'd
    ==7468==    at 0x402195F: calloc (vg_replace_malloc.c:279)
    ==7468==    by 0x804892A: main (dios2.c:100)
    just before detecting a strange value (I wrote a function to check the integrity of the values in the arrays).

    I don't know if I'm doing something wrong, as, these strange values, doesn't appear on all values, only from time to time, so, if you can point me where could be the problem...

    Thanks in advance.
    Last edited by kwyjibo; 10-28-2008 at 11:44 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, can't comment on your actual problem, but:
    srand ( rand() );
    is probably not a good construct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) Do not seed more than once.
    2) Seed with the time, since it is guaranteed to be unique, thus giving you unique numbers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    Hi,

    Thanks for the responses. Random numbers is not a priority, as I plan to move to dSFMT, and I don't think it could be the probleme of the strange behaviour.

    Also, I tried to summarize the code, to make it readable, but there are more code, in fact, the Valgrind report, mentions memcpy, as I used this function to make copies of positions of the padres and hijos arrays.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    	for ( i = 0; i < 64; i++ )
    	{
    		// Localizaciones
    		srand ( rand() );
    		padres[i].localizacion = ( uint64_t ) ( ( pow ( 2, 32 ) * rand() ) / RAND_MAX ) << 32;
    		srand ( rand() );
    		padres[i].localizacion = padres[i].localizacion | ( uint64_t ) ( ( pow ( 2, 32 ) * rand() ) / RAND_MAX );
    
    		// Tipo de antena
    		srand ( rand() );
    		padres[i].tipo = ( uint64_t ) ( ( pow ( 2, 32 ) * rand() ) / RAND_MAX ) << 32;
    		srand ( rand() );
    		padres[i].tipo = padres[i].tipo | ( uint64_t ) ( ( pow ( 2, 32 ) * rand() ) / RAND_MAX );
    
    		//printf("Individuo &#37;d: %llx - %llx\n",i,padres[i].localizacion,padres[i].tipo);
    
    		for ( j = 0; j < 64;j++ )
    		{
    			// Altura
    			srand ( rand() );
    			padres[i].altura[j] = 1 + 14 * ( ( float ) rand() / (RAND_MAX  + 1.0));
    
    			// Azimuth
    			srand ( rand() );
    			padres[i].azimuth[j] = 5 * ( ( float ) rand() / (RAND_MAX  + 1.0));
    
    			// Tilt
    			srand ( rand() );
    			padres[i].tilt[j] = 15 - 30 * ( ( float ) rand() / (RAND_MAX  + 1.0));
    		}
    	}
    "If srand() makes things more random, then more srand()'s must be SUPER random! Plus with random as its seed, it will be super ultra random!!!!"

    Not exactly........ Surely it would help you spot the lack of randomality in the underlying algorithm if anything.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kwyjibo View Post
    Hi,

    Thanks for the responses. Random numbers is not a priority, as I plan to move to dSFMT, and I don't think it could be the probleme of the strange behaviour.

    Also, I tried to summarize the code, to make it readable, but there are more code, in fact, the Valgrind report, mentions memcpy, as I used this function to make copies of positions of the padres and hijos arrays.
    Yes, it's not random that valgrind mentions memcpy - it's highly likely that you made some sort of mistake in that section. I don't see anything (besides from the random number generation) that is questionable in the code you posted. It is fairly common here that the posted code is not where the problem is, so don't feel bad about "posting the wrong bit of code".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  2. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM