4 Replies - 281 Views - Last Post: 23 November 2011 - 06:45 AM Rate Topic: -----

#1 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Am i misusing "Delete" for garbage collection?

Posted 22 November 2011 - 07:08 PM

Here i have written an object oriented program to generate any music scale on the fly. I have been told my a few programmers on here, far more experienced then myself, that it is not necessary to make an object to accomplish this task. Therefore i will perquisite by telling you that i wrote this program to learn the basics of O.O.P. in C++, and it is not meant to be particularly useful as an object.
I know that C++ does not have automatic garbage collection, and that you must manually free up any memory you use on the fly. I have read that you only need to use the "delete" command if you have used "new" to manually allocate uninitialized array space. So my question is, am i misusing the "delete" command? and if so, how do i delete normal variables?
Main.cpp
#include <iostream>
#include <string>
#include "musickey.h"

using namespace std;

int main()
{
	string key, mode;
    cout<<"Input Key: ";
	cin >>key;

	cout<<"Input Mode: ";
	cin >>mode;
	musickey mykey(key, mode);
	cout<<"\nThe "<<key<<" "<<mode<<" Scale is "<<mykey.toString()<<endl
		<<"\nChromatic: "<<mykey.Chromatic();
	cin.ignore(80,'\n');
	cin.get();
return 0;
}






Musickey.h
#ifndef MUSICKEY_H
#define MUSICKEY_H
#include<string>

using namespace std;

class musickey
{
    public:
        musickey();
        musickey(string &, string &);
        void setKey(string &);
        void setMode(string &);
        string toString();
        string Chromatic();
    private:
        string key, mode, scale;
        string generateScale(string &incKey, string &incMode);
};

musickey::musickey()
{
    key = "C";
    mode = "major";
}

musickey::musickey(string &incKey, string &incMode )
{
    key = incKey;
    mode = incMode;
}

void musickey::setKey(string &incString)
{
    key = incString;
    delete &incString;
}

void musickey::setMode(string &incString)
{
    mode = incString;
    delete &incString;
}
string musickey::generateScale(string &incKey, string &incMode)
{
    //initialize/declare everything
	const string chromatic[12]=
    {
        "A","A#","B","C","C#","D","D#","E","F","F#","G","G#"
    };
	string formula;
    string mode;
    string scale;
    unsigned short int last, next;
	const string modeoptions[][2] =
	{
        {"major", "wwhwwwh"},
        {"minor", "whwwhww"},
        {"dorian", "whwwwhw"},
        {"phrygian", "hwwwhww"},
        {"lydian", "wwwhwww"},
        {"mixolydian","wwhwwww"},
        {"locrian", "hwwwwww"},
	};
	//\initialize/declare everything

	//take care of case sensitivity
	for(unsigned short int i = 0; i < incMode.length(); i++)
	{
		incMode[i] = tolower(incMode[i]);
	}
	incKey[0] = toupper(incKey[0]);

	//\take care of case sensitivity

	//SetKey
    unsigned short int i;
    for(i = 0; (chromatic[i] != incKey) && (i < 12) ; i++){}
    if(i == 12)
    {
        return "\nError\nKey Must Be:\nA, A#, B, C, C#, D, D#, E, F, F#, G, G#";
    }
    last = i;
    //\SetKey

    //SetMode
    bool brk = false;
    for(unsigned short int i = 0; i < 7; i++)
    {
        for(int ii = 0; !brk ; i++)
        {
            if (i > 7)
            {
               return "\nError\nMode Must Be:\nMajor, Minor, Dorian, Phrygian, Lydian, Mixolydian, Locrian";
            }
            if(incMode == modeoptions[i][ii])
            {
                formula = modeoptions[i][ii+1];
                brk = true;
                break;
            }
        }
        if(brk)
        {
            break;
        }
    }
    delete &brk; //brk is no longer needed
    //\SetMode

    //GetScale
    for(unsigned short int i = 0; i < 8; i++)
    {
        scale.append(chromatic[last] + " ");
        if(formula[i] == 'w')
        {
            next = 2;
        }
        if(formula[i] == 'h')
        {
            next = 1;
        }
        next += last;
        if(next > 11)
        {
            next -= 12;
        }
        last = next;
    }
    //\GetScale

return scale;
    delete &scale;
    delete &modeoptions;
    delete &i;
    delete &formula;
    delete &mode;
    delete &chromatic;
}

string musickey::toString()
{
    string scale = generateScale(key, mode);
    return scale;
    delete &scale;
}
string musickey::Chromatic()
{
    return "A A# B C C# D D# E F F# G G#";
}
#endif //\MUSICKEY.H



you can see here where i use the "delete" commands

Is This A Good Question/Topic? 0
  • +

Replies To: Am i misusing "Delete" for garbage collection?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Am i misusing "Delete" for garbage collection?

Posted 22 November 2011 - 07:39 PM

You have utterly not understood delete. Actually, based on your scope, you probably don't understand the notion of object lifetime anyway.

Let's say you want an integer. C++ provides you three forms of allocation. Static, automatic, and dynamic. Static allocation means it exists for the lifetime of the program. This is not something you control. It is how it is in C++. A good example of a statically allocated variable is any global.

Automatic variables have the lifetime of the function they are in. They are "alive" for the scope of the function. A local variable in a function has automatic storage. Again, this is how it is in C++. This is not something you control. You cannot create an automatic variable, and then decide halfway through the function to deallocate it, because you do not decide on the lifetime. It is set by the rules of C++.

Dynamic memory is what you explicitly request and manage yourself during the runtime of your program. The easiest way to achieve this is to use the new operator. You always manage the lifetime of memory you dynamically request, so you are responsible for ensuring that you free up this memory when you are done. If you call new to allocate, you must call delete at some point to deallocate.

Understand what delete is. It is not delete as in remove. It is not delete as in you don't care about a variable anymore. It is an operator that indicates that memory, that you allocated with new, needs to be freed.

> am i misusing the "delete" command?

Based on what I just said, are you?

> nd if so, how do i delete normal variables?

You don't. You neither understood delete nor the notion of lifetime and what you control.
Was This Post Helpful? 4
  • +
  • -

#3 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: Am i misusing "Delete" for garbage collection?

Posted 22 November 2011 - 07:44 PM

Thank you very much for clearing that up for me :balloon: though, your condescension is unnecessary, beggars cant be choosers :). You are correct, i did not understand either of those things, however i AM a high school student. Thank's for keeping me on the straight and narrow :) its a big relief that i don't have to worry about garbage collection with Static and Automaticically allocated variables

This post has been edited by Ty Meador: 22 November 2011 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Am i misusing "Delete" for garbage collection?

Posted 22 November 2011 - 08:16 PM

> your condescension is unnecessary

If I were to be condescending, I wouldn't bother responding at all. I am not going to waste free time on properly responding to people who I think are not worth it.

> however i AM a high school student

Irrelevant.
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Am i misusing "Delete" for garbage collection?

Posted 23 November 2011 - 06:45 AM

View PostOler1s, on 22 November 2011 - 10:16 PM, said:

If I were to be condescending, I wouldn't bother responding at all. I am not going to waste free time on properly responding to people who I think are not worth it.

Actually, no. To be condescending, you have to display your feeling of superiority. If you didn't respond at all, how would anyone know how superior you feel?
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1