C :: Linked List - Freeing Memory / Setting Things To Null

Feb 20, 2013

I've been working on this program and am finished with what I want it to accomplish. But the last step I'd like to take is to delete the linked list and free all the memory. When I free the memory the way I am right now, the program breaks. I'm not sure how I should go about freeing and setting things to NULL.

Code:
typedef struct node {
char* value;
struct node* next;
} LinkedList;

//function prototypes
void llAdd(LinkedList** ll, char* value);

[Code] .....

View 6 Replies


ADVERTISEMENT

C/C++ :: Freeing Allocated Memory In Doubly Linked List

Feb 21, 2015

I am having issues freeing memory that I allocated when adding a node to a doubly linked list. I have tried adding free() at the end of the remove function from the list with no luck. I have tried using all sorts of temporary nodes and dummy nodes to free without losing node information. Have tried storing current node, moving to next one, then freeing the old current one, without luck. Everytime I try to free a node it destroys the list. It loses important node information and can no longer operate properly and I am met with all sorts of memory crashes. I will post my add and delete nodes functions here:

/**
* Adds a node to a given list
*
* @param q pointer a a list
* @param node pointer to the node to be added
*/
void list_add(list *q, path *node){
path *pn;
if(!(pn = (path*)malloc(sizeof(*pn)))){
perror("malloc");
exit(1);

[Code] ....

Those free's at the end are to get rid of nodes I malloced in find_path. This find_path works really well when run once lol. It finds shortest path and prints it no problem, but doing it over and over again will be problematic as it is leaking almost every bit of memory it uses />.

So in short, how to free an allocated node when I remove it from a list while still being able to use it? I have tried moving the remove function to different locations like the end of the file and still no luck. I even tried allocating a new current_node each iteration of while loop, using it, then freeing it at the end of the while loop and took out the allocation in the list_add() function. This didn't work either />. How to stop the leakage.

View 8 Replies View Related

C/C++ :: Freeing Linked List Node With Pointer?

Mar 30, 2015

I'm having a bit of an issue here. I have a linked list where each node contains a pointer to a string (which has been malloc'd when the node was created and inserted) and a pointer to the next node in the linked list.

I'm creating a function which will free the node (or effectively delete it). However, I'm receiving a free(): invalid pointer error.

My function:

void removeNode(node *(*nodeToRemove))
{
free((*nodeToRemove)->data);
(*nodeToRemove)->next = NULL;
free(*nodeToRemove);
}

Is this how I should go about freeing this node?

View 5 Replies View Related

C++ :: Setting Up Linked List Class With Templates?

Dec 2, 2013

I am trying to implement a linked list using a class template, however each of my classes that I want in the list all inherit from an Account class. I have attempted to template the linked list class as account however Ive come into errors I can't resolve. How I can go about this?

The error exists within the customer.cpp class where it says

14IntelliSense: a value of type "Account *" cannot be assigned to an entity of type "CurrentAccount *"f:Further C++Assignment with TemplatesAssignmentCustomer.cpp229Assignment

Customer class:

#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <iostream>
#include <string>
#include "Account.h"
#include "AccountLinkedList.h"

[Code] .....

View 2 Replies View Related

C++ :: Freeing Memory Without Destructing

Sep 12, 2013

Say we have this crude memory pool that is on the heap:

char* mpool = (char*)malloc(sizeof(char) * 4096);

And we used placement new to construct a couple objects in place:

char* next = mpool;
void* fooptr = new (next) FOO();
next += sizeof(FOO);
void* bazptr = new (next) Baz();
next += sizeof(Baz);

And at the end, let's say the memory is freed without having called the object destructors:

free(mpool);

What exactly happens? I haven't found any page that states what happens in this situation. Does it cause undefined behavior?

View 4 Replies View Related

C++ :: Freeing Dynamic Arrays From Memory

Sep 27, 2013

So I'm trying to wrap my head around dynamic arrays, and I've finally figured out how to build one, using

int arraysize = randomnumber;
int* arrayname = new int[arraysize];

Now, how to delete the array. Can I free the whole array from any scope in my program using a simple line like delete arrayname;?

Also, since I'm working in heap, I can't rely on the debugger to show me when I'm doing something wrong, If I create a struct such as :

struct twoints {int one, int two};

and then try to create a dynamic array of said struct using

int arraysize = 5;
twoints* arrayname = new twoints[arraysize];

Am I basically creating an array of 5 two ints?

//is it the same as doing this?

twoints arrayname[5];

View 8 Replies View Related

C :: Allocating And Freeing Memory For Array Of Structures

Jul 27, 2014

I have to allocate memory for an array of structures, and my structure looks as following:

Code:
typedef struct {
char *name;
Uint *start_pos;
Uint len;
}
example_struct;

And now I want to allocate memory, for a variable number (so an array) of example_struct, so I first do:
Code:

example struct *all_struct;
int total_num = 3;
//will be set somehow, but for the example I set it on 3 all_struct = malloc (sizeof(example_struct) * total_num);

And now, as far as I now, I will have to allocate for each field of the structure memory, in order to be able to use it later. But I have problem at this point, a problem of understanding:

- I just allocated memory for 3 structures, but don't I have to allocate then memory for each structure separately, or can I just now allocate the fields like this:

Code: all_struct[0].name = malloc.....

But if yes, why the hell this works...

View 10 Replies View Related

C++ :: Setting Entire Array To NULL

Dec 12, 2013

Okay so I've declared an array like this.

Foo *Blocks[100][100][10000] = {0};

And as far as my understanding goes This creates an array with every member set to NULL.

And then later on in my code some of these get given values using:

Blocks[a][b][c] = new Foo;

And then when I want to unload I would think that I could just go

Blocks = {0};

But obviously this doesn't work.

So I was wondering if there was a way of doing This, with out creating a loop and changing every one to NULL manually.

View 8 Replies View Related

C :: Setting Hash-table Entries To NULL?

Apr 21, 2013

Here is my code so far:

Code:
//LINEAR CHAINING OR SEPERATE CHAINING...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_USERNAME_LEN 15
#define MAX_PASSWORD_LEN 20

[Code] ....

When I compile I get this error:

Code:
simpleauth.c: In function ‘initialize_table’:
simpleauth.c:179:30: error: incompatible types when assigning to type ‘DBEntry’ from type ‘void *’

I am sure its a simple error but I cannot seem to figure out how to set all the entries in the database to NULL. My plan is this:

1. Set all entries to NULL.
2. Check if entry is empty, if empty insert into hash-location, if not increment until empty...

Part 2 seems easy to me, but I cannot set them to empty....

View 3 Replies View Related

C :: Creating Linked List Of Memory Blocks

Apr 22, 2014

I am creating linked list of memory blocks. Allocate a block of 512 bytes and insert into the first node of list. then Allocate a 2nd block of same size and add to the list. now free each block from the list.

View 14 Replies View Related

C++ :: Creating A Linked List Of Common Elements From Two Other Linked Lists

Apr 29, 2013

I'm trying to write a function that takes two linked lists and creates a third one with only the common elements.

It assumes the first list (the caller) has no dups, but it doesn't seem to be working. The program doesn't crash, it just hangs when it is supposed to display L3 (the third list)..everything else runs and is displayed fine.

template <typename T>
LList <T> LList <T>:: common (LList <T> &B)//common fct
{
Node <T> *hunter1 = Head;

[Code]......

View 10 Replies View Related

C++ :: How To Access Different Things In Two Dimensional Arrays

Oct 28, 2013

So I started taking a C++ class two weeks ago and we just got to arrays. I am confused on how to pull data from two dimensional arrays. The assignment for this topic reads as follows. Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:

a. Function getData: This function reads and stores data in the two dimensional array.
b. Function averageHigh: This function calculates and returns the average high temperature for the year.
c. Function averageLow: This function calculates and returns the average low temperature for the year.
d. Function indexHighTemp: this function returns the index of the highest high temperature in the array.
e. Function indexLowTemp: this function returns the index of the lowest low temperature in the array.

I made the getData function. I do know that i am supposed to more then likely use nested loops on each function to iterate through each column. I have been trying to figure this out. I first saw in my book that the array could be variables so naturally I tryed to make the array loop and increment up the column and row each time a item is stored to make it easier to loop in different functions but then i found out that they have to have a constant value and that doesn't work. Here is the code for index high temp that i have been working on. I honestly feel like i'm over thinking this and its easier then what i'm making it out to be. Here is all of the code.

Code:
#include <iostream>
#include <string>
using namespace std;
void getData(int month);
void averageHigh(int month);
void averageLow(int month);

[Code]...

View 4 Replies View Related

Visual C++ :: Do Other Things Whilst In A Loop

Mar 25, 2013

I'm writing a piece of software in MFC. In one function, I have a while loop using "KeepLooping" as the condition.

"KeepLooping" is a boolean that is set to false by a toolbar button, key press or menu option. The problem is, when the loop starts running, its obviously so busy in the loop that button presses, toolbar buttons and menu clicks do nothing, so the loop never exits.

What's the best (or should that be simplest?) way around this? I remember in VB you could rather crudely just use DoEvents in a loop to make sure it carried on doing other things in the mean time, but I'm guessing its not that simple in C++.

View 13 Replies View Related

C :: Insert Linked List Into Another Linked List

Jun 29, 2013

I have a linked list comprised of chars like so...

Code:

node1 - "p"
node2 - "o"
node3 - "p"

I need a function that will take in three perameters...node *replaceChar(node *head, char key, char *str)Stipulations of this function. head is the head of the list, 'key' and 'str' are guaranteed to contain alphanumeric characters only (A-Z, a-z, and 0-9). str can range from 1 to 1023 characters (inclusively). So if I call this function with these perameters..

Code:

node *head == /*the head of the list to be examined*/
char key == "p"char *str == "dog"The new list will look like this...
node1 - 'd'
node2 - 'o'
node3 - 'g'
node4 - 'o'
node5 - 'd'
node6 - 'o'
node7 - 'g'

All instances of 'p' were replaced with 'dog' I have a toString function which takes in a string and converts it to a linked list and returns the head. So assume that you can call the function on str = "dog" so...

Code:

toString(str) == /*this will return the head to the list made from the str*/

If it's unclear what my question is...I am stumped on how to write the replaceChar function the one that takes in three perameters..

View 3 Replies View Related

C :: Linked List / Adding Element To Beginning Of List

Dec 31, 2014

Code:

// Write a function called insertEntry() to insert a new entry into a linked list.

Have the procedure take as arguments a pointer to the list entry to be inserted (of type struct entry as defined in this chapter), and a pointer to an element in the list after which the new entry is to be inserted.

// The function dveloped in exercise 2 only inserts an element after an existing element in the list, thereby prenting you from inserting a new entry at the front of the list.

(Hint: Think about setting up a special structure to point to the beginning of the list.)

#include <stdio.h
struct entry1 {
int value;
struct entry1 *next;
};

[code]...

This is a working version of the exercise, but I don't think I'm doing what's asked. I was able to add an element to the beginning of the list using an if statement, not creating a special structure that points to the beginning of the list. How would I go about creating a special structure that points to the beginning of the list to add a new element at the beginning of the list?

View 8 Replies View Related

C++ :: Null Terminator Same As Null And Through False Value?

Feb 18, 2014

I am looking at one of the functions of an exercise:

void escape(char * s, char * t) {
int i, j;
i = j = 0;
while ( t[i] ) {
/* Translate the special character, if we have one */
switch( t[i] ) {

[code]...

Notice the while loop evaluates the current value in t to true or false. When we hit the null terminator, does that get evaluated as 0 and hence evaluates as a falsy value so the while loop exits?

View 1 Replies View Related

C :: War Card Game With Linked Lists And Dynamic Memory

Apr 23, 2013

I am struggling to finish up my game of War. There are a few problems I have ran in to. My shuffle function seems to work fine. I believe my problem lies within my game rules. When I run my code, the game usually plays all the way through but almost always ends in 'War'. I am struggling to get the game to restart after running once. I am not sure how to clear the list and start over.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>

[Code] ....

View 4 Replies View Related

C++ :: Linked List Delete List?

May 30, 2013

I'm working on a linked list and was wondering how this looks to everybody else for a deleteList function.

void deleteList(Node* head)
{
Node* iterator = head;
while (iterator != 0)

[code].....

View 6 Replies View Related

C :: Free Memory From A Dynamically Allocated Array Of Pointers To Linked Lists

Feb 26, 2013

Having some frustrating issues trying to free memory from a dynamically allocated array of pointers to linked lists. I think the problem is in how I initialize the pointers to NULL. Is there a more elegant way to have the program recognize that the list is empty so it knows to create a head node for the linked list in the function 'add_end_stub_to_array'?

I ran the code through Valgrind and it says that memory is definitely lost from this array.

This is the structure definition.

Code: struct stub_edge {
int loc_id;
int anim_type;
int mkt;
struct stub_edge *next_node;
};

Here is the code snippet from main allocating and deallocating memory to the array.

Code:

struct stub_edge **stub_list = (struct stub_edge **)malloc( sizeof(struct stub_edge *) * 12);
for (i = 0; i < 12; i++)
{
stub_list[i] = (struct stub_edge *)malloc(sizeof(struct stub_edge));
stub_list[i] = NULL;
}
stub_list = add_end_stub_to_array(end_stubs, stub_list);
destroy_end_stub_array(stub_list);

Here the function for adding nodes to the lists by reading through a dynamically allocated 2D array. (The end_stubs array is ordered by month and each linked list represents events occuring within the month).

Code:

struct stub_edge **add_end_stub_to_array(int **end_stubs, struct stub_edge **list)
{
long int i = 0;
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
int break1 = 0;
while(i < num_edges && break1 == 0 && mon < 12)

[Code]...

Here is the function for freeing memory from the list.

Code:

void destroy_end_stub_array(struct stub_edge **list)
{
if(list != NULL)
{
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
for(mon = 0; mon < 12; mon++)

[Code]...

View 1 Replies View Related

C# :: Session Is Null But Not Null?

Mar 31, 2015

When you login to my site my loginservice which is done by ajax and json make a session called context.Session["Name"]. With BreakPoints it shows that everything is good and the variables are in place. However when I use Session["Name"] it comes out as null.

I will add my code at the bottem not

using System;
using System.Collections.Generic;
using System.Linq;

[Code].....

View 2 Replies View Related

C :: Doubly Linked List

May 15, 2013

I need to make the functions using these function prototypes. I am mainly having problems with GetFirst() and SwapData() but how to do it..

Header File with Prototypes
Code:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
/**
* @file
* This file provided a doubly-linked list implementation capable of storing any arbitrary data.
* The list implementation relies on a chain metaphor: a list is merely a sequence of links
* (ListItems) and there is no separate construct to represent the entire list, each ListItem in it

[Code]....

View 14 Replies View Related

C :: Linked List Search

May 7, 2013

I read an article on linked list here: C Linked List Data Structure Explained with an Example C Program

Code:

struct test_struct* search_in_list(int val, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
}

[code].....

What is "if(prev)"? Wouldn't "prev" always have the same value? Secondly, if tmp is NULL (which will be the case when the loop if(ptr->val == val) finds a match the first time it is run), is *prev assigned a NULL?

View 1 Replies View Related

C :: Double Linked List

Jan 2, 2014

I'm having a small issue here with my linked list.I built a linked list with strings and it worked perfectly.Now since i'm using strtok() to separate the string.for now here's what i've got:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct dict_word *word;
typedef struct node *Node;
typedef struct double_linked_list *DLL;
}

[code]....

View 1 Replies View Related

C :: Static Linked List

Jan 6, 2014

I have the codes for the functions: "Given the functions that we have discussed and defined in the class and the code that we have created, create the code for the Delete Node function and the Search function. Create an application that will incorporate all the functions that we have discussed and the new ones that you need to create. The application should allow the linked list to be built initially from a text file. The application should then allow for the user select an operation to perform. The acceptable operations are

- to search for an item
- to delete an item
- to enter a new item
- to exit the application

After the initial build and loading of data from the textfile, the application should print a listing of each item in the list and the node that it is located in. after a delete or insert, the application should display an output of the list again showing each item in the list and the node that it resides in.

The data item for this problem will be the inventory structure that we created in class and the data is in the inventory file that you already have downloaded."

View 4 Replies View Related

C :: Singly Linked List

Mar 7, 2013

I am having difficulty getting my program to run. I have looked and looked many times trying to figure out what I am doing wrong. I have a hunch that it's my load function that's acting up, but from my perspective, my creation of my singly linked list looks good. From the creation of my singly linked list, does anything stand out as being abnormal?

Code:

typedef struct Node {
char word[LENGTH+1];
struct Node *Next;
} Node;
}

[code]....

View 6 Replies View Related

C :: Sorted Linked List

Apr 2, 2013

I been ask to create a sorted linked list module, and to test each function with 1 unit, 2 +.so i wrote my addtofront function( we have to use the same as the teacher)

Code:

struct MyList{
char * name;
Sorted * AddmyChar;
struct MyList * next;
}

[code]....

but i cant seem to test it because it always display a seg fault.... i dont know what i did wrong....how do i call this function in main to display a string i want?

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved