Linked list with memory leak issues Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Memory leak in a scraperWorking C code dealing with lists: problem with memory leakDoubly linked list with no apparent memory leaksPossible memory leak with WPF Custom Control?Game LevelManager Memory Leak?Possible memory issue in linked list programMemory leak using UserControlsLinked List with c++ classLinked list using dynamic memoryMemory allocation for linked list in C
"Destructive power" carried by a B-52?
How to make triangles with rounded sides and corners? (squircle with 3 sides)
malloc in main() or malloc in another function: allocating memory for a struct and its members
Did pre-Columbian Americans know the spherical shape of the Earth?
Is there a verb for listening stealthily?
Why can't fire hurt Daenerys but it did to Jon Snow in season 1?
Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?
How does the body cool itself in a stillsuit?
Determine whether an integer is a palindrome
Adapting the Chinese Remainder Theorem (CRT) for integers to polynomials
Why are current probes so expensive?
What is the proper term for etching or digging of wall to hide conduit of cables
First paper to introduce the "principal-agent problem"
Is this Kuo-toa homebrew race balanced?
Did any compiler fully use 80-bit floating point?
How to make an animal which can only breed for a certain number of generations?
As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?
New Order #6: Easter Egg
Sally's older brother
Should man-made satellites feature an intelligent inverted "cow catcher"?
What are some likely causes to domain member PC losing contact to domain controller?
Is it OK to use the testing sample to compare algorithms?
Can two people see the same photon?
What was the last profitable war?
Linked list with memory leak issues
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Memory leak in a scraperWorking C code dealing with lists: problem with memory leakDoubly linked list with no apparent memory leaksPossible memory leak with WPF Custom Control?Game LevelManager Memory Leak?Possible memory issue in linked list programMemory leak using UserControlsLinked List with c++ classLinked list using dynamic memoryMemory allocation for linked list in C
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
The nodes seems to connect just fine until implementing my getName() function for variable assignment:
> *** Error in `./zBunny.exe': free(): invalid pointer: 0x00007f0e528dab78 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f0e5258d7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f0e5259637a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f0e5259a53c]
./zBunny.exe[0x4016e8]
./zBunny.exe[0x401ef2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f0e52536830]
./zBunny.exe[0x401569]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00604000-00605000 r--p 00004000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00605000-00606000 rw-p 00005000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
01f52000-01f84000 rw-p 00000000 00:00 0 [heap]
7f0e4c000000-7f0e4c021000 rw-p 00000000 00:00 0
7f0e4c021000-7f0e50000000 ---p 00000000 00:00 0
7f0e5220d000-7f0e52315000 r-xp 00000000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52315000-7f0e52514000 ---p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52514000-7f0e52515000 r--p 00107000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52515000-7f0e52516000 rw-p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52516000-7f0e526d6000 r-xp 00000000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e526d6000-7f0e528d6000 ---p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528d6000-7f0e528da000 r--p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528da000-7f0e528dc000 rw-p 001c4000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528dc000-7f0e528e0000 rw-p 00000000 00:00 0
7f0e528e0000-7f0e528f6000 r-xp 00000000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e528f6000-7f0e52af5000 ---p 00016000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af5000-7f0e52af6000 rw-p 00015000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af6000-7f0e52c68000 r-xp 00000000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52c68000-7f0e52e68000 ---p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e68000-7f0e52e72000 r--p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e72000-7f0e52e74000 rw-p 0017c000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e74000-7f0e52e78000 rw-p 00000000 00:00 0
7f0e52e78000-7f0e52e9e000 r-xp 00000000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e53073000-7f0e53079000 rw-p 00000000 00:00 0
7f0e5309c000-7f0e5309d000 rw-p 00000000 00:00 0
7f0e5309d000-7f0e5309e000 r--p 00025000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309e000-7f0e5309f000 rw-p 00026000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309f000-7f0e530a0000 rw-p 00000000 00:00 0
7ffe1d800000-7ffe1d821000 rw-p 00000000 00:00 0 [stack]
7ffe1d9b0000-7ffe1d9b2000 r--p 00000000 00:00 0 [vvar]
7ffe1d9b2000-7ffe1d9b4000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
This program is acting as a template for what will be a linked list of bunny objects with unique names and features.
int Nodes::insert()
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->name = getName();
new_node->prev = NULL;
new_node->next = head;
if (head != NULL)
head->prev = new_node;
head = new_node;
return 0;
I've designed it so that ideally when a new node is made the each variable has relevant data assigned from a function call. For example:
new_node->name = getName();
In the instance of my memory issue the first node created runs fine but the second one produces the error at this^ function call assignment.
The full code:
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using std::vector;
using std::string;
using std::cout;
using std::endl;
class Node
public:
int data;
string name;
Node* next;
Node* prev;
;
class Nodes
private:
Node* head = NULL;
public:
int insert();
void display();
int randomGen(int n);
string getName();
void extract(std::vector<string> *vect, string fileName);
;
void Nodes::display()
Node* next = head;
while(next != NULL)
cout << next->name << 'n';
next = next->next;
int Nodes::insert()
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->name = getName();
new_node->prev = NULL;
new_node->next = head;
if (head != NULL)
head->prev = new_node;
head = new_node;
return 0;
int Nodes::randomGen(int n)
srand((int)time(0));
return (rand() % n) + 1;
// --
void Nodes::extract(vector<string> *vect, string fileName)//pulls lines from a file and splits the names up from each line using tokenization indexing them for use
std::ifstream accessFile;
accessFile.open (fileName);
string pulledLine;
if (accessFile.is_open())
string extractedName;
getline(accessFile, pulledLine);
for(auto p: pulledLine)
if(p != ' ')
if(extractedName != " ")
extractedName += p;
else
extractedName = p;
;
else
vect->push_back(extractedName);
extractedName = " ";
string Nodes::getName()//utilizes extract() to pull names and combines them using randomGen() to pull a name from a random index.
std::vector<string> fNames = " ";
std::vector<string> lNames = " ";
const string fileName[2] = "fnames.txt", "lnames.txt";
extract(&fNames, fileName[0]);
extract(&lNames, fileName[1]);
/* original formula for pulling random names
string fullName = fNames[randomGen(sizeof(fNames))] + " " + lNames[randomGen(sizeof(lNames))];
*/
//put here so I could focus on memory leak.
string fullName = "cjaz";
return fullName;
// --
int main()
Nodes control;
control.insert();
control.insert();
// control.insert();
// control.insert();
control.display();
I'm looking to figure out the source of this memory issue and bring to light any bad practices or design flaws that I might be using.
Any and all feedBack is appreciated, Thank you!
object-oriented c++11 linked-list memory-management
$endgroup$
add a comment |
$begingroup$
The nodes seems to connect just fine until implementing my getName() function for variable assignment:
> *** Error in `./zBunny.exe': free(): invalid pointer: 0x00007f0e528dab78 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f0e5258d7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f0e5259637a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f0e5259a53c]
./zBunny.exe[0x4016e8]
./zBunny.exe[0x401ef2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f0e52536830]
./zBunny.exe[0x401569]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00604000-00605000 r--p 00004000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00605000-00606000 rw-p 00005000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
01f52000-01f84000 rw-p 00000000 00:00 0 [heap]
7f0e4c000000-7f0e4c021000 rw-p 00000000 00:00 0
7f0e4c021000-7f0e50000000 ---p 00000000 00:00 0
7f0e5220d000-7f0e52315000 r-xp 00000000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52315000-7f0e52514000 ---p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52514000-7f0e52515000 r--p 00107000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52515000-7f0e52516000 rw-p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52516000-7f0e526d6000 r-xp 00000000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e526d6000-7f0e528d6000 ---p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528d6000-7f0e528da000 r--p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528da000-7f0e528dc000 rw-p 001c4000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528dc000-7f0e528e0000 rw-p 00000000 00:00 0
7f0e528e0000-7f0e528f6000 r-xp 00000000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e528f6000-7f0e52af5000 ---p 00016000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af5000-7f0e52af6000 rw-p 00015000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af6000-7f0e52c68000 r-xp 00000000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52c68000-7f0e52e68000 ---p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e68000-7f0e52e72000 r--p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e72000-7f0e52e74000 rw-p 0017c000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e74000-7f0e52e78000 rw-p 00000000 00:00 0
7f0e52e78000-7f0e52e9e000 r-xp 00000000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e53073000-7f0e53079000 rw-p 00000000 00:00 0
7f0e5309c000-7f0e5309d000 rw-p 00000000 00:00 0
7f0e5309d000-7f0e5309e000 r--p 00025000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309e000-7f0e5309f000 rw-p 00026000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309f000-7f0e530a0000 rw-p 00000000 00:00 0
7ffe1d800000-7ffe1d821000 rw-p 00000000 00:00 0 [stack]
7ffe1d9b0000-7ffe1d9b2000 r--p 00000000 00:00 0 [vvar]
7ffe1d9b2000-7ffe1d9b4000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
This program is acting as a template for what will be a linked list of bunny objects with unique names and features.
int Nodes::insert()
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->name = getName();
new_node->prev = NULL;
new_node->next = head;
if (head != NULL)
head->prev = new_node;
head = new_node;
return 0;
I've designed it so that ideally when a new node is made the each variable has relevant data assigned from a function call. For example:
new_node->name = getName();
In the instance of my memory issue the first node created runs fine but the second one produces the error at this^ function call assignment.
The full code:
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using std::vector;
using std::string;
using std::cout;
using std::endl;
class Node
public:
int data;
string name;
Node* next;
Node* prev;
;
class Nodes
private:
Node* head = NULL;
public:
int insert();
void display();
int randomGen(int n);
string getName();
void extract(std::vector<string> *vect, string fileName);
;
void Nodes::display()
Node* next = head;
while(next != NULL)
cout << next->name << 'n';
next = next->next;
int Nodes::insert()
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->name = getName();
new_node->prev = NULL;
new_node->next = head;
if (head != NULL)
head->prev = new_node;
head = new_node;
return 0;
int Nodes::randomGen(int n)
srand((int)time(0));
return (rand() % n) + 1;
// --
void Nodes::extract(vector<string> *vect, string fileName)//pulls lines from a file and splits the names up from each line using tokenization indexing them for use
std::ifstream accessFile;
accessFile.open (fileName);
string pulledLine;
if (accessFile.is_open())
string extractedName;
getline(accessFile, pulledLine);
for(auto p: pulledLine)
if(p != ' ')
if(extractedName != " ")
extractedName += p;
else
extractedName = p;
;
else
vect->push_back(extractedName);
extractedName = " ";
string Nodes::getName()//utilizes extract() to pull names and combines them using randomGen() to pull a name from a random index.
std::vector<string> fNames = " ";
std::vector<string> lNames = " ";
const string fileName[2] = "fnames.txt", "lnames.txt";
extract(&fNames, fileName[0]);
extract(&lNames, fileName[1]);
/* original formula for pulling random names
string fullName = fNames[randomGen(sizeof(fNames))] + " " + lNames[randomGen(sizeof(lNames))];
*/
//put here so I could focus on memory leak.
string fullName = "cjaz";
return fullName;
// --
int main()
Nodes control;
control.insert();
control.insert();
// control.insert();
// control.insert();
control.display();
I'm looking to figure out the source of this memory issue and bring to light any bad practices or design flaws that I might be using.
Any and all feedBack is appreciated, Thank you!
object-oriented c++11 linked-list memory-management
$endgroup$
$begingroup$
Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– David White
3 mins ago
add a comment |
$begingroup$
The nodes seems to connect just fine until implementing my getName() function for variable assignment:
> *** Error in `./zBunny.exe': free(): invalid pointer: 0x00007f0e528dab78 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f0e5258d7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f0e5259637a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f0e5259a53c]
./zBunny.exe[0x4016e8]
./zBunny.exe[0x401ef2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f0e52536830]
./zBunny.exe[0x401569]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00604000-00605000 r--p 00004000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00605000-00606000 rw-p 00005000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
01f52000-01f84000 rw-p 00000000 00:00 0 [heap]
7f0e4c000000-7f0e4c021000 rw-p 00000000 00:00 0
7f0e4c021000-7f0e50000000 ---p 00000000 00:00 0
7f0e5220d000-7f0e52315000 r-xp 00000000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52315000-7f0e52514000 ---p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52514000-7f0e52515000 r--p 00107000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52515000-7f0e52516000 rw-p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52516000-7f0e526d6000 r-xp 00000000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e526d6000-7f0e528d6000 ---p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528d6000-7f0e528da000 r--p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528da000-7f0e528dc000 rw-p 001c4000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528dc000-7f0e528e0000 rw-p 00000000 00:00 0
7f0e528e0000-7f0e528f6000 r-xp 00000000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e528f6000-7f0e52af5000 ---p 00016000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af5000-7f0e52af6000 rw-p 00015000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af6000-7f0e52c68000 r-xp 00000000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52c68000-7f0e52e68000 ---p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e68000-7f0e52e72000 r--p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e72000-7f0e52e74000 rw-p 0017c000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e74000-7f0e52e78000 rw-p 00000000 00:00 0
7f0e52e78000-7f0e52e9e000 r-xp 00000000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e53073000-7f0e53079000 rw-p 00000000 00:00 0
7f0e5309c000-7f0e5309d000 rw-p 00000000 00:00 0
7f0e5309d000-7f0e5309e000 r--p 00025000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309e000-7f0e5309f000 rw-p 00026000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309f000-7f0e530a0000 rw-p 00000000 00:00 0
7ffe1d800000-7ffe1d821000 rw-p 00000000 00:00 0 [stack]
7ffe1d9b0000-7ffe1d9b2000 r--p 00000000 00:00 0 [vvar]
7ffe1d9b2000-7ffe1d9b4000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
This program is acting as a template for what will be a linked list of bunny objects with unique names and features.
int Nodes::insert()
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->name = getName();
new_node->prev = NULL;
new_node->next = head;
if (head != NULL)
head->prev = new_node;
head = new_node;
return 0;
I've designed it so that ideally when a new node is made the each variable has relevant data assigned from a function call. For example:
new_node->name = getName();
In the instance of my memory issue the first node created runs fine but the second one produces the error at this^ function call assignment.
The full code:
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using std::vector;
using std::string;
using std::cout;
using std::endl;
class Node
public:
int data;
string name;
Node* next;
Node* prev;
;
class Nodes
private:
Node* head = NULL;
public:
int insert();
void display();
int randomGen(int n);
string getName();
void extract(std::vector<string> *vect, string fileName);
;
void Nodes::display()
Node* next = head;
while(next != NULL)
cout << next->name << 'n';
next = next->next;
int Nodes::insert()
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->name = getName();
new_node->prev = NULL;
new_node->next = head;
if (head != NULL)
head->prev = new_node;
head = new_node;
return 0;
int Nodes::randomGen(int n)
srand((int)time(0));
return (rand() % n) + 1;
// --
void Nodes::extract(vector<string> *vect, string fileName)//pulls lines from a file and splits the names up from each line using tokenization indexing them for use
std::ifstream accessFile;
accessFile.open (fileName);
string pulledLine;
if (accessFile.is_open())
string extractedName;
getline(accessFile, pulledLine);
for(auto p: pulledLine)
if(p != ' ')
if(extractedName != " ")
extractedName += p;
else
extractedName = p;
;
else
vect->push_back(extractedName);
extractedName = " ";
string Nodes::getName()//utilizes extract() to pull names and combines them using randomGen() to pull a name from a random index.
std::vector<string> fNames = " ";
std::vector<string> lNames = " ";
const string fileName[2] = "fnames.txt", "lnames.txt";
extract(&fNames, fileName[0]);
extract(&lNames, fileName[1]);
/* original formula for pulling random names
string fullName = fNames[randomGen(sizeof(fNames))] + " " + lNames[randomGen(sizeof(lNames))];
*/
//put here so I could focus on memory leak.
string fullName = "cjaz";
return fullName;
// --
int main()
Nodes control;
control.insert();
control.insert();
// control.insert();
// control.insert();
control.display();
I'm looking to figure out the source of this memory issue and bring to light any bad practices or design flaws that I might be using.
Any and all feedBack is appreciated, Thank you!
object-oriented c++11 linked-list memory-management
$endgroup$
The nodes seems to connect just fine until implementing my getName() function for variable assignment:
> *** Error in `./zBunny.exe': free(): invalid pointer: 0x00007f0e528dab78 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f0e5258d7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f0e5259637a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f0e5259a53c]
./zBunny.exe[0x4016e8]
./zBunny.exe[0x401ef2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f0e52536830]
./zBunny.exe[0x401569]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00604000-00605000 r--p 00004000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00605000-00606000 rw-p 00005000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
01f52000-01f84000 rw-p 00000000 00:00 0 [heap]
7f0e4c000000-7f0e4c021000 rw-p 00000000 00:00 0
7f0e4c021000-7f0e50000000 ---p 00000000 00:00 0
7f0e5220d000-7f0e52315000 r-xp 00000000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52315000-7f0e52514000 ---p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52514000-7f0e52515000 r--p 00107000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52515000-7f0e52516000 rw-p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52516000-7f0e526d6000 r-xp 00000000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e526d6000-7f0e528d6000 ---p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528d6000-7f0e528da000 r--p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528da000-7f0e528dc000 rw-p 001c4000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528dc000-7f0e528e0000 rw-p 00000000 00:00 0
7f0e528e0000-7f0e528f6000 r-xp 00000000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e528f6000-7f0e52af5000 ---p 00016000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af5000-7f0e52af6000 rw-p 00015000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af6000-7f0e52c68000 r-xp 00000000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52c68000-7f0e52e68000 ---p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e68000-7f0e52e72000 r--p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e72000-7f0e52e74000 rw-p 0017c000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e74000-7f0e52e78000 rw-p 00000000 00:00 0
7f0e52e78000-7f0e52e9e000 r-xp 00000000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e53073000-7f0e53079000 rw-p 00000000 00:00 0
7f0e5309c000-7f0e5309d000 rw-p 00000000 00:00 0
7f0e5309d000-7f0e5309e000 r--p 00025000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309e000-7f0e5309f000 rw-p 00026000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309f000-7f0e530a0000 rw-p 00000000 00:00 0
7ffe1d800000-7ffe1d821000 rw-p 00000000 00:00 0 [stack]
7ffe1d9b0000-7ffe1d9b2000 r--p 00000000 00:00 0 [vvar]
7ffe1d9b2000-7ffe1d9b4000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
This program is acting as a template for what will be a linked list of bunny objects with unique names and features.
int Nodes::insert()
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->name = getName();
new_node->prev = NULL;
new_node->next = head;
if (head != NULL)
head->prev = new_node;
head = new_node;
return 0;
I've designed it so that ideally when a new node is made the each variable has relevant data assigned from a function call. For example:
new_node->name = getName();
In the instance of my memory issue the first node created runs fine but the second one produces the error at this^ function call assignment.
The full code:
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using std::vector;
using std::string;
using std::cout;
using std::endl;
class Node
public:
int data;
string name;
Node* next;
Node* prev;
;
class Nodes
private:
Node* head = NULL;
public:
int insert();
void display();
int randomGen(int n);
string getName();
void extract(std::vector<string> *vect, string fileName);
;
void Nodes::display()
Node* next = head;
while(next != NULL)
cout << next->name << 'n';
next = next->next;
int Nodes::insert()
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->name = getName();
new_node->prev = NULL;
new_node->next = head;
if (head != NULL)
head->prev = new_node;
head = new_node;
return 0;
int Nodes::randomGen(int n)
srand((int)time(0));
return (rand() % n) + 1;
// --
void Nodes::extract(vector<string> *vect, string fileName)//pulls lines from a file and splits the names up from each line using tokenization indexing them for use
std::ifstream accessFile;
accessFile.open (fileName);
string pulledLine;
if (accessFile.is_open())
string extractedName;
getline(accessFile, pulledLine);
for(auto p: pulledLine)
if(p != ' ')
if(extractedName != " ")
extractedName += p;
else
extractedName = p;
;
else
vect->push_back(extractedName);
extractedName = " ";
string Nodes::getName()//utilizes extract() to pull names and combines them using randomGen() to pull a name from a random index.
std::vector<string> fNames = " ";
std::vector<string> lNames = " ";
const string fileName[2] = "fnames.txt", "lnames.txt";
extract(&fNames, fileName[0]);
extract(&lNames, fileName[1]);
/* original formula for pulling random names
string fullName = fNames[randomGen(sizeof(fNames))] + " " + lNames[randomGen(sizeof(lNames))];
*/
//put here so I could focus on memory leak.
string fullName = "cjaz";
return fullName;
// --
int main()
Nodes control;
control.insert();
control.insert();
// control.insert();
// control.insert();
control.display();
I'm looking to figure out the source of this memory issue and bring to light any bad practices or design flaws that I might be using.
Any and all feedBack is appreciated, Thank you!
object-oriented c++11 linked-list memory-management
object-oriented c++11 linked-list memory-management
asked 5 mins ago
VildjhartaVildjharta
124
124
$begingroup$
Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– David White
3 mins ago
add a comment |
$begingroup$
Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– David White
3 mins ago
$begingroup$
Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– David White
3 mins ago
$begingroup$
Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– David White
3 mins ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217861%2flinked-list-with-memory-leak-issues%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217861%2flinked-list-with-memory-leak-issues%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– David White
3 mins ago