Heap vs. Stack

I'm having issues

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

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

#1 whatThewhat  Icon User is offline

  • New D.I.C Head

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

Heap vs. Stack

Posted 19 April 2010 - 12:20 PM

I need help with implementing my array of cats from the stack. But I'm don't exactly know how. I also need to run this 10 times. So I need a for loop in printCat function . . . i think?

#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;

	srand( time( NULL ) );

	pCats = malloc(sizeof(CAT));	

	catIndex = rand() % 6;

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

	// Would this be considered a heap?
// Do I need to change -> to . for it to be a stack?
	strcpy(pCats->name, names[catIndex]); 
	pCats->weight = rand() % 30 + 1; 
	pCats->neuteredFlag = rand() % 2; 

	printCat(pCats);
	neuter(pCats);
	printCat(pCats);

	system("Pause");

}

void printCat(CAT *pCats){
	printf("Name   Weight  Neutered Flag\n");
	printf("====   ======  =============\n\n");

	//?? Need help with for loop ??
	if(pCats->neuteredFlag == FALSE){
		printf("%s%6i         No\n\n", pCats->name, pCats->weight);
	}
	else{
		printf("%s%6i       Yes\n\n", pCats->name, pCats->weight);
	}
}

void neuter(CAT *pCats){
	if(pCats->neuteredFlag == TRUE){
		printf("%s has already been neutered.\n\n", pCats->name);
	}
	else{
		printf("Note: %s has now been successfully neutered.\n\n", pCats->name);
		pCats->neuteredFlag = TRUE;
	}

}


Thanks for any helpful replies.

EDIT: Indented code correctly. I added comments throughout the code that ask questions.

This post has been edited by whatThewhat: 19 April 2010 - 12:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Heap vs. Stack

#2 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 - 12:25 PM

Ask a question.

Also, your code does not have proper formatting. Look up indent style on Wikipedia. If you want to fix your existing code, look up UniversalIndentGUI.
Was This Post Helpful? 0
  • +
  • -

#3 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 - 12:34 PM

 #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;

	srand( time( NULL ) );

	pCats = malloc(sizeof(CAT));	

	catIndex = rand() % 6;

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

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

	printCat(pCats);
	neuter(pCats);
	printCat(pCats);

	system("Pause");

}

void printCat(CAT pCats){
	printf("Name   Weight  Neutered Flag\n");
	printf("====   ======  =============\n\n");

	// for loop here?
	if(pCats.neuteredFlag == FALSE){
		printf("%s%6i         No\n\n", pCats.name, pCats.weight);
	}
	else{
		printf("%s%6i       Yes\n\n", pCats.name, pCats.weight);
	}
}

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

}



When I change ' -> ' to ' . ' I get error messages. Why? What am I doing wrong?

This post has been edited by whatThewhat: 19 April 2010 - 12:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 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 - 12:46 PM

-> is not the same . Let's take the construction a.b. Even though we aren't involving any actual types here, syntactically there are some clear requirements. a has to be a struct, class, or union. a->b is the same as (*a).b That means a is pointer to a struct, class, or union.

It should be clear that you need to figure out what the type of a is, and then use the appropriate syntax to access a member.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

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

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Heap vs. Stack

Posted 19 April 2010 - 12:47 PM

If it's possible to give a quick rundown:

stack: memory declared at compile time, an array like thisCAT pCats[10]; allocates memory to hold 10 CATs on the stack. There is no need to allocate memory on the heap for it.

heap: memory allocated using 'new' or 'malloc'. This is used when you don't know how much memory you'll need at compile time. There are other reasons as well, but for now this definition will suffice.


When you allocate, you need to give enough space for 'x' amount of objects you need. For example:

//this is horribly wrong
CAT pCats[10]; //10 cats on the stack
pCats = malloc(sizeof(CAT)); //incrorrect, but allocates 1 cat on the heap

//if you wanted to do that:
CAT* catHandler; //pointer to a CAT
catHandler = malloc(sizeof(CAT)); //correct allocation for 1 cat


//if you want multiple CATs
CAT* cats[5]; //an array of 5 CAT pointers
cats[0] = malloc(sizeof(CAT));//etc...0-4


//or 
CAT** cats;
cats = (CAT**)malloc(5*sizeof(CAT*)); //allocate a pointer to a pointer
cats[index] = malloc(sizeof(CAT)); //same as above, etc... 


Was This Post Helpful? 0
  • +
  • -

#6 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 - 12:50 PM

@KYA Thanks for the breakdown. So I shouldn't allocate any memory?

This post has been edited by whatThewhat: 19 April 2010 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

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

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Heap vs. Stack

Posted 19 April 2010 - 12:55 PM

If you know how many CATs you want at compile time, use the stack, otherwise use the heap.


Also, your function prototypes don't match the implementation. You don't need to0 pass a pointer to a CAT object, since you aren't modifying any of its data and I'm pretty sure you'd be ok with the performance hit/shallow copying.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Heap vs. Stack

Posted 19 April 2010 - 12:55 PM

Where did you pull calloc out of what KYA just said? You can certainly use it (I do all the time...I like my variables initialized into a known state always), but it suggests you are perhaps getting conflicting information elsewhere, which could be very confusing.

EDIT: You edited your post to remove calloc...
Was This Post Helpful? 0
  • +
  • -

#9 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 - 01:02 PM

Ok so I made some adjustments to the code still not right though. I added a for loop because I want it to print out 10 cats randomly. And the compiler still gives me errors to change ' . ' to '->'

Am I implementing something wrong? Lost . . .

#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*, int i);
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;

	srand( time( NULL ) );

	//pCats = malloc(sizeof(CAT));	

	catIndex = rand() % 6;

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

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

	printCat(pCats, i);
	neuter(pCats);
	printCat(pCats, i);

	system("Pause");

}

void printCat(CAT pCats, int i){
	printf("Name   Weight  Neutered Flag\n");
	printf("====   ======  =============\n\n");

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

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

}

This post has been edited by whatThewhat: 19 April 2010 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#10 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 - 01:41 PM

pCats.weight[i] = rand() % 30 + 1; 
So, pCats is a struct or class? Show me the line of code that declares pCats as a struct or class.

This post has been edited by Oler1s: 19 April 2010 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

#11 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 - 01:57 PM

I thought the first line in main ( ) that says
CAT pCats
declares it as a struct?
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

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

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Heap vs. Stack

Posted 19 April 2010 - 02:00 PM

Reread my post. You didn't fix your stack/heap intermixing issues.
Was This Post Helpful? 0
  • +
  • -

#13 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 - 02:09 PM

@KYA oh I see

I put called the function:
void printCat(CAT*, int i);
void neuter(CAT*);


Instead it should be:

void printCat(CAT, int i);
void neuter(CAT);


Right or Wrong? Am I still missing something?

But I still get many errors to change the operand.

This post has been edited by whatThewhat: 19 April 2010 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

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

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Heap vs. Stack

Posted 19 April 2010 - 02:12 PM

That's one issue, but specifically I was referring to the array of 10 cats declared on the stack which you then point towards one dynamically allocated cat. One or the other, not both.
Was This Post Helpful? 0
  • +
  • -

#15 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 - 02:30 PM

@KYA I'm not quit following. How should I declare the array of 10 cats then?

Do I eliminate pCats and just do CAT[10].

Sorry it's taking me while to figure this out : (

This post has been edited by whatThewhat: 19 April 2010 - 02:31 PM

Was This Post Helpful? 0
  • +
  • -

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