Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My problem is that when I am passing a pointer array "q" collected by function "sense" to other function "move" , the values in "q" gets randomly changes. Any response will be heartily appreciated. Here is my code:

Main()
int main(int argc,char *argv[])
{
    int i,j,k;
    float *p=(float *)malloc(sizeof(float));
    float *q=(float *)malloc(sizeof(float));
    j=0;
    k=0;

    for(i=0;i<NCELLS;i++)
                 p[i]=1.0/NCELLS;

        q=sense(p,j); //Gives write values in q
    j++;

    q=move(q,motion[k]); // when passed to "move" values of q randomly changes
    k++;

    printf("\n");
    for(i=0;i<NCELLS;i++)
                printf("%f\t", q[i]);

    printf("\n");
    return 0;
}
functions:
float* move(float *p,int U)
{
    int i;
    float temp;
    float *next=(float *)malloc(sizeof(float));
    printf("\n");
    for(i=0;i<NCELLS;i++)
            printf("%f\t", p[i]); //Here I am checking for change in values
    for(i=0;i<NCELLS;i++)
    {
        temp=pGoal*p[mod(i-U,NCELLS)];  
        temp=temp+pOvershoot*p[mod(i-U+1,NCELLS)]; 
        temp=temp+pUndershoot*p[mod(i-U-1,NCELLS)]; 
        next[i]=temp;
    }  

    return(next);
}

float* sense(float *p,int j)
{
    int i;
        float *q, sum;
    q=(float *)malloc(sizeof(float));
        sum=0;
        for(i=0;i<NCELLS;i++)
        {
                if(strcmp(World[i],Sense[j])==0)
                        q[i]=pHit*p[i];
                else
                        q[i]=pMiss*p[i];

                        sum+=q[i];     
         }
     for(i=0;i<NCELLS;i++)
                q[i]=q[i]/sum;

    return(q);
}
share|improve this question
1  
please work on your indentation. Your code is pretty unreadable at this point. –  Femaref Jun 2 at 11:24
 
I don't have the time to take a serious look at it, but printing as a debugging tool will only take you so far. Use a debugger to step thorough the code and examine the values deeply. –  StoryTeller Jun 2 at 11:25
 
You have a memory leak here: q=move(q,motion[k]); and here: q=sense(p,j);. Memory allocated here: float *q=(float *)malloc(sizeof(float)); will be lost. –  soon Jun 2 at 11:25
2  
malloc(sizeof(float)) be allocated only one element. –  BLUEPIXY Jun 2 at 11:26
 
What's NCELLS? 1? –  Charles Bailey Jun 2 at 11:29
show 4 more comments

1 Answer

float *q=(float *)malloc(sizeof(float));

How many floats are you allocating? One.

for(i=0;i<NCELLS;i++)
            printf("%f\t", q[i]);

How many floats are you attempting to access? Five. Do you see a problem here? If you want five floats, allocate five floats:

float *q = malloc(5 * sizeof *q);

In your move function, you allocate and return a new array only to discard the old without destroying the old (using free) after returning. This causes a memory leak. Why do you even need malloc? Don't call malloc in these functions; Leave that decision for the caller (main) to make. In this case, the caller doesn't need dynamic allocation, so it benefits significantly from the choice. Use the right tool for the job...

I suggest adopting a style consistent to memcpy for these functions. Any external variables should be provided as arguments or inside structs that are provided as arguments.

void find_move(float *, float const *, size_t, size_t, float, float, float);
void find_sense(float *, float const *, size_t, char const * const *, char const *, float, float);

int main(void)
{
    float p[NCELLS], q[NCELLS];

    for (size_t i = 0; i < NCELLS; i++)
    {
        p[i] = 1.0 / NCELLS;
    }

    find_sense(q, p, NCELLS, world, sense[1], hit, miss);
    find_move(p, q, NCELLS, 0, goal, overshoot, undershoot);

    for (size_t i = 0; i < NCELLS; i++)
    {
        printf("%f\t", p[i]);
    }

    putchar('\n');
}

void find_move(float *destination, float const *source, size_t length, size_t offset, float goal, float overshoot, float undershoot)
{
    for (size_t i = 0; i < length; i++)
    {
        float temp = goal * source[mod(i - offset, length)]
                   + overshoot * source[mod(i - offset + 1, length)]
                   + undershoot * source[mod(i - offset - 1, length)];
        printf("%f\t", source[i]);
        destination[i] = temp;
    }
    putchar('\n');
}

void find_sense(float *destination, float const *source, size_t length, char const * const *world, char const *sense, float hit, float miss)
{
    float sum = 0;

    for (size_t i = 0; i < NCELLS; i++)
    {
        if (strcmp(world[i], sense) == 0)
        {
            destination[i] = hit * source[i];
        }
        else
        {
            destination[i] = miss * source[i];
        }

        sum += destination[i];     
    }

    for (size_t i = 0; i < length; i++)
    {
        destination[i] /= sum;
    }
}

P.S. Note the consistent whitespace and descriptive identifiers used here. I even put braces where people expect them, however unnecessary that may seem. I expect that if my code were hideous, you wouldn't want to decipher it. You'd probably ignore the answer and wait for a new one, much like most people will ignore your question and wait for a new one... Please keep this in mind when you present code to us.

share|improve this answer
 
Thanks a lot dear n yeah surely I will henceforth follow your advice. Again thanks a lot:) –  user2444953 Jun 3 at 0:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.