Heap vs. Stack

I'm having issues

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 1456 Views - Last Post: 19 April 2010 - 08:36 PM Rate Topic: -----

#31 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Heap vs. Stack

Posted 19 April 2010 - 08:05 PM

Compiling is not the same as correct. Runs now doesn't mean will run later. Your code might crash on my computer. I see some bad code that is syntactically valid. Taken from main:

catIndex = rand() % 6;

while(cat_names == TAKEN){
        catIndex = rand() % 6;
}

strcpy(pCats[i].name, names[catIndex]); 
pCats[i].weight = rand() % 30 + 1; 
pCats[i].neuteredFlag = rand() % 2; ;



Just for fun, why don't you tell me what this block of code does. Translate the above to English, line by line.
Was This Post Helpful? 0
  • +
  • -

#32 whatThewhat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 01-April 10

Re: Heap vs. Stack

Posted 19 April 2010 - 08:11 PM

...

This post has been edited by whatThewhat: 19 April 2010 - 08:56 PM

Was This Post Helpful? 0
  • +
  • -

#33 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Heap vs. Stack

Posted 19 April 2010 - 08:22 PM

No. None of what you said is true. Line by line.

catIndex = rand() % 6; catIndex is randomly set to 0..5 inclusive.

while(cat_names == TAKEN){ cat_names is an array. It decays, and you get an integer pointer. Basically, what that line of code is saying: while cat_names has the memory address of TAKEN..... TAKEN is 1, so basically either there is an infinite loop or not, depending on the address of cat_names. In reality, there won't be an infinite loop because no variable will have an address close to 0.

catIndex = rand() % 6; And in this possibly infinite loop, you repeatedly set this single catIndex to a random variate that is uniform over 0..5.

strcpy(pCats[i].name, names[catIndex]); You then index names array with this catIndex. You also index pCats with i. catIndex is determined at runtime. i is set to 0. Copy string appropriately.

pCats[i].weight = rand() % 30 + 1; As before, i is set to 0, so only pCats[0].weight is set to some random value from 1..30.

pCats[i].neuteredFlag = rand() % 2; ; There's two statements on this line. The second is the empty ; statement. The first statement sets pCats[0].neuteredFlag to 0..1 determined at runtime.

What you think your code does diverges from what you have.

The output is junk because your preceding code is junk.

This post has been edited by Oler1s: 19 April 2010 - 08:23 PM

Was This Post Helpful? 0
  • +
  • -

#34 whatThewhat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 01-April 10

Re: Heap vs. Stack

Posted 19 April 2010 - 08:28 PM

Well yea I know it's not the program it's the programmer.
Was This Post Helpful? 0
  • +
  • -

#35 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Heap vs. Stack

Posted 19 April 2010 - 08:36 PM

How can you fix your code if you don't even understand what it does? For example, if you talk about each cat being modified, I should see some looping construct. There isn't one. That's a dead giveaway.

There's a fundamental disconnect between what you want, and what your code is saying. You are describing what you want each line of code to mean, not how it actually evaluates. If I write a[i] = b, it does not mean for every item in a, assign b to it. All it means is that i+1 element of a is set to b. It's that level of technical evaluation that you need.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3