Jump to content
  • Advertisement
Sign in to follow this  
numegil

Allocating memory for dynamic char array

This topic is 3160 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, So I have an appointment class, which contains in the header file: class appointment { public: appointment(); appointment(const appointment& orig); virtual ~appointment(); time startTime; time endTime; char *subject; // this is the relevant part char *location; void print() const; void read(); private: }; in the .cpp implementation file, I have the following function: void appointment::read() { char *ptr; ptr = strtok(NULL, ","); subject = new char[80]; // this line wasn't here originally. Now crashes here. strcpy(subject, ptr); // originally crashed here. startTime.read(); endTime.read(); ptr = strtok(NULL, ","); //strcpy(location, ptr); location = "location"; } // readAppointment() In short, I am trying to copy the value of ptr into the subject instance variable. According to my debugger (using NetBeans), it "cannot access memory at address 0x14". I added a line above the strcpy() in an attempt to allocate the memory, but now it instead crashes on that line with the same error. I know that ptr is storing the value correctly, a cout just before the part where it crashes successfully prints the value. I know I'm probably just allocating the memory wrongly here, but I"m not sure how to do it correctly, and Google isn't helping much. Any help would be greatly appreciated! -Numegil

Share this post


Link to post
Share on other sites
Advertisement
You really, really shouldn't be using C strings in C++. I'll leave this lecture to one of the usuals (Zahlman, get in here), but anyway..

It sounds like your this pointer when your application crashes is null. Check to see if that's the case?

Share this post


Link to post
Share on other sites
Why are passing NULL as the first argument to strtok? Why are you trying to parse NULL for tokens?

see http://www.cppreference.com/wiki/c/string/strtok

Share this post


Link to post
Share on other sites
Quote:
Original post by NumberXaero
Why are passing NULL as the first argument to strtok? Why are you trying to parse NULL for tokens?


Because that's how it works :) EDIT: Or check out the page you just linked.

Share this post


Link to post
Share on other sites
Quote:
Original post by mattd
You really, really shouldn't be using C strings in C++. I'll leave this lecture to one of the usuals (Zahlman, get in here), but anyway..

It sounds like your this pointer when your application crashes is null. Check to see if that's the case?


I'm required to use C strings as part of the assignment for some reason. In any case, you were right, the "this" was null. Debugging that now... Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by NumberXaero
Why are passing NULL as the first argument to strtok? Why are you trying to parse NULL for tokens?
While that link says to pass NULL it in fact doesn't give an explanation for why. The reason why is because strtok is a pretty strange function that stores information you pass into it using a local static variable. If you pass in NULL it reuses the stored information. Otherwise it updates the stored information. It probably does something like, well... see here to learn how strtok is implemented.

Share this post


Link to post
Share on other sites
Quote:
Original post by mattd
Quote:
Original post by NumberXaero
Why are passing NULL as the first argument to strtok? Why are you trying to parse NULL for tokens?


Because that's how it works :) EDIT: Or check out the page you just linked.
Except the first time you call it, you have to pass in the string, and I can't see where he's doing that...

Obviously, there's still the appointment class being NULL, but I don't think that's the only problem here.

Share this post


Link to post
Share on other sites
From the link mattd gave

Quote:
The first call in the sequence has s1 as its first argument, and is followed by calls with a null pointer as their first argument.


Quote:
The first call in the sequence searches the string pointed to by s1


Its a strange call but it doesnt read minds, you need to give it something atleast once.

Share this post


Link to post
Share on other sites
NumberXaero, Codeka: true. I was assuming he'd called it somewhere outside of appointment::read beforehand (yes, I know that's a horrible idea).

Share this post


Link to post
Share on other sites
Quote:
Original post by mattd
NumberXaero, Codeka: true. I was assuming he'd called it somewhere outside of appointment::read beforehand (yes, I know that's a horrible idea).


Yes, that's exactly what I'm doing. For this assignment, we are supposed to convert C code to object-oriented C++ code. The original code was provided by the instructor. I'm trying to minimalize my work effort by just placing as much of the code that he had in the C version into the appropriate new class functions as possible.

(Problem solved!)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!