Howto design my method?

G

g.vukoman

Hi all,

I asking me how to design my method. Should I use pointer only? Or
references? Or Values? I don't know...

Below is a small method to read the mem amount of a machine. Fell free
to edit the src below

#include "MemSysInfo.h"

namespace SysInfo {

unsigned int * SysInfo::MemSysInfo_t::GetSize() const { // SHOULD I
RETURN A POINTER/REFERENCE/VALUE?

using namespace std;
using namespace boost;

ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
string* inLine = new string; // I
const string* const outLine = new string("\\2"); // USE
const regex* const rgx = new regex("^(MemTotal: )(\\d+)
( kB)?"); // POINTER ONLY?
unsigned int* memSize = new unsigned int(0); // WHAT WOULD
stringstream* stringToInt = new stringstream; // YOU USE?

if(!inFile->is_open())
throw "can_not_open_proc_meminfo"; // SHOULD I DELETE POINTERS
BEFORE THROW?
else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
memSize == 0 || stringToInt == 0)
throw "no_memory_available"; // SHOULD I DELETE POINTERS BEFORE
THROW?


while(getline(*inFile, *inLine))
if(regex_match(*inLine, *rgx)) {
*stringToInt << regex_replace(*inLine, *rgx, *outLine);
*stringToInt >> *memSize;
break;
}

inFile->close();

delete inFile;
delete inLine;
delete outLine;
delete rgx;
delete stringToInt;

return(memSize);
}

} // namespace SysInfo

Goran
 
B

Barry

Hi all,

I asking me how to design my method. Should I use pointer only? Or
references? Or Values? I don't know...

Below is a small method to read the mem amount of a machine. Fell free
to edit the src below

#include "MemSysInfo.h"

namespace SysInfo {

unsigned int * SysInfo::MemSysInfo_t::GetSize() const { // SHOULD I
RETURN A POINTER/REFERENCE/VALUE?

using namespace std;
using namespace boost;

ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
string* inLine = new string; // I
const string* const outLine = new string("\\2"); // USE
const regex* const rgx = new regex("^(MemTotal: )(\\d+)
( kB)?"); // POINTER ONLY?
unsigned int* memSize = new unsigned int(0); // WHAT WOULD
stringstream* stringToInt = new stringstream; // YOU USE?

if(!inFile->is_open())
throw "can_not_open_proc_meminfo"; // SHOULD I DELETE POINTERS
BEFORE THROW?
else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
memSize == 0 || stringToInt == 0)
throw "no_memory_available"; // SHOULD I DELETE POINTERS BEFORE
THROW?


while(getline(*inFile, *inLine))
if(regex_match(*inLine, *rgx)) {
*stringToInt << regex_replace(*inLine, *rgx, *outLine);
*stringToInt >> *memSize;
break;
}

inFile->close();

delete inFile;
delete inLine;
delete outLine;
delete rgx;
delete stringToInt;

return(memSize);
}

} // namespace SysInfo

Goran

I would say use all value in your case, have you try?
 
G

g.vukoman

I would say use all value in your case, have you try?

Yes, I tried it all. By value, by reference and by pointer. All
works... But why should I use values? When do I use values, pointers
or references? Are there rules I understand?

Goran
 
T

tragomaskhalos

Hi all,

I asking me how to design my method. Should I use pointer only? Or
references? Or Values? I don't know...

Below is a small method to read the mem amount of a machine. Fell free
to edit the src below

#include "MemSysInfo.h"

namespace SysInfo {

unsigned int * SysInfo::MemSysInfo_t::GetSize() const { // SHOULD I
RETURN A POINTER/REFERENCE/VALUE?

using namespace std;
using namespace boost;

ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
string* inLine = new string; // I
const string* const outLine = new string("\\2"); // USE
const regex* const rgx = new regex("^(MemTotal: )(\\d+)
( kB)?"); // POINTER ONLY?
unsigned int* memSize = new unsigned int(0); // WHAT WOULD
stringstream* stringToInt = new stringstream; // YOU USE?

if(!inFile->is_open())
throw "can_not_open_proc_meminfo"; // SHOULD I DELETE POINTERS
BEFORE THROW?
else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
memSize == 0 || stringToInt == 0)
throw "no_memory_available"; // SHOULD I DELETE POINTERS BEFORE
THROW?

while(getline(*inFile, *inLine))
if(regex_match(*inLine, *rgx)) {
*stringToInt << regex_replace(*inLine, *rgx, *outLine);
*stringToInt >> *memSize;
break;
}

inFile->close();

delete inFile;
delete inLine;
delete outLine;
delete rgx;
delete stringToInt;

return(memSize);

}
} // namespace SysInfo

