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;








-3












$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!









share









$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

















-3












$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!









share









$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













-3












-3








-3





$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!









share









$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





share












share










share



share










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
















  • $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










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
);



);













draft saved

draft discarded


















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















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar