Heap vs. Stack

I'm having issues

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

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

#16 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

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.
Was This Post Helpful? 0
  • +
  • -

#17 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 - 03:20 PM

Made some more adjustments and got less errors but I'm still getting errors with this code. Is it my function called printCat with the for loop or the if and else? Or pCat[]? Please take a look.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h> 

#define AVAILABLE 0
#define TAKEN 1
#define TRUE 1
#define FALSE 0

typedef struct{
	int weight;
	char name[10];
	int neuteredFlag;
} CAT;

void printCat(CAT);
void neuter(CAT);

main(){ 
	CAT pCats[10]; // allocating from the stack
	char *names[6] = {"Paul" , "Neko", "Gato", "Bert", "Ann" , "Deb" };
	int cat_names[6] = { { AVAILABLE } };
	int catIndex;
	int i = 0;

	srand( time( NULL ) );

	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; 
	
	printCat(pCats[10]);
	neuter(pCats[10]);
	printCat(pCats[10]);

	system("Pause");

}

void printCat(CAT pCats[]){
	int i;

	printf("Name  Weight Neutered Flag\n");
	printf("====  ====== =============\n\n");

	for(i = 0; i < pCats; i++){
		if(pCats[i].neuteredFlag == FALSE){
			printf("%s%5i      No\n\n", pCats[i].name, pCats[i].weight);
		}
		else{
			printf("%s%5i     Yes\n\n", pCats[i].name, pCats[i].weight);
		}
	}
}

void neuter(CAT pCats[]){
	int i;

	if(pCats[i].neuteredFlag == TRUE){
		printf("%s has already been neutered.\n\n", pCats[i].name);
	}
	else{
		printf("Note: %s has now been successfully neutered.\n\n", pCats[i].name);
		pCats[i].neuteredFlag = TRUE;
	}

}

This post has been edited by whatThewhat: 19 April 2010 - 04:17 PM

Was This Post Helpful? 0
  • +
  • -

#18 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 - 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:

