SRL Archive - C/C++ Help and Tutorials

double pointers in a record


XcanadamanX

2010-10-01 23:05:27

i cant really explain so here is my assignment http://www.cas.mcmaster.ca/~qiao/courses/cs2sc3/assignments/a10f2.pdf

question 1 is the one i have no idea how to implement.

Nava2

2010-10-01 23:18:50

This is supposed to be in C# isnt it?

XcanadamanX

2010-10-02 14:27:46

no c++…

HyperSecret

2010-10-02 15:33:04

The way I understand it….

You have to design your own payrollT type and in your book at the end of chapter 2 there is already a employeeT type that you are suppose to use.

So like…


int main() {
payrollT *payrollPtr;

*payrollPtr = GetPayRoll();

delete payrollPtr; //make sure you delete the memory from the heap that you get from GetPayRoll()
return 0;
}

payrollT GetPayRoll() {
payrollT *temp = new paytollT();
//ask the user for the input of the
cin >> payrollT->employeeTArray[index]->employeeInfo;
//blah
//...
//...

return temp;
}

That is how I understand it…the gist of it. Somewhere in your payrollT type you will have an array (employeeT type) that needs to be access…

XcanadamanX

2010-10-05 10:01:22

so heres my code: i keep getting bad pointers and buffer overloads and i have no idea why…

#include "simpio.h"
#include <iostream>

struct employeeRecordT{
string name;
string title;
string ssn;
double salary;
int withholding;
};

struct payrollT{
int empNum;
employeeRecordT **employee;
};
payrollT Pay;

int main() {
cout << "How many employees: ";
Pay.empNum = GetInteger();
employeeRecordT *emp = new employeeRecordT[Pay.empNum];
Pay.employee = &emp;
for (int x = 0; x < Pay.empNum; x++){
cout << "Employee #" << x+1 << endl;
cout << " Name: ";
Pay.employee[x]->name = GetLine();
cout << " Title: ";
Pay.employee[x]->title = GetLine();
cout << " SSNum: ";
Pay.employee[x]->ssn = GetLine();
cout << " Salary: ";
Pay.employee[x]->salary = GetReal();
cout << " Withholding: ";
Pay.employee[x]->withholding = GetInteger();
}
cout << Pay.employee[0]->name;
cout << Pay.employee[1]->name;
return 0;

i am only trying with 2 employees thats why the bottom shows [0] and [1].

Wizzup?

2010-10-05 10:48:38

You need to create the memory for each structure in the array. Or rather, allocate memory for every member in emp. I don't think you're doing this atm.

note: I'm quite familiar with C, but not at all with C++; so I may be wrong as to what the "new" keyboard does exactly.

E: Also, compile with -ggdb and run it through GDB. It helps.

XcanadamanX

2010-10-05 16:18:42

new is "supposed" to allocate memory to the array…

Wizzup?

2010-10-05 16:22:28

From: XcanadamanX
new is "supposed" to allocate memory to the array…


Is simpio default on Linux installations? I don't have it on Ubuntu nor gentoo. (Trying to compile your code)

XcanadamanX

2010-10-05 16:32:42

no its not. i could send you my whole code. we are using some code from stanford.

edit: if you go to the middle of the page there is a link called blank project. just open that and insert my code in main http://www.cas.mcmaster.ca/~qiao/courses/cs2sc3/index.html

Wizzup?

2010-10-05 17:32:24

The array doesn't seem to be allocated properly:


(gdb) print Pay.employee[0]
$4 = (employeeRecordT *) 0x603018
(gdb) print Pay.employee[1]
[B]$5 = (employeeRecordT *) 0x1[/B]

PS: I replaced all the Get*** functions with a random variable. Eg, string("name"); or 42;
PSPS: I didn't use that code because I didn't have time to figure out how to use it. It assumes I have a mac or a pc, and it also requires me to use some IDE? I didn't find a Makefile anyway.

XcanadamanX

2010-10-05 17:53:41

so "new" isnt doing what we were told it does…

Wizzup?

2010-10-05 18:17:29

Unless I messed up testing; but that's what I got. See for yourself with gdb? I don't want to put you on the wrong track. :)

XcanadamanX

2010-10-05 18:46:38

no im pretty sure you are right. because pay.employee[0] always works but i can never do pay.employee[1] even if i allocate space for 3…i just dont know how to fix it.

Feroc1ty

2010-10-05 19:51:25

new creates an object on the stack and gives you the location of it, by the way the code creates a memory leak, its cleared up soon after cause the program closes but you should always delete something you create

"int *x = new int
delete x"

code above creates an integer x, but it's not what you think it is, it doesnt hold a value, it holds a pointer to a value which was created on the stack by the new keyword

"x = 10"

that wouldn't change the value of the object you created, that would simply change the pointer from pointing from the object to a new object which I have no idea what it is, it would also create a memory leak due to you now not being able to delete the object you created since nothing points to it anymore

"*x = 10"

this would make the new object (which is an integer) 10, and after you are done with that integer or whatever object you decide to create on that pointer you should delete it

"delete x"


dunno if that helped but i saw that you didnt understand what new/delete meant…

in short: new/delete create an object on the stack and only way to access them is to use pointers, its another way of creating a global variable with it being much easier to debug if there was an error with it

XcanadamanX

2010-10-05 21:07:47

according to my text. new allocates memory on the heap, not the stack

Feroc1ty

2010-10-05 23:16:32

From: XcanadamanX
according to my text. new allocates memory on the heap, not the stack


sorry got the two confused, you're right, stack is cleaned up as the function passes whilst the heap stays alive until the whole program is killed

just a quick question, what's the symbol ** mean?

XcanadamanX

2010-10-05 23:27:28

a pointer to a pointer. so double pointer

ive figured it out. i will post my code when i get home. it was a little more complicated than expected!

edit:
#include "simpio.h"
#include <iostream>

struct employeeRecordT{
string name;
string title;
string ssn;
double salary;
int withholding;
};

struct payrollT{
int empNum;
employeeRecordT **employee;
};

//Function Prototypes


payrollT GetPayroll();

//Main Loop

int main() {
payrollT Pay;
Pay = GetPayroll();
return 0;
}

/* Function GetPayroll
* -------------------
* Inputs employee's information and outputs a type PayrollT
*/

payrollT GetPayroll(){
payrollT *temppay; //declares a pointer of type payrollT
cout << "How many employees: ";
temppay = new payrollT; //initializes the pointer
temppay->empNum = GetInteger(); //puts the number of employees into empNum
temppay->employee = new employeeRecordT*[temppay->empNum]; //initializes an array of pointers

for (int i = 1; i <= temppay->empNum; i++){
temppay->employee[i] = new employeeRecordT; //initializes each index of the array
cout << "Employee #" << i << endl;
cout << " Name: ";
temppay->employee[i]->name = GetLine();
cout << " Title: ";
temppay->employee[i]->title = GetLine();
cout << " SSNum: ";
temppay->employee[i]->ssn = GetLine();
cout << " Salary: ";
temppay->employee[i]->salary = GetReal();
cout << " Withholding: ";
temppay->employee[i]->withholding = GetInteger();
}

return *temppay; //returns a payrollT type
}

simpio is the GetLine() and GetInteger() and GetReal(). you could use cin for that