Goran

Wow, whatever gave you the idea to do all that
dynamic allocation ?!

In your example, use value for everything,
including the return type.

People coming from a Java background tend to
dynamically allocate objects inappropriately,
but you've even done it for unsigned int.
If you're using someone else's code as
inspiration for this coding style, burn
it and forget you ever saw it. Seriously.
 
B

Barry

Yes, I tried it all. By value, by reference and by pointer. All
works... But why should I use values? When do I use values, pointers
or references? Are there rules I understand?

Goran

I think this is a long story. You better read a textbook to have a more
comprehensive understanding
 
G

g.vukoman

Wow, whatever gave you the idea to do all that
dynamic allocation ?!

In your example, use value for everything,
including the return type.

People coming from a Java background tend to
dynamically allocate objects inappropriately,
but you've even done it for unsigned int.
If you're using someone else's code as
inspiration for this coding style, burn
it and forget you ever saw it. Seriously.

The method was just a theoretical example by me :) I just need a
feeling for C++ I still miss. Why should I use stack? Is it faster?
It's easier but this is not the point. Will my program crash if I use
to much stack? E.g. by usage of more than 640KB? Why exists references
as pointers already exists? Questions, questions, questions... :)

Thanks

Goran
 
T

tragomaskhalos

The method was just a theoretical example by me :) I just need a
feeling for C++ I still miss. Why should I use stack? Is it faster?
It's easier but this is not the point. Will my program crash if I use
to much stack? E.g. by usage of more than 640KB? Why exists references
as pointers already exists? Questions, questions, questions... :)

Thanks

- Short answer: I wouldn't worry about running out of stack on
modern architectures (unless you are doing some sort of embedded
work). If you have a large multidimensional array on the stack
you could run into problems I suppose, but this is best managed
via a vector anyway, which will by default manage its memory
dynamically behind the scenes.

- References solve some problems that are awkward or ugly with
pointers, but often the choice of which to use is as much based
on idiom as technical consideration.

- Pointers are useful for hanging on to objects which need
arbitrary lifetimes and that are too expensive to copy (or should
not be copied for semantic reasons).

But as Barry sagely points out, this is really too broad a
subject for a pat answer in an n.g.. Good C++ textbooks will
not only explain this stuff far better than I can but will have
copious examples of good idiomatic code which will give you
a feel for the right way to do things.
 
G

g.vukoman

- Short answer: I wouldn't worry about running out of stack on
modern architectures (unless you are doing some sort of embedded
work). If you have a large multidimensional array on the stack
you could run into problems I suppose, but this is best managed
via a vector anyway, which will by default manage its memory
dynamically behind the scenes.

- References solve some problems that are awkward or ugly with
pointers, but often the choice of which to use is as much based
on idiom as technical consideration.

- Pointers are useful for hanging on to objects which need
arbitrary lifetimes and that are too expensive to copy (or should
not be copied for semantic reasons).

But as Barry sagely points out, this is really too broad a
subject for a pat answer in an n.g.. Good C++ textbooks will
not only explain this stuff far better than I can but will have
copious examples of good idiomatic code which will give you
a feel for the right way to do things.

Thanks to all! Looking out for good C++ books :)

Goran
 
J

Jerry Coffin

[ ... ]
The method was just a theoretical example by me :) I just need a
feeling for C++ I still miss. Why should I use stack? Is it faster?
It's easier but this is not the point. Will my program crash if I use
to much stack? E.g. by usage of more than 640KB? Why exists references
as pointers already exists? Questions, questions, questions... :)

You should use the stack because it keeps your code simple, exception
safe. As a bonus, allocating stack space is frequently faster than
allocating from the free store.

Depending on your system, the stack may easily have a limit, but you can
usually increase it if needed (though needing to do so is quite rare).

References are different from pointers, and provide some capabilities
that would be difficult to simulate with pointers. An obvious instance
is when you're doing operator overloading:

x >> y;

writes to y, but you don't want to have to explicitly take its address,
which would be necessary if you were using a pointer instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,370
Members
48,062
Latest member
leehaan

Latest Threads

Top