7 Replies - 238 Views - Last Post: 25 November 2012 - 01:36 PM Rate Topic: -----

#1 sumsar1812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 12

output parameter giving wrong results ?

Posted 25 November 2012 - 11:31 AM

Hi guys im doing my exam project in C and I need a bit of help.

So i have this function:

int moreThanGoals(int goals,int lines,kamp matches[],int *moreGoals) /* "index" is the amount of matches there have over "int goals" goals in it. */
{
  int i,k = 0;
  int index;


  for(i = 0; i <= lines; i++)
  {
    if ((matches[i].homeResult + matches[i].outResult) > goals)
	{
	  moreGoals[k] = i;
	  printf("k:%d - goals :%d\n",k,moreGoals[k]);
	  k++;
	}
  }
  free(moreGoals);
  index = k;
  return index;
}


whitch works perfectly but when I call the function from main it goes wrong:

  int *goalIndex,goalIndexI,i;
  goalIndex = (int *)malloc(sizeof(int));
  if (goalIndex == 0)
  {
    printf("Error: not enough memory");
  }
  goalIndexI = moreThanGoals(5,lines,ma,goalIndex);
  printf("Task 1 is complete with the following results:\n");
  printf("Round     date     HomeTeam     OuterTeam     Score     People\n");
  printf("%d\n",goalIndex[0]);



So i can see in the function with printf that it works and posts the right results, but when i try to see goalIndex[0]it shows "1629738420" whitch is very wrong... I really can't see the reason for it doing this ? if any it would maybe be the output parameter, not sure though?

Also another side question, i use malloc up in main, and then i use free() down in the function moreThanGoals, is it wrong to do it like this ? or should i first do it after the function have ended? (in main)

Is This A Good Question/Topic? 0
  • +

Replies To: output parameter giving wrong results ?

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: output parameter giving wrong results ?

Posted 25 November 2012 - 11:57 AM

Quote

Also another side question, i use malloc up in main, and then i use free() down in the function moreThanGoals, is it wrong to do it like this ? or should i first do it after the function have ended? (in main)

generally the same entity which allocates something should be the one that frees it; it would be better to free it in main after the function is called.

Also you are only allocating 1 integer but you are using multiple. You need to allocate enough to store all the values.
Was This Post Helpful? 0
  • +
  • -

#3 sumsar1812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 12

Re: output parameter giving wrong results ?

Posted 25 November 2012 - 12:15 PM

[quote name='ishkabible' date='25 November 2012 - 11:57 AM' timestamp='1353869872' post='1753885']

Quote

Also you are only allocating 1 integer but you are using multiple. You need to allocate enough to store all the values.


Hmm I'm not sure what you mean here. I do allocate memory for goalIndex , and it can hold all the information( i checked in end of my function moreThanGoals) but the problem is when it returns the values through output parameters, and gives me a whole new result ?
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: output parameter giving wrong results ?

Posted 25 November 2012 - 12:54 PM

goalIndex = (int *)malloc(sizeof(int));

this only allocates space for 1 integer, nothing more; if that's all you expect in the input then that is ok but it looks like 'k' is incremented and that more than one array location is used.

I can't believe I didn't see this before but you're freeing the array before you read the value too; that's the biggest issue here
free(moreGoals);

is called before the function returns but then you read goalIndex after the function returns. the result is being deleted before you can read it.

This post has been edited by ishkabible: 25 November 2012 - 12:55 PM

Was This Post Helpful? 1
  • +
  • -

#5 sumsar1812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 12

Re: output parameter giving wrong results ?

Posted 25 November 2012 - 12:58 PM

So ok i might have a whole wrong idea of free ? i can't use the variable after i free ?
Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: output parameter giving wrong results ?

Posted 25 November 2012 - 01:29 PM

ya, after you free it you can't use it, or rather it's undefined to dereference it.

This post has been edited by ishkabible: 25 November 2012 - 01:31 PM

Was This Post Helpful? 1
  • +
  • -

#7 sumsar1812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 12

Re: output parameter giving wrong results ?

Posted 25 November 2012 - 01:31 PM

Okay thanks, btw the variable k, is not being bigger(not an array) is just the size of the array really.
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: output parameter giving wrong results ?

Posted 25 November 2012 - 01:36 PM

Free doesn't wipe the memory clean when it's called; however, the memory is marked as "fair game" for any other allocation needs. Think of malloc as reserving a chunk of memory so only you can use it, once you done with that memory you should free it so that it can be used for something else.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1