Hi,
This is a problem I've been scratching my head with for a week or so now. I have a few options now but I'm curious if this is best practice. I'd like to get into programming as a career so I'd like to get into good habits and therefore use code as part of a portfolio.
I'm receiving string input from the command line. Multiple words. So I'm using cin.getline();. My target is to have it as memory dynamically allocated on the heap to ensure I'm not wasting memory.
Therefore, for my input, I'm using a buffer variable (char []) of a certain length and will restrict my input size to this. I'm copying across this string to newly created space on the heap. My concern is, where is it best to do this; the constructor for the object, or the function that is initializing the object?
So far, my strategy is to do this in the function that initializes the object and pass a pointer to the object. In effect the object steals the target storage, so to speak, by receiving the pointer and cleans up the memory in the destructor. While the calling function assumes this and does not delete but resets the pointer.
Is this wise to create and delete memory in two separate parts of the code if they are aware of what the other is doing or would you avoid it? If so is there a better way to do this? I had trouble passing arrays and I am reluctant to pass an array as I'd have to pass a separate array size variable and I'm efficiency paranoid!
Anyway, I haven't quite strung the code together, but this is how I reasoned it in my head by doing each part separate. If anyone can support or advise against this method, I'd appreciate it. Also any advice on how they do it would be lovely.
Thanks in advance,
Luke
P.s. Wall of text? Anyone request a wall of text?
String Storage in Objects
Started by LukeyJ, Nov 10 2008 02:28 PM
8 replies to this topic
#1
Posted 10 November 2008 - 02:28 PM
|
|
|
#2
Posted 10 November 2008 - 02:40 PM
Any reason you're not using std::string?
#3
Posted 10 November 2008 - 02:54 PM
Errr... I like to reinvent the wheel?
I'm guessing it is a nice efficient string template with the functionality of PHP strings? :o I'll look them up tomorrow...
I'm guessing it is a nice efficient string template with the functionality of PHP strings? :o I'll look them up tomorrow...
#4
Posted 11 November 2008 - 08:43 AM
string is flexible on length, manages its own memory, and has FAR lower risk of buffer overruns if you don't manage your indexes just right.
#5
Posted 11 November 2008 - 03:24 PM
Thanks guys.
I have another question.
I thought I'd put it here as I'd rather not start a new thread.
What is the best way to store a struct in an object. Would you do so via the use of a pointer or is it straightforward to include them in objects. If this is the situation. How do you initiate these with values?
I have another question.
I thought I'd put it here as I'd rather not start a new thread.
What is the best way to store a struct in an object. Would you do so via the use of a pointer or is it straightforward to include them in objects. If this is the situation. How do you initiate these with values?
#6
Posted 12 November 2008 - 07:13 AM
It depends on the nature of the struct. For a linked list node, I would use a pointer to the first node. For pure data, I would just include it as an object.
#7
Posted 12 November 2008 - 07:28 AM
I'm not sure I completely understand. It only has two values, a char and a int. I had some problems including it in an initialization list. I'm not too sure how...
Is that right?
struct food {
char moo;
int waa;
} cheese;
: cheese.moo(a), cheese.waa(1)
Is that right?
#8
Posted 12 November 2008 - 09:04 AM
struct food
{
char moo;
int waa;
[COLOR="Blue"] food(char moo_ = 'A', int waa_ = 0)
: moo(moo_)
, waa(waa_)
{
}
[/COLOR]};
class T
{
food cheese;
int count;
public:
[COLOR="Blue"] T(char moo_ = 'A', int waa_ = 0, int count_ = 0)
: [B]cheese(moo_,waa_)[/B]
, count(count_)
{
}
[/COLOR]};
#9
Posted 12 November 2008 - 09:24 AM
Ah, I wasn't aware structs could have constructors.
Thanks mate. A great help. It seems I can't rep either of you. That's hardly fair.
Thanks mate. A great help. It seems I can't rep either of you. That's hardly fair.


Sign In
Create Account


Back to top









