Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

some questions about C++

Status
Not open for further replies.

mImoto

Full Member level 4
Joined
Feb 21, 2002
Messages
210
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,733
C++ questions?

Hello,

I have developed a Visual C++ 6.0 program. I would like to ask you some questions. I am learning Visual C++ and I am quite newbie.

In my program I read lines from a file. I use static memory allocation.
Cline is a class created by me.

CLine ArrayData[1000]; // Here 1000 is the nunber of lines of the file
// I don't know the nunber but I expect to be less than 1000.
char Data[1000][16];
// Here the 1000 is for the same reason but the 16 is fixed


I know that maybe this is not the optimun solution (I reserve probably more memory than I use) and it seems more C than C++.
I was wondering in using instead dynamic memory management but I don't know how to do it. I have also thought in using the "vector" class but I think is only for unidimensional arrays¿?.

The question is ¿How you experts do these?. It is better to use dynamic memory?. And is it possible to declare a vector of my Class CLinea or the types in vector are only int, etc?.

Thanks a lot and sorry for hitting you with so many questions,

Best regards,

mimoto
 

Re: C++ questions?

Hi miMoto,

There are many ways to do this, but I think the best one is using the Standard Library (included in any C++ distribution)

first of all, you include the vctor library

#include <vector> //without .h 8O

//declare your types
std::vector<Cline> ArrayData;

to insert your data at the end of your vector use the push_back function, ex.
ArrayData.push_back( ... );

to access your data use the [] operator, like common c arrays.

to use multidimension arrays declare types.

std::vector<char> ch_vector;
std::vector<ch_vector> Data;

if have any doubt look at https://www.sgi.com/tech/stl/, or send me a message.

best regards,

mgs_p
 


Hi

I am ot expert in C++ but dynamic memory allocation is done through 'new' operator
and released thorugh 'delete' .
on the right side of new there will be class .
From other side you can use STL container library as mgs_p has adviced or you can write your own library . But ntoe that dynamic memory allocation in C++
is more time consuming than malloc in C .
 

Re: C++ questions?

Hello MImoto,

You should consider if your thoughts are more for basic sakes or determine to hit a appropriate solution in terms of a C++ program.

Do you prefer the last way my advice is to use the Microsoft Foundation Classes provided also by your VC++ package and should be installed on your Hard Disc yet.
I have to admitt the understanding isn't done within hours but spend some time to learn more about it and you will find out, your problem is solved yet.
You don't have to develop a new class on your own.
In the very beginning type only CArray somewhere inside your editor field have a click on it so the cursor rests there and then press key <F1>.
The Help System will be called.
Select CArray inside the listfield pops up and you'll see a finished class covering all your wishes in terms to administrate your data. Use the class members belongs to and find out what they do and which are the right ones for your data management.

good luck!

Robby
 

Re: C++ questions?

Thanks for the answers,

I will learn more about the MFCs. They seem what I am looking for. And last question.

If I use the vector container instead of:
CLine ArrayData[1000]; // Here 1000 is the nunber of lines of the file

vector<Cline> v(100);

What happen if I need after more than 100 elements. should I delete the heap memory allocated for v(100) and allocate more memory copying the previous array in it or this is done automatically.

Best regards,

Javi
 

Re: C++ questions?

MFC is good if you want to limit your knowledge to windows programs.
But be carefull with MFC!!! They seem to be easy to use but are hard to master!!!

If you want to write more general programs you could use the STL (Standard Template Library) as mentionned above. It might be a littlebit harder to start with them (especially the iterator part might be strange at first) but they are very powerfull once you know how to use them!

Or if you realy want complete flexibility you could try to write your own container class and overload the operators you want ([], +, +=, ==, ...)
(You should read a good C++ book before you start doing this ;-) )


The suggestion I give about Visual C++:
Try learning C++ first!!!
Visual C++ is dangerous and allot of ppl loose themselves in writing gui's and putting the algorithms, data, ... in the gui's (BAD!!!). They never seem to get to thinking about classes, inheritance, overloading, ... .
Try starting with the functionality (in a terminal application for example) and DON't start with putting the buttons on the dialog box and calling the classwizard to add a method to the button.

Antharax
 

Re: C++ questions?

Regarding your question:
You're using the STL and not the MFC by using vector (which is good!!!)
(There doesn't seem to be a CVector class :-D )
And you don't have to care about the size!!! When you add something to the vector it will automatically increase size when needed.
(you could instantiate it like this: vector<Cline > v)

Just add what you want, read, remove elements, ...

Antharax
 

Re: C++ questions?

Hi Javi !,
I think you can't do this: vector<Cline> v(100);
And you must write this instead: vector<Cline> v;
so if you call cout<< v.max_size( ); you can see the maximum possible length of the vector which you can allocate.
and when you call v.push_back( clineVar ); or v.insert(clineVar, 3); indeed the vector class allocates memory for you class so don't worry about it.
if you want to know what is the number of elements in your vector use this: cout<< v.size( );
also if you want to know what is the number of elements in the memory of your vector : cout<< v.capacity( );
this function return real number of storage that contains free memory in your vector of cource if you add some element more than this number, the vector class automatically will allocate more memory for your vector.
but it's better if you use v.resize( 100 ); (it's more efficient) if you have enough memory.
Best Regards
:wink:
 

Re: C++ questions?

Hello Antharax,

You are absolutely right, knowledge the MFC isn't done 'by the way' but after about 2 years you get a good feeling what happens inside this sophisticate system. The situation is running complicate because of the fact not all API features are wraped inside the MFC and so the knowledge of the complete Win Application Interface is signed on. To forget this lead to a dead end street.
Sometimes announced statements, using the MFC by stand allone are more a subject to think twice about. At least in my opinion.

What the MFC does is to provide a skelet with such features like Doc/View-Design, a divertion between data and view and a must for powerfull program design. To invent this by yourself would take a lot of time better to use for the main things.

greetings

Robby
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top