Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 12, 2009, 06:53 PM   #1
Coolnat2004
macrumors 6502
 
Join Date: Jan 2005
Location: Northern Kentucky
Send a message via ICQ to Coolnat2004 Send a message via AIM to Coolnat2004 Send a message via MSN to Coolnat2004 Send a message via Yahoo to Coolnat2004 Send a message via Skype™ to Coolnat2004
C++ - strcpy into array

So, I'm pretty new to C++. I have a problem copying a c-style string into an empty array of c-style strings. Here is the relevant code:

Code:
bool addToList(char ** list, int * counts, char * word, int numwords) {
	bool found = false;
	(...)
	if (!found) {
		cout << "NOT";
		strcpy(list[numwords],word);
		counts[numwords] = 1;
		numwords++;
	}
	return true;
}

int main() {
	char **list;
	int *counts;
	counts = new int[100];
	list = new char*[100];
	int numwords = 0; // number of words
	(...)
	char * word;
	word = "asdf";
	addToList(list, counts, word, numwords);
	return 0;
}
The code compiles just fine, but it stops running when it hits the line with strcpy(). (If I comment that line out, it runs to the end)

I would try another method, but this is how the professor wants it to be done. Actually, he was using strcpy_s(), which is specific to Microsoft as I understand. I'm using Xcode, so that's not an option.

Are there any masters of C++ out there that can explain my error for me?
__________________
nathanbolender.com
 15" MacBook Pro 2.8GHz |  12" PowerBook G4 1.5GHz |  iPhone 3GS 16GB
Coolnat2004 is offline   0 Reply With Quote
Old Apr 12, 2009, 07:39 PM   #2
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Hi

I think your problem is that you've allocated space for pointers to your 100 strings, but you haven't actually allocated memory for the strings.

Code:
list = new char*[100];
So in the above, you've created an array of uninitialised pointers to strings. What you need to do in addToList() is allocate memory for the string before copying it.

Also, in addToList() you pass numwords by value when I imagine it would be more useful to pass it by reference (or a pointer) seeing that you increment it.

b e n
lazydog is offline   0 Reply With Quote
Old Apr 12, 2009, 11:50 PM   #3
Coolnat2004
Thread Starter
macrumors 6502
 
Join Date: Jan 2005
Location: Northern Kentucky
Send a message via ICQ to Coolnat2004 Send a message via AIM to Coolnat2004 Send a message via MSN to Coolnat2004 Send a message via Yahoo to Coolnat2004 Send a message via Skype™ to Coolnat2004
Thank you for your help! I solved the problem by doing this before copying the word:
Code:
list[numwords] = new char[strlen(word)];
__________________
nathanbolender.com
 15" MacBook Pro 2.8GHz |  12" PowerBook G4 1.5GHz |  iPhone 3GS 16GB
Coolnat2004 is offline   0 Reply With Quote
Old Apr 13, 2009, 05:00 AM   #4
Kurukuru
Guest
 
Although not the same as strcpy_s, strncpy can be used to provide you with some protection of the destination buffer, by allowing you to limit the number of characters written to it.
  0 Reply With Quote
Old Apr 13, 2009, 09:30 AM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by Coolnat2004 View Post
Thank you for your help! I solved the problem by doing this before copying the word:
Code:
list[numwords] = new char[strlen(word)];
That thing is not big enough. strlen tells you the number of bytes starting at the address given until a null is reached, non-inclusive. This means it will tell you how many characters are in a C-style string, but not how many bytes are needed to hold those characters plus the null terminator. You should add one to the result of strlen.

Also, do you know the pattern you need to follow to properly delete all of this memory you're allocating? I.e. what should be deleted first, etc.?

-Lee
lee1210 is offline   0 Reply With Quote
Old Apr 13, 2009, 11:19 AM   #6
gnasher729
In Time-Out
 
Join Date: Nov 2005
Quote:
Originally Posted by Coolnat2004 View Post
So, I'm pretty new to C++. I have a problem copying a c-style string into an empty array of c-style strings. Here is the relevant code:
Before you write any code, you should write down what data structures you will have. Because the data structures are the essential thing here; the code is just a small detail. The thread makes it look as if you are hacking your solution together without ever stepping back and thinking about what data you need to store. You can do that and succeed with a 50 line program. You will run into trouble at 500 lines, have no chance at 5,000 lines, and be utterly stumped at real world software.

So please: What will be your data structures?
gnasher729 is offline   0 Reply With Quote
Old Apr 13, 2009, 11:27 AM   #7
cube
macrumors G3
 
Join Date: May 2004
You're programming in C++, not C. Use the STL.
cube is offline   0 Reply With Quote
Old Apr 13, 2009, 11:53 AM   #8
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
I don't want to discount what gnasher729 and cube are saying here, but wanted to point out that the OP mentioned that this was for a class. The teacher/professor may have prescribed that C-style "strings" be used in a C-style array, rather than using std::vector and std::string, etc.

-Lee
lee1210 is offline   0 Reply With Quote
Old Apr 13, 2009, 01:00 PM   #9
Coolnat2004
Thread Starter
macrumors 6502
 
Join Date: Jan 2005
Location: Northern Kentucky
Send a message via ICQ to Coolnat2004 Send a message via AIM to Coolnat2004 Send a message via MSN to Coolnat2004 Send a message via Yahoo to Coolnat2004 Send a message via Skype™ to Coolnat2004
I did end up using strncpy() due to similar advice I found elsewhere (and in the book, imagine that!). I would rather use the STL, but the professor is teaching it this way (hopefully just for now).

I did add 1 to the length when copying it, because I realized that it was missing the \0. It is working beautifully. Thanks for the help and advice.
__________________
nathanbolender.com
 15" MacBook Pro 2.8GHz |  12" PowerBook G4 1.5GHz |  iPhone 3GS 16GB
Coolnat2004 is offline   0 Reply With Quote
Old Apr 13, 2009, 03:04 PM   #10
Sander
macrumors 6502
 
Join Date: Apr 2008
I see that your professor explicitly required this, but I would certainly ask why. If this is only to show you a few classes from now that this tedious and error-prone stuff is done differently in C++, I'm all for it.

If it turns out that he stopped learning C++ himself after memorizing that "malloc is now spelled new and free is now delete" then you should seriously consider finding a different educator.

I'm not kidding.
Sander is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Want RAID Array Mufasa804 Mac Peripherals 4 Sep 25, 2013 05:29 PM
Array Containing Dictionaries ahan.tm iPhone/iPad Programming 2 Nov 18, 2012 11:31 AM
NSMutable Array Help AnonymousInUse iPhone/iPad Programming 12 Oct 11, 2012 04:46 PM
RAID Array help Lord Adama Mac Pro 3 Jun 5, 2012 11:27 PM

Forum Jump

All times are GMT -5. The time now is 06:21 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC