Free memory of a map<int, string> [duplicate] - c++

This question already has answers here:
How do I force my std::map to deallocate memory used?
(7 answers)
Closed 8 years ago.
I have a program:
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
using namespace std;
int main() {
map<int, string> m;
for (int i = 0; i < 1000000; i++)
{
m[i] = "jahsdghsagdfv sahgvsahgd fvsahgdf fsdfjsadvhjgsd jhgfhsahfvsafh asfvasgfv jhgfdvsahgvfs";
}
m.clear();
while (1) {sleep(5);}
return 1;
}
clear() does nothing. In memory monitor I see memory usage 184 Mb and nothing change after clear. Why ? How to clear memory of map ?

Yes, map::clear does something: "Removes all elements from the map container (which are destroyed), leaving the container with a size of 0."
The data will not be removed also from stack/heap,but this shall not influence you since you'll have no pointer and no cast type to that obsolete data.
Probably when you will refill the map that memory area will be reused and updated with new values (if it was not used by other variables meanwhile).

Related

Error C++ : does not name a type [duplicate]

This question already has answers here:
"Y does not name a type" error in C++
(2 answers)
Closed 7 years ago.
I have a ploblem with the next code, I get the error: 'ptab' does not name a type and 'pfreeC' does not name a type, I don't understand how to solve that, thanks for your help =)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
int *ptab; //Here is the error
ptab=new int[64];
bool *pfreeC; //Here is the error
pfreeC=new bool[11];
The problem is that you have code outside a function body
using namespace std;
int *ptab;
bool *pfreeC;
int main()
{
ptab = new int[64];
pfreeC = new bool[11];
return 0;
}
Of course you should also delete the allocated memory. Or even better, use smart pointers.

"Process terminated with status -1073741819" simple program with vector

for some reason I get the "Process terminated with status -1073741819" error whenever I run my program, I've read that some people get this error because of something wrong with code-blocks/the compiler, i just wanted to know if there is anything wrong with my code before i go reinstalling compilers and such. I'm using code::blocks and the GNU GCC compiler.
my code creates a vector which stores 40 working hours in a week, and a vector inside that vector which stores letters representing the 5 people available in those hours.
Schedule.cpp:
#include <iostream>
#include "Schedule.h"
#include <vector>
#include <string>
using namespace std;
/// Creates a Vector which holds 40 items (each hour in the week)
/// each item has 5 values ( J A P M K or X, will intialize as J A P M K)
vector< vector<string> > week(40, vector<string> (5));
Schedule::Schedule(){
for (int i = 0; i<40; i++){
week[i][0] = 'J';
week[i][1] = 'A';
week[i][2] = 'P';
week[i][3] = 'M';
week[i][4] = 'K';
}
// test
cout << week[1][3] << endl;
}
header file:
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include <vector>
#include <string>
using namespace std;
class Schedule
{
public:
Schedule();
protected:
private:
vector< vector<string> > week;
};
#endif // SCHEDULE_H
main.cpp:
#include <iostream>
#include "Schedule.h"
#include <vector>
#include <string>
using namespace std;
int main()
{
Schedule theWeek;
}
This is not a copiler bug.
You are getting a memory fault in your constructor.
There are several things wrong with your code, for example in your cpp you declare a global vector week which then is hiden in the constructor since the constructor will access Schedule::week .
Your cpp should be something like :
// comment out the global declaration of a vector week ...
// you want a vector for each object instantiation, not a shared vector between all Schedule objects
// vector< vector<string> > week(40, vector<string> (5));
Schedule::Schedule()
{
for (int i=0;i<40;i++)
{
vector<string> stringValues;
stringValues.push_back("J");
stringValues.push_back("A");
stringValues.push_back("P");
stringValues.push_back("M");
stringValues.push_back("K");
week.push_back(stringValues);
}
}
You get the memory fault in your code when you try to access your week vector for the first time :
week[i][0] = 'J' ;
At the moment you call that line of code, your Schedule::week vector has 0 elements inside it (so week[i] is already a fault).

