Hey guys, I was just wondering when using classes to obtain a name or something like that when a pointer is used how come you don't have to use the new and delete key words.

class LibBook

{
    private:
       char *name
    public:
       LibBook( char* = '\0')
       void showBookName()

};

Thanks in advance.

Dani AI

Generated

Short answer: the Date example does not call for new/delete because day and month are fixed-size member arrays that are allocated as part of the object itself, not separately on the heap. As noted, only when memory is obtained dynamically (a pointer that must be made to point at separately allocated storage) is new/delete required. 's comment about not mixing character literals and pointer/null initialization was also on target.

A few practical pitfalls not yet discussed in the thread: returning a pointer to a local automatic buffer inside a function produces a dangling pointer; returning a pointer to a member array is safe only while the object still exists. Classes that hold raw char* owning memory must implement correct copy/destruction semantics (the "rule of three/five") to avoid double-free or leaks; fixed-size member arrays get copied by the compiler’s member-wise copy, but that does not prevent overflow or missing null terminators when copying strings into them.

Safer modern approach: prefer std::string for names and dates. std::string manages allocation and copying automatically, so no manual new/delete is needed. Example pattern:

class LibBook {
  std::string name;
public:
  LibBook(const std::string& n = {}) : name(n) {}
  const std::string& getName() const { return name; }
};

If staying with C strings, ensure explicit null-termination when using functions like strncpy, or use safer helpers such as strlcpy where available. For pointer initialization use nullptr in modern C++, or 0/NULL pre-C++11; avoid initializing pointers with character constants like '\0' to prevent confusion. For reference material see the std::string documentation (cppreference) and the strncpy notes (cppreference).

Recommended Answers

All 8 Replies

Hey guys, I was just wondering when using classes to obtain a name or something like that when a pointer is used how come you don't have to use the new and delete key words.

Generally you do. Do you have a more specific example? The one you posted is not really correct. The '\0' should be 0.

I found another example:

class Date
{
 private:
	 char day[10];
	 char month[10];
	 int year;
  
public:
	

	void set(char* day, char* month, int year);
  
        void display(Date datem);
  
        char* get_day();

	char* get_month();

	int get_year();

 };

void Date::set(char* d, char* m, int y)
{

	strncpy(day, d, 9);
	strncpy(month, m, 9);
	year=y;

}

  
void Date::display(Date date)
{
	cout << "The day is ";
	cout << date.get_day();
	
	cout << "\nThe month is ";
	cout << date.get_month();
	
	cout << "\nThe year is ";
	cout << date.get_year();
	cout << endl;
}
  
char* Date::get_day()
{
	return day;
}

char* Date::get_month()
{
	return month;
}

int Date::get_year()
{
	return year;
}

What is your question in regard to pointers about your last posted code? The functions that return a pointer to the beginning of a char array that is private? Or the function parameters that receive a pointer to the beginning of a string to copy?

I was just wondering with the variables such as day and month as they are pointers how come the new and delete keywords don't have to be used.

I was just wondering with the variables such as day and month as they are pointers how come the new and delete keywords don't have to be used.

The day and month variables are declared to be arrays of length 10. You only need to use new and delete[] if the arrays have not been allocated yet.

The one you posted is not really correct. The '\0' should be 0.

Hi Dave, I was under the impression that '\0' was equal to 0?
I got the output "equal" for the following program.

#include <cstdio>

int main()
{
  if ( '\0' == 0 )
    printf( "equal" );
  getchar();
}

Or is this only restricted to my compiler (mingw)?

The day and month variables are declared to be arrays of length 10. You only need to use new and delete[] if the arrays have not been allocated yet.

Thanks for your help Dave & Dante.

Hi Dave, I was under the impression that '\0' was equal to 0?

It was more of a hint not to get into the habit of writing characters to pointers and confusing NULL and '\0'.

[edit]So yes, I overstated things when I said it wasn't quite correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.