Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,452 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,144 people online right now. Registration is fast and FREE... Join Now!




making a delete func()

 
Reply to this topicStart new topic

making a delete func(), Im using DevC++..

cyvill_domingo74
22 Jan, 2008 - 10:23 PM
Post #1

New D.I.C Head
*

Joined: 2 Sep, 2007
Posts: 41


My Contributions
CODE
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define FALSE 0;
#define TRUE 1;

typedef char string10[11];
typedef char string20[21];

struct CustRec{
string10 Name;
string20 Address;
struct CustRec *Prev, *Next;
};

typedef struct CustRec *CustPtr;

CustPtr FirstCust, LastCust, PrevCust, CurrentCust;

void AddRecord(string10 N, string20 A)
{
    if(FirstCust == NULL){
    CurrentCust = malloc(sizeof(struct CustRec));
    strcpy(CurrentCust->Name,N);
    strcpy(CurrentCust->Address,A);
    CurrentCust->Next = NULL;
    CurrentCust->Prev = NULL;
    FirstCust = CurrentCust;
    LastCust = CurrentCust;
    }
    else {

    PrevCust = LastCust;
    CurrentCust = malloc(sizeof(struct CustRec));
    strcpy(CurrentCust->Name,N);
    strcpy(CurrentCust->Address,A);
    PrevCust->Next = CurrentCust;
    CurrentCust->Next = NULL;
    CurrentCust->Prev = PrevCust;
    LastCust = CurrentCust;
    }

    }


void ListForwards(void)
{
    CurrentCust = FirstCust;
    while (CurrentCust !=NULL)
    {
     printf("Name : %s\n",CurrentCust->Name);
     printf("Address : %s\n",CurrentCust->Address);
     CurrentCust = CurrentCust->Next;
    }
    printf("\n");
}

void ListBackwards(void)
{
CurrentCust = LastCust;
while (CurrentCust !=NULL) {
printf("Name : %s\n", CurrentCust->Name);
printf("Address : %s\n", CurrentCust->Address);
CurrentCust = CurrentCust->Prev;
}
printf("\n");
}// ListBackwards ends here.

void DEL
{
     ???????
     ???????
     ???????
     ??????
     ??????
}

int main(void)
{
FirstCust = NULL;
LastCust = NULL;
PrevCust = NULL;
CurrentCust = NULL;
AddRecord("Bernil","Ecoland");
AddRecord("Andrei","Nova Tierra");
AddRecord("Loresh","Central Park");
AddRecord("Carinna","Ladislawa");
AddRecord("Jovan","Woodridge");

ListForwards();
ListBackwards();
ListForwards();
getch();
}


QUOTE
how can i make a delete func() in this code... please guide me or give some tips to read about... tnx

User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Making A Delete Func()
23 Jan, 2008 - 08:19 AM
Post #2

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
This might work it just wraps the "free" function:
CODE

int del(CustRec cr)
{
  if (cr != null)
   return free(cr);
  return 1;
}


Reference:
http://gbdk.sourceforge.net/doc/html/c0508.html
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Making A Delete Func()
23 Jan, 2008 - 01:07 PM
Post #3

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,582



Thanked: 12 times
Dream Kudos: 325
My Contributions
Why bother wrapping it?
User is online!Profile CardPM
+Quote Post

cyvill_domingo74
RE: Making A Delete Func()
24 Jan, 2008 - 03:32 AM
Post #4

New D.I.C Head
*

Joined: 2 Sep, 2007
Posts: 41


My Contributions
QUOTE(Tom9729 @ 23 Jan, 2008 - 02:07 PM) *

Why bother wrapping it?


now im confuse.. wat's wrapping by the way?

sorry im just a novice in programming..

sorry for asking silly questions..
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Making A Delete Func()
24 Jan, 2008 - 09:10 AM
Post #5

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
Wrapping is just to place a function around an existing function that does the same thing.

I wrapped it for two reasons:
1. To show that wrapping functions can add to what the function does.
2. To return a value of 1 if the object is already null.
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Making A Delete Func()
24 Jan, 2008 - 09:58 AM
Post #6

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,582



Thanked: 12 times
Dream Kudos: 325
My Contributions
I'm just saying, why bother writing another function that just calls free(); and basically nothing else.

@cyvill, to free up memory allocated by malloc();, you use the free(); function.

For example.
CODE

#include <stdlib.h>
#include <memory.h>
#include <string.h>

...

char* str;

str = malloc(strlen("Hello world!")); /* Allocate memory to str. */
strcpy(str, "Hello world!");

printf("%s\n", str);

free(str); /* Free up the memory used by str. */


The one thing you need to be careful about is to never try to free something that wasn't allocated by malloc.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 01:08PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month