Segmentation Fault C++ pointers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am new to C++ programming but I know that pointers cause segmentation error. The problem is in the Readline() method when I am trying to read a sudoku but I cannot fix it. What am I missing?
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include "Sudoku.h"
using namespace std;
// Constructor
Sudoku::Sudoku(){
root=cells;
row=0;
row_ptr=&row;
}
void Sudoku::Readline(string s,int i) {
int lead;
for(int k=0;k<9;k++){
lead=(9*i)+k;
if (s[k]!=',') {
*(root+lead)=s[k];
} else {
*(root+lead)=0;
}
}
}
void Sudoku::MakeSudoku(string s){
//cout<<(*row_ptr)++<<' '<<s<<"\n";
Readline(s,(*row_ptr)++);
}
The class definition is
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Sudoku{
public:
int cells[81];
int row;
int *root;
int *row_ptr;
public:
Sudoku();
void MakeSudoku(string s);
void Readline(string s,int i);
void PrintSudoku();
};
The main file is
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "Sudoku.cpp"
using namespace std;
int main()
{
Sudoku sd;
// Input csv file containing sudoku
ifstream filen("sudoku.csv");
string s;
if(!filen.is_open()){
cout << "Error opening file";
} else{
while(!filen.eof()){
getline(filen,s);
sd.MakeSudoku(s);
}
}
filen.close();
//sd.PrintSudoku();
return 0;
}
Your code is no C++ code. Except file operations it is (bad styled) C code. You are using a plain array (cells), you even do an unnecessary copies of the array (root) and that pointer arithemtic is dangerous (as you are currently experiencing).
I think you should rewrite your code a bit which will solve your problem:
You should use descriptive variable names... k,s,i,etc. are hard to read
Use a two-dimensional array for 'cells'. Or even better a C++ container like a vector of vectors. The latter would check boundaries and you could get rid of your pointer arithmetics (which causes such faults when done the wrong way) and you could use plain indexes.
Use proper indentions and empty lines for block separation
Don't use magic numbers like "81" and "9". Create constants. Give them names! make them dependent from each other if they are dependent.

How to load Multidimensional array values into vector?

This is part of the code (header and the main part):
#include <iostream>
#include <sstream>
#include <string>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <glut.h>
#include <RassHost.h>
#include <api\iomap.h>
#include <api\iotrans.h>
#include <api\cgeometry.h>
#include <vector>
using namespace std;
int main()
{
cout << "Enter IP: " << endl;
getline(cin, server_ip);
enum(KEY_L = 'A', KEY_R = 'D', KEY_RUN = 'WW', KEY_JUMP='SPACE');
typedef OBJECT_3D_SYS_TYPES_NUM OBJECT3D_RCN_TYPE;
OBJECT3D_RCN_TYPE _psyObjects[][] = getPsyhicsPartObjects();
vector<OBJECT3D_RCN_TYPE> _objects;
//I would like to load _psyObjects[][] into vector<OBJECT3D_RCN_TYPE> _objects;
Server::StartGame(Server::getIP(), 8888, "-r run", false);
system("pause");
return 0;
}
Is it possible to copy _psyObjects values into vector<OBJECT3D_RCN_TYPE>?
I want to control the multidimensional array with vector api, if it is possible.
Thanks!
You'll need to create a vector of vectors:
vector< vector<OBJECT3D_RCN_TYPE> > _objects;
Then just fill it like a normal vector.
I'd post more code, but you need to know the dimensions of the array, and I can't see those from the code.
You could also use a Boost::multi_array. It's api is like std::vector's, but possibly similar enough to meet your needs.

How to declare vectors in C++?

I'm trying to use a vector of strings in my code instead of an array of strings but apparently I miss some detail in the declaration of the vector. Using the following code, I get this error: ‘vector’ was not declared in this scope
// Try to implement a vector of string elements
#include<iostream>
using namespace std;
int main() {
const int MAX_ITEMS = 10;
vector<string> my_vector(MAX_ITEMS);
return 0;
}
How should I correctly declare the vector?
You should add these includes:
#include <vector>
#include <string>
You have to include the header:
#include <vector>
#include <string>
You need:
#include <vector>