5 Replies - 848 Views - Last Post: 10 January 2010 - 03:22 AM Rate Topic: -----

#1 maybnxtseasn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 04-April 09

where is my memory leak at

Posted 09 January 2010 - 10:14 PM

so......where is my memory leak at?

this program has user enter a word...the words characters r counted.....then the word is put backwards into a different array...then each character is compared from the beginning of the reversed array and normal array....returns true if a palinDrome...false if not....

example of this is racecar is spelled the same backwards!

// testPrograms.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


bool palinDrome(char *orgWord)
{
	int count = 0;
	int z = 0;
	int loop = 1;
	bool result = true;

	for (;orgWord[count] != '\0';count++)					 //count number of characters in the user entered word
	{
	}
	int i = count;
	char *rWord = new char[count];
	do													   //loop to reverse word into a different array
	{
		rWord[i--] = orgWord[z++];
	} while (i != 0);

	for (int x = 0; x<count;x++)
	{
		if (rWord[x] != orgWord[x])
		{
			delete []rWord;
			return false;
		}
		
	}
	delete []rWord;
	return true;
}

int main()
{
	char *word = '\0';
	cout << "Palinedrome checker" << endl;
	cout << "Please enter the word to be check" << endl;
	cin >> (word = new char[]);

	bool result = palinDrome(word);

	if (result == true)
	cout << "The word entered is a palindrome" << endl;
	else
	cout << "That word is not a palindrome =[" << endl;

	delete []word;
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: where is my memory leak at

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5307
  • View blog
  • Posts: 27,200
  • Joined: 10-May 07

Re: where is my memory leak at

Posted 09 January 2010 - 10:19 PM

** Renamed title to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

#3 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: where is my memory leak at

Posted 09 January 2010 - 10:40 PM

Hello maybnxtseasn im Kohana nice to meet you.

First of all your code is very messy. Declaring variables for use in for a loop that doesn't do anything, you should never use a global variable inside a for loop. Its bad practice.

Also from what i can see, you are passing a pointer too the bool function, this pointer is a pointer to a char, not an array. So in your bool code when you try to add to it, it doesn't understand why you have the [] after it.
	char *rWord = new char[count];
	do													   //loop to reverse word into a different array
	{
		rWord[i--] = orgWord[z++];
	} while (i != 0);



You see? orgWord[]; is simply wrong. There is also no {} to accompany the "while (i != 0).

Please revise
Was This Post Helpful? 0
  • +
  • -

#4 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: where is my memory leak at

Posted 09 January 2010 - 10:50 PM

Your first problem is in this line
rWord[i--] = orgWord[z++];

i is set equal to count, but count is the number of elements in the array. So when you access the array starting at count you actually need to take one off it (arrays start at 0 not 1). So use this postfix decrectment operator - rWord[--i], to take one off i before accessing the array.

Your second problem seems to be here(I not totally sure about this, never seen this method being used) , cin >> (word = new char[]);
What is this actually doing(? i don't know), what number of chars are actually being allocated my guess would be 0. So I would give word a fixed size.

Hope this helps I could be wrong about the second part.
Was This Post Helpful? 0
  • +
  • -

#5 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: where is my memory leak at

Posted 09 January 2010 - 11:01 PM

View PostKohana, on 9 Jan, 2010 - 09:40 PM, said:

Hello maybnxtseasn im Kohana nice to meet you.

First of all your code is very messy. Declaring variables for use in for a loop that doesn't do anything, you should never use a global variable inside a for loop. Its bad practice.

Also from what i can see, you are passing a pointer too the bool function, this pointer is a pointer to a char, not an array. So in your bool code when you try to add to it, it doesn't understand why you have the [] after it.
	char *rWord = new char[count];
	do													   //loop to reverse word into a different array
	{
		rWord[i--] = orgWord[z++];
	} while (i != 0);



You see? orgWord[]; is simply wrong. There is also no {} to accompany the "while (i != 0).

Please revise

Sorry Kohana,

But the calling methods are fine, yes the function returns a bool, but is passed a char * (therefore a string).
Whats wrong with orgWord[]? This is how you can access a pointer
There are no {} accompanying the while loop because its a do{....} while(...); loop.

Take a little bit more time to read fully before posting.
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: where is my memory leak at

Posted 10 January 2010 - 03:22 AM

You're writing C++ code so you should not be using char[] to represent plain text data; instead, change your code to use the string type; You won't need any pointers or calls to new/delete (and nor should you either)

#include <string> 
    string word;
    cout << "Palinedrome checker" << endl;
    cout << "Please enter the word to be check" << endl;
    cin >> word; 

bool palinDrome(const string& orgWord)
{
    int z = 0;
    bool result = true;

    int i = orgWord.length()-1;
    string rWord;
    do 
    {
        rWord += orgWord[i--];
    } while (i != 0);

    for (int x = 0; x<rWord.length(); x++)
        if (rWord[x] != orgWord[x])
            return false;
    return true;
} 
You should notice a few things;

(1) a string starts out empty and resizes itself automatically when you append/assign data
(2) the += operator will concatenate text to the end of a string
(3) a string knows its own size and has a .length() function
(4) as with char arrays, its last character is at length() - 1
(5) also as with char arrays, you can access its elements with [ ]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1