Getting a munmap_chunk(): invalid pointer error, using pointers for my first time

I am using pointers for the first time and for the life of me I can't figure it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 #include <iostream>
#include <iomanip>
#include <fstream>
#include "Shareholder.h"
#include "Share.h"

using namespace std;

int numberOfLines();
Share *loadData(int size);
void shareholderData(Shareholder &sh);
void displayData(Share *myShare, int num);
void purchaseShares(Share *myShare, Shareholder &sh, int num);
void displayShareholder(Shareholder &sh);
void balanceAdjust(Shareholder &sh);

int main(){

  int amount = numberOfLines();
  Share *shares = loadData(amount);
  displayData(shares, amount);
  Shareholder shareholder1;
  shareholderData(shareholder1);
  purchaseShares(shares, shareholder1, amount);
  delete shares;
  shares = 0;
  displayShareholder(shareholder1);
  balanceAdjust(shareholder1);

  exit(0);
}

//The function I belive not to be working.

void purchaseShares(Share *myShare, Shareholder &sh, int num){

  bool valid = false;
  string comBought;
  int sharesBought;
  int match;
  double invested, balance;

  do{
	cout << endl;
	cout << setw(5) << "" << "Which company would you like to buy shares from? ";
	cin.ignore();
	getline(cin, comBought);
	cout << setw(5) << "" << "How many shares would you like to buy? ";
	cin >> sharesBought;
	cout << endl;

	for(int i = 0; i < num; i++){
  	string target = myShare[i].getCompanyName();
  	if(comBought == target){
    	match = i;
    	valid = true;
  	}
	}
	if (valid == false){
  	cout << setw(5) << "" << "Error! Invalid company! Please enter compnay from list.";
	}
  }while(valid == false);

  invested = myShare[match].getPricePerShare() * sharesBought;
  balance = sh.getAvailableBalance() - invested;

  sh.setAmmountInvested(invested);
  sh.setAvailableBalance(balance);
  sh.setNumberofShare(sharesBought);
  sh.setInvestmentCompany(myShare[match].getCompanyName());

}


If I need to include more of my code let me know please.
One thing I see wrong at a glance is that you are dereferencing myShare as an array, but you are calling delete shares; instead of delete[] shares;

An object created with new must be destroyed with delete.
An object created with new[] must be destroyed with delete[].

But that issue is not specific to purchaseShares. Can you use a debugger and tell us exactly which line is generating the error?

Showing more code might help (like showing loadData and getCompanyName), but what would help more is if you copied your code into another project, and then whittled down the code to create a minimal example that we can compile to reproduce the problem.

PS: Unrelated to your actual issue, but you should avoid using the exit() function. If you desire to return from main, either just put nothing down (it will automatically return 0), or write return 0; This allows any automatic object destruction to still run gracefully.
Last edited on
I will run the deubugger now and get back to you. I am a begginer so all of this is pretty new to me. I began learning to with CodeBlocks and now my university forces us to use Atom on a virtual machine which hasnt been the easiest transition.
The issue was the way I deleted the pointer. Thank you so much for the help:)
Topic archived. No new replies allowed.