Jump to content

String Storage in Objects

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
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?

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Any reason you're not using std::string?

#3
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
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...

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
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?

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
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...

struct food {
char moo;
int waa;
} cheese;

: cheese.moo(a), cheese.waa(1) 

Is that right?

#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
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
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
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.