I'm having issues

Re: Heap vs. Stack

Posted 19 April 2010 - 02:38 PM

CAT pCat[10];

This creates a stack based array of 10 cat objects. Why you're using Hungarian notion, I do not know. While arrays do have an implicit pointer to their base, we usually don't treat it as such, at least in this case. Array and pointer relationships are out of the scope of this thread.

CAT* pCat;

Creates a stack based pointer to a CAT object. It currently does not point to anything.

CAT* pCat;
pCat = malloc(sizeof(CAT));

We just allocated memory for one CAT object on the heap. Now we can do cat stuff with the pointer. We do NOT malloc the array from above, memory has already been set aside.

Re: Heap vs. Stack

Posted 19 April 2010 - 03:30 PM

printCat(pCats[10]);

That's interesting code. In particular, what do you think pCats[10] means, syntactically? If you're not sure how to answer that, let me pose this question: Describe the differences between the three constructs below:

Re: Heap vs. Stack

Posted 19 April 2010 - 05:00 PM

Don't guess. Not knowing the difference between an array, a pointer, and a regular variable is not a foundation to build on. When passing arrays you just pass the name. The size is only used during creation. Subscript notation (the brackets) are used at creation and access of elements.

The reason you could do either is because C has a quirk [C++ has it too] where an array passed as a parameter is treated like a regular pointer. This means that any special data about the area (sizeof() for example) is now thrown out the window.

You will then need to change how you handle the param in the function. You can't just use the name, you need to specify an index and then the data at that index you wish to access. You are accessing the index at 'i', but you never set its value.

Re: Heap vs. Stack

Posted 19 April 2010 - 07:33 PM

Yes these errors are very frustrating even more because I procrastinated and I have a deadline at 11:30 tonight. Probably not going to make it but I really want to get this code right because I want to learn. So mind is a little frazzled.

Anyway, I changed functions to:

void printCat(CAT[]);
void neuter(CAT[]);

But now I get this crazy output. And I believe its my for loop in the function in printCat. I need help with printing 10 cats (name, weight, neutered or not) out randomly. It's weird because it say build succeeded but than when I run it breaks.

Re: Heap vs. Stack

Posted 19 April 2010 - 07:44 PM

Your last post doesn't ask a question. Furthermore, it doesn't offer any details, describing problems as "weird things happened" and "broken". What is this, a UFO story? Or (thanks, Baavgai, for reminding me of xkcd), see computer problems joke. That's roughly how I see the information you present. "Broken" and "weird output" are not evidence. Copy paste observations is evidence.