?
Solved

Problem deallocating memory for pointer

Posted on 2003-02-20
4
Medium Priority
?
237 Views
Last Modified: 2010-04-17
I've written this really simple win32 console program in Visual C++ to take in a name and print it out on the screen but when I run it, though it works, it exits with a debug error and I can't figure out why - can anyone help?

The program is :


#include <iostream>

int main(void)
{
  char *name = new char;

  std::cout << "please enter your name/n ";
  std::cin >> name;
  std::cout << "hello " << name;

  delete name;
  return 0;
}

 
0
Comment
Question by:TomG_london
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Rimvis earned 150 total points
ID: 7986461
The new keyword is used as an operator for creating a new object or array.

Specify size of array for input buffer:

#include <iostream>

int main(void)
{
 //Set suitable size to your array
 char *name = new char[100];

 std::cout << "please enter your name/n ";
 std::cin >> name;
 std::cout << "hello " << name;

 //Delete space allocated
 delete[] name;
 return 0;
}
0
 

Expert Comment

by:nanden
ID: 7986466
you are allocating a char pointer and then deleting it is causing memory corruption since the size you are trying to delete is greater than the one allocated.
use char *name = new char[SOMESIZE]; where SOMESIZE is less than what you anticipate the user to enter for the name. Else, best you use CStrings.
HTH<
0
 

Author Comment

by:TomG_london
ID: 7986503
Thanks both, I will accept both your posts as answers but I have a further question, what is the advantage of dynamically allocating a pointer to an array over just declaring a an array of a certain size as in char[10] name?

The reason I was using the pointer was so there was no restriction on the length of name, is using the CStrings class the only way to get around this?

Thanks

Tom
0
 

Expert Comment

by:nanden
ID: 7991917
what you are actually doing when you are getting a pointer and allocating data to it is getting memory on the heap. You got to know how much memory to deallocate when you are deleting and hence the "SOMESIZE" array that i was talking abt. What CStrings do is that they maintain the array internally and decide the size of the array depending on how much data the user wants it to hold. you can do the same using your pointer, then check the user input and allocate the same amount of memory to your pointer as the user inputted data needs. then it would be very similar to the way CStrings allocate memory. (except for the vast number of member funx that CStrings provide)
HTH.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Simple Linear Regression
Introduction to Processes
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question