|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Can't wrap my head around POINTERS!!!!!!
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||
|
Programmer
|
i don't get it...
I mean, i understand that all a pointer is, is a 'pointer' to a memory location. Like an address to a house. Its just a locater to let other things know where you house is. I'm working on this assignment for school, and I'm just stuck. I bought a new book, been reading online, but still cant figure out what I'm reading to what i need to do... Maybe I'm just over thinking it. Maybe i just don't understand it to even begin to think about using the idea. Who knows... Here is some code i am working on.... Code:
// set up code to report memory leaks when running in Debug mode
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#include <iostream>
#include <iomanip>
#include "Food.h"
using namespace std;
static const char *PROMPT = "> "; // prompt string for command input
static const char PROMPT_END = ':'; // character for terminating food parameter label input
static Food **foods; // dynamically allocated array for storing foods
static int nFoods = 0; // how many foods we are storing in the array
int main(void)
{
foods = new Food *[100];
cout << "Nutricalc 1.0\nImplemented by: your name" << endl << endl;
cout << "\nedit food database" << endl
<< " commands: a(dd), c(lear), d(elete) index, l(ist), m(eal), q(uit)" << endl
<< "-------------------------------------------------------------------" << endl << endl;
for (;;) {
char cmdChar;
float tempVar;
Food *tempPointer;
cout << PROMPT;
cin >> cmdChar;
switch (cmdChar) {
case 'a': case 'A':
tempPointer = new Food;
cout << "caloires" << endl;
cin >> tempVar;
tempPointer->setCaloriesValue(tempVar);
cout << "carbohydrates" << endl;
cin >> tempVar;
tempPointer->setCarbsValue(tempVar);
cout << "fat" << endl;
cin >> tempVar;
tempPointer->setFatValue(tempVar);
cout << "cholesterol" << endl;
cin >> tempVar;
tempPointer->setCholesterolValue(tempVar);
cout << "sodium" << endl;
cin >> tempVar;
tempPointer->setSodiumValue(tempVar);
cout << "protein" << endl;
cin >> tempVar;
tempPointer->setProteinVar(tempVar);
foods[nFoods] = tempPointer;
++nFoods;
break;
case 'c': case 'C':
for (foods[nfoods] >= 0 ; foods[nfoods] <= 100 ; foods[nfoods]++)
{
delete nfoods[]; //This case is supposed to delete all the elements of the array that the food entry's are stored in. But cant get it to work...
nfoods++;
}
break;
case 'd': case 'D': {
// code for deleting a food. Dont have the slightest idea...
}
case 'l': case 'L':
for (nfoods[] =! null ; nfoods[] >= 100 ; nfoods[]++)
{
cout << foods << endl; //Code for outputting each food.
nfoods++;
}
break;
case 'q': case 'Q':
goto quit;
default:
cout << "invalid command!\n";
break;
}
cout << endl;
}
quit:
free(Food);
cout << endl; // Clears the array so there is no memory leaks, but not sure what to put in for this part...
// report any memory leaks
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
return 0;
}
Please Help!!!! Driving me nuts!!! Also, i need to make a function that outputs each array element to a file. Again, not to comfortable with pointer arrays enough to figure this out...
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
The Project Keeper
|
I don't understand what your problem is. Maybe you could explain it more?
__________________
|
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||
|
Programmer
|
There is a dynamic array:
Code:
static Food **foods foods = new Food *[100]; Code:
#pragma once
class Food
{
public:
void setCaloriesValue(float number);
void setCarbsValue(float number);
void setFatValue(float number);
void setCholesterolValue(float number);
void setSodiumValue(float number);
void setProteinValue(float number);
float returnCalories(void);
float returnCarbs(void);
float returnFat(void);
float returnCholesterol(void);
float returnSodium(void);
float returnProtein(void);
enum Category
{
unknown = -1, meat, poultry, seafood, dairy, vegetable,
fruit, grain, sweet, nCategories
};
~Food(void);
private:
char *name; // name of the food
Category category; // what kind of food
float calories; // calories
float carbohydrates; // grams
float fat; // grams
float cholesterol; // grams
float sodium; // grams
float protein; // grams
};
I need to setup a loop that goes through and 'deletes' each array entry. Then i need to make another loop that only deletes a specific entry, say: foods[3] or foods[34] Not to sure if i have the right idea here... But I'm pretty sure i have the right thinking in putting in a temp variable to keep track of where I'm at in the array.... Hope this makes more sense to you than it does for me... lol.... Not really sure if i'm on the right track...
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
The Project Keeper
|
Ok so you need a loop to clear an array. Simple enough.
In C# : (Meaning you have to convert this to C++ yourself.) Code:
int Position
for (Position = 0; Position < (array size); Position++)
{
array[Position] = 0;
}
__________________
|
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
If you declare your array this way
Code:
static Food **foods foods = new Food *[100];
|
|||||||||||||
|
|
|
|
#6 (permalink) | ||||||||||
|
Programmer
|
Quote:
And the comment made about the code to delete array elements wont work the way you suggest. If you set the first array ([0]) to 0 it screws up the entire array. The first entry, the '0', also known as null is needed for the array to work correctly.... Not entirely sure why, but some friends that are way ahead of me in the coding thing said that you ether need to use a 'delete' command in the array. I'm guessing that C++ is smart enough to understand that your not deleting the array, but just the data inside the array's reference... But like i said, i could be wrong... I'm still kinda a nub at this ![]()
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
||||||||||
|
|
|
|
#7 (permalink) | ||||||||||||
|
New to Overclock.net
|
Code:
Food foods[] = new Food[100]; If you use Code:
Food foods[100] I think the source of your confusion lies in the fact that arrays in C/C++ are just pointers to a sequence of bytes. When you use an array you can use Code:
int* myPtr = &myIntArray[0]
Last edited by ghell : 03-09-08 at 09:48 PM. |
||||||||||||
|
|
|
|
|
#8 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
I've never heard that. Why do you say the stack is faster than the heap? The only reason I could really think of is page faults, but that shouldn't affect a trivial program like this
|
|||||||||||||
|
|
|
|
#9 (permalink) | ||||||||||||
|
New to Overclock.net
|
Because it is faster. For a trivial program, it will not make a noticeable impact on performance anyway but for a large application or an application that involves a lot of number crunching on small data sets, the stack should be used. Using the heap unnecessarily will inevitably lead to memory leak anyway.
__________________The simplest example of the heap being slower is that every time you read or write to the heap, you have to go through a pointer. Imagine the trivial case of storing a 32 bit integer on the stack vs storing a pointer to it on the heap. In order to read the integer from the stack, you can just read it directly into a register. In order to read it form the heap you must get the pointer into a register, go and find it on the heap and then get the value. When writing back you must do the same thing. This is just a small example that should hopefully be simple enough to see instantly without having to think about it but there are further causes of lower performance on the heap. You should be able to find plenty of information on these further details if you google phrases such as "stack faster than heap" or "stack vs heap". The heap should mainly just be used where large amounts of data need to be used, such as when loading images. This is because the stack is a very small store in comparison. The stack is also simpler so why overcomplicate things?
|
||||||||||||
|
|
|
|
|
#10 (permalink) | ||||||||||||||
|
Apple Doesn't Love You
|
Quote:
|
||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|