Help with pointers and Dynamic memory allocations

Posted 02 July 2010 - 08:22 AM

Hi, I tried writting a program that is supposed to behave like the "string" class using pointers and dynamic memory allocations. It compiles but on run time it either stops working or i get wrong results. And if I try debugging, at some point it pops up an error. This is part of the code with the fault:

Replies To: Help with pointers and Dynamic memory allocations

Re: Help with pointers and Dynamic memory allocations

Posted 02 July 2010 - 09:01 AM

What's the exact error/message?

Some thoughts after reading your code:

1. Pick a naming convention and stick with it. I prefer class names that start with capitals. The name after the ending bracket of the class isn't really used for C++ classes, structs yes. Not illegal, but not really something people do, there's no need, you just use the class name.
2. Memory allocation should happen in the constructor, currently it only sets the pointer to NULL, not really helpful. There's no guarantee the user will call the next function that actually allocates, speaking of which...
3. Your allocation allocates space for exactly one character. That's not a string. It explains why you're segfaulting all over the place. 4. What is 'strings'? You declare and initialize three of them to NULL in main, where is that class definition? Typo? Didn't notice the typedef.
5. Passing around pointer references is indicative of poor design. While they are useful, you have them everywhere. The outside world should not have direct access to your "internals" (aside from traditional accessors/mutators as needed).

Re: Help with pointers and Dynamic memory allocations

Posted 02 July 2010 - 09:25 AM

KYA, on 02 July 2010 - 08:01 AM, said:

What's the exact error/message?

Some thoughts after reading your code:

1. Pick a naming convention and stick with it. I prefer class names that start with capitals. The name after the ending bracket of the class isn't really used for C++ classes, structs yes. Not illegal, but not really something people do, there's no need, you just use the class name.
2. Memory allocation should happen in the constructor, currently it only sets the pointer to NULL, not really helpful. There's no guarantee the user will call the next function that actually allocates, speaking of which...
3. Your allocation allocates space for exactly one character. That's not a string. It explains why you're segfaulting all over the place.
4. What is 'strings'? You declare and initialize three of them to NULL in main, where is that class definition? Typo?
5. Passing around pointer references is indicative of poor design. While they are useful, you have them everywhere. The outside world should not have direct access to your "internals" (aside from traditional accessors/mutators as needed).

Memory gets allocated when the "getline" function is called but as for allocating memory for just one character - the program expands it to accept more characters. I don't intend to use the "string" class because that's what i'm trying to reproduce. "char*" was typedefed to "strings". Finally, passing the pointers as arguments was the only possible way i could access the variables from the main - or at least, to the extent of my scope of c++.

the C++ string stores the data, and provides an interface to access and modify that data; most importantly, there are no char* or char[] variables floating around in the code which uses string, because that data is encapsulated - in other words, the way in which the C++ string type is designed does not expose its internals to the outside world.

These issues are nothing whatsoever to do with pointers or arrays - they are issues of data abstraction (i.e. writing a user-friendly class - this is the foundation of Object orientation); If you aren't familiar with these concepts, then writing a class using pointers/arrays is a step too far for you at the moment. Get to grips with classes first, and worry about pointers later; if learning classes is equivalent to learning how to switch on a car engine, then learning dynamic memory management is like learning to reverse around a corner.

One thing you could try is writing your class initially using the C++ string type (remember not to let the underlying type become exposed to the outside world). Afterwards, substituting char* for string should be a little easier.

Re: Help with pointers and Dynamic memory allocations

Posted 04 July 2010 - 12:03 PM

I always find it easier to use C++'s normal keywords instead of adding typedefs all over the place. I'l give you something to get you started but I can't access a compiler at the moment so you will probably have to fix a few errors.

I stick to prefixing _ for my member variables, I try to stick to using the same things all the time.