Skip to main content
GameDev.net gamedev.net
🔒 Locked

how do i change this example code to stop memory leaks ?

Started by thedodgeruk Nov 20, 2010 at 1:58 PM 6 replies 1.6k views
Original Post
thedodgeruk
thedodgeruk
#include
#include
#include
using namespace std;

#define _CRTDBG_MAP_ALLOC
#include

struct myStruct
{
int i;
string str;
};

int main()
{
deque mine;
_crtBreakAlloc=-1;

myStruct* s;
for (int i =0 ; i < 10 ; i ++)
{
s = new myStruct;
s->i = i;
s->str = "hello" +i;

mine.push_back(s);
//delete s;
}

for (int i = 0; i < mine.size() ; i++)
cout << mine->i << endl;

system( "pause" );
}

as you can see its very simple . if i use the "delete s" then it will stop the memory leaks but will also make everything in mine compleatly random .

can anyone help , or give me a link or book name to find how to fix this please
KulSeran
KulSeran
Well, to stop the leak, you have to call delete at some point. Obviously you don't want to do it while initalizing your deque, so add another loop at the end of main to delete everything in the deque.
thedodgeruk
thedodgeruk
"Well, to stop the leak, you have to call delete at some point. Obviously you don't want to do it while initalizing your deque, so add another loop at the end of main to delete everything in the deque."


this is the part i do not know how to do . any help please
rip-off
rip-off
The simplest thing is to not dynamically allocate the structures:
#include <iostream>#include <string>#include <deque>using namespace std;#define _CRTDBG_MAP_ALLOC#include <crtdbg.h>struct myStruct{   int i;   string str;   myStruct(int i, const std::string &s) : i(i), s(s) {}};int main(){   deque <myStruct> mine;    _crtBreakAlloc=-1;    for (int i =0 ; i < 10 ; i ++)    {       // This is shorthand       mine.push_back(myStruct(i, "hello " + i);       // The long way would be:       // myStruct s(i, "hello " + i);       // mine.push_back(s);    }    for (int i = 0; i < mine.size() ; i++)    {        cout << mine->i << endl;    }    system( "pause" );}
CzarKirk
CzarKirk
You could try adding smart pointers to the struct?
alvaro
alvaro
Did anyone notice that "hello"+i is actually pointer arithmetic, and undefined behavior if i>=7?

fastcall22
fastcall22
Quote:
Original post by alvaro
Did anyone notice that "hello"+i is actually pointer arithmetic, and undefined behavior if i>=7?


I did! To the OP: Unlike some popular scripting and programming languages out there that use the addition operator to signify string concatenation, in C++ you must use the stringstream class to convert types to strings:

#include <sstream>std::stringstream ss;ss << "hello " << i;s->str = ss.str();


Your current version performs pointer arithmetic, and I assume that's not the intended behavior:
const char* p;p = "Hello " + 0;  // p == "Hello ";p = "Hello " + 1;  // p == "ello ";p = "Hello " + 2;  // p == "llo ";p = "Hello " + 3;  // p == "lo ";p = "Hello " + 4;  // p == "o ";p = "Hello " + 5;  // p == " ";p = "Hello " + 6;  // p == "";  (*p == '\0')p = "Hello " + 7;  // p == ???
Zahlman
Zahlman
Quote:
Original post by thedodgeruk
"Well, to stop the leak, you have to call delete at some point. Obviously you don't want to do it while initalizing your deque, so add another loop at the end of main to delete everything in the deque."


this is the part i do not know how to do . any help please


Umm... what part can't you figure out?

1) Add another loop. You have already shown a loop in your code, so you know how to do this.

2) At the end of main. Surely that isn't the problem.

3) To delete. You've already shown that you know how to use the 'delete' keyword.

4) Everything in the deque. You do this by using the loop to access each element, and call 'delete' on each one. It's exactly the same idea as in your loop where you access each element, dereference the pointer, access the 'i' member, and feed it to std::cout.




Or you could just not use pointers in the first place. They accomplish absolutely nothing in your code at the moment except to make it harder to get everything right. You can make a std::deque, no '*', just fine.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.