1) pCats
2) pCats[5]
3) pCats[10
Was This Post Helpful? 0
  • +
  • -

#19 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 - 03:53 PM

I thought I was using the array in this function as a counter.

This post has been edited by whatThewhat: 19 April 2010 - 04:13 PM

Was This Post Helpful? 0
  • +
  • -

#20 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Heap vs. Stack

Posted 19 April 2010 - 03:59 PM

You need to take a step back and read up on arrays. I would go through this, reading up on anything you aren't familiar with.
Was This Post Helpful? 0
  • +
  • -

#21 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 - 04:57 PM

What exactly do I put inside the brackets of printCat? I tried many things but I still keep getting errors.
Was This Post Helpful? 0
  • +
  • -

#22 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

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.
Was This Post Helpful? 0
  • +
  • -

#23 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 - 05:18 PM

I get the differences but I'm just getting confused because of the structs. That's why I don't understand because I passed the function in the main as printCat(pCat);

But I still get errors saying: " cannot convert from 'CAT [10]' to 'CAT' " And that's why I added the printCat(pCat[10]);
Was This Post Helpful? 0
  • +
  • -

#24 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Heap vs. Stack

Posted 19 April 2010 - 05:19 PM

A single CAT and an array of cats are not equivalent:

CAT singleCat != CAT pCats[10]



If you want to pass the whole array you need to change your parameters to accept one.
Was This Post Helpful? 0
  • +
  • -

#25 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 - 07:06 PM

Why am I not passing the whole array? What parameters do I need to add?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h> 

#define AVAILABLE 0
#define TAKEN 1
#define TRUE 1
#define FALSE 0

typedef struct{
	int weight;
	char name[10];
	int neuteredFlag;
} CAT;

void printCat(CAT);
void neuter(CAT);

main(){ 
	CAT pCats[10];
	char *names[6] = {"Paul" , "Neko", "Gato", "Bert", "Ann" , "Deb" };
	int cat_names[6] = { { AVAILABLE } };
	int catIndex;
	int i = 0;

	srand( time( NULL ) );

	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; 
	
	printCat(pCats);
	neuter(pCats);
	printCat(pCats);

	system("Pause");

}

void printCat(CAT pCats[]){
	int i;

	printf("Name  Weight  Neutered Flag\n");
	printf("====  ======  =============\n\n");

	for(i = 0; i < pCats; i++){
		if(pCats[i].neuteredFlag == FALSE){
			printf("%s%5i      No\n\n", pCats[i].name, pCats[i].weight);
		}
		else{
			printf("%s%5i     Yes\n\n", pCats[i].name, pCats[i].weight);
		}
	}
}

void neuter(CAT pCats[]){
	int i;

	if(pCats[i].neuteredFlag == TRUE){
		printf("%s has already been neutered.\n\n", pCats[i].name);
	}
	else{
		printf("Note: %s has now been successfully neutered.\n\n", pCats[i].name);
		pCats[i].neuteredFlag = TRUE;
	}

}

This post has been edited by whatThewhat: 19 April 2010 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#26 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Heap vs. Stack

Posted 19 April 2010 - 07:11 PM

void printCat(CAT);
void neuter(CAT);



The parameter indicates that these functions accept one [and only one!] CAT.

As I said on the previous page, if you want to pass an array you need to change the parameter type:

//change the prototypes!
void printCat(CAT*); //or
void neuter(CAT[]);



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.
Was This Post Helpful? 0
  • +
  • -

#27 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 - 07:18 PM

Actually, there's an inconsistency:

void printCat(CAT);
//.... later on ....
void printCat(CAT pCats[]){



Inconsistent! Calm down and read your own code. Errors are frustrating, but if you wildly make changes, you won't get things to work.
Was This Post Helpful? 0
  • +
  • -

#28 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 - 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.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h> 

#define AVAILABLE 0
#define TAKEN 1
#define TRUE 1
#define FALSE 0

typedef struct{
	int weight;
	char name[10];
	int neuteredFlag;
} CAT;

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

main(){ 
	CAT pCats[10];
	char *names[6] = {"Paul" , "Neko", "Gato", "Bert", "Ann" , "Deb" };
	int cat_names[6] = { { AVAILABLE } };
	int catIndex;
	int i = 0;

	srand( time( NULL ) );

	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; 
	
	printCat(pCats);
	neuter(pCats);
	printCat(pCats);

	system("Pause");

}

void printCat(CAT pCats[]){
	int i = 0;

	printf("Name  Weight  Neutered Flag\n");
	printf("====  ======  =============\n\n");

	for(i = 0; i < 10; i++){
		if(pCats[i].neuteredFlag == FALSE){
			printf("%s%5i      No\n\n", pCats[i].name, pCats[i].weight);
		}
		else{
			printf("%s%5i     Yes\n\n", pCats[i].name, pCats[i].weight);
		}
	}
}

void neuter(CAT pCats[]){
	int i = 0;

	if(pCats[i].neuteredFlag == TRUE){
		printf("%s has already been neutered.\n\n", pCats[i].name);
	}
	else{
		printf("Note: %s has now been successfully neutered.\n\n", pCats[i].name);
		pCats[i].neuteredFlag = TRUE;
	}

}


Thanks for your replies. They have been helpful.

@KYA I set the index ' i ' to 10 but I get a crazy output! I don't know what's wrong. :dontgetit:

This post has been edited by whatThewhat: 19 April 2010 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

#29 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 - 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.
Was This Post Helpful? 0
  • +
  • -

#30 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 - 07:55 PM

This is what I mean by weird output:

Quote

Name Weight Neutered Flag
==== ====== =============

Gato 8 Yes

╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Ñáf«╕ ↕-858993460 Yes


Press any key to continue . . .


I'm not setting up the for loop correctly to print out 10 cats. You see the names and the weight are all in crypt and it prints out Yes for neutered every time. How do I fix it? Any suggestions?

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

Was This Post Helpful? 0
  • +
  • -

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