I have written 2 functions. The first works and the second does not. Please note, the calling function does not know the size of the array to be returned, so it cannot be pre-sized.
The problem is that I cannot determine how to make the second work. Thanks for any enlightenment.
// DynArray.cpp : Proto type for creating table on the fly
Have you tried passing the value by reference and just accessing it that way?
int MakeArray2(Tag& tbl)
It's just a guess, I don't have time to look at the code with seriousness at this moment, but that might be a solution for you.
Alexis Moshinsky
April 8th, 2002, 01:14 PM
Hi,
Your function is not work becouse You lost address of tbl
with exit from function. The correct signature could be:
int MakeArray2(Tag*&);
SGilcrist
April 8th, 2002, 02:03 PM
Thank you that is exactly what I needed to do.
NMTop40
April 8th, 2002, 06:09 PM
you also call new a number of times and there are no matching deletes. Note, you will need to use delete[], but in any case you should not be using these but should use vector.
Note also that you have a slight bounds overrun, as "Fred\0" will put two zero characters at the end, one that you typed in as \0 and the other as the end of string terminator. Therefore it requires 6 characters. In your current situation that will never actually be a problem because that array is immediately followed by another one of 125 characters, which you subsequently write to.
of course you should not really be using char arrays, but std::string.
(Or just write the whole thing in C).
The best things come to those who rate
SGilcrist
April 8th, 2002, 07:16 PM
Thank you. I am relatively new to C/C++, so learning the subtleties, such as the array overrun, is important and will take a while to learn.
You are also right about the delete function. In this case though it was not included, except as a note, because I did not want to confuse the issue.
Sam
Paul McKenzie
April 8th, 2002, 08:46 PM
Array bounds and new/delete are not a problem if you use C++ properly (like NMTop40 suggests -- get rid of the char arrays and use std::string).
It's good that you are learning a programming language, however you should seriously consider either learning 'C' *or* learning C++ -- there is no such language as C/C++. C and C++ are two seperate languages, and currently, you are learning 'C', because none of the code really shows any usage of C++. The problem with learning 'C' and then C++ is that you will have to unlearn many things in 'C' when you finally learn C++. For example, there should have been no reason to introduce arrays like this or use char arrays for a C++ program. For a 'C' program, you have to resort to this type of coding, but not for a C++ program. Instead, the vector<> class should have been introduced instead of raw arrays.
For example, this should have been your program from a C++ perspective:
#include <string>
#include <vector>
#include <iostream>
//...
//...
struct Tag
{
int index ;
std::string tag;
std::string value;
} ;
//...
typedef std::vector<Tag> TagArray;
//...
TagArray MakeArray1()
{
//need function to delete tables
// No you don't!!
return 0;
}
Note there are no new or delete, or any functions borrowed from 'C' to do character array manipulations. Yes, they exist in C++, but if you want to use them, you might as well code in 'C'. If you stick to learning 'C' then your original program is OK. However, if you are learning C++, I suggest that you quickly drop the type of coding your doing now and learn the proper ways to code a C++ program, before it's too late. Maybe this link can help:
http://www.parashift.com
This link contains the C++ FAQ. It goes into further detail of why learning 'C' programming when you really want to learn C++ is a waste of time and could be detrimental.
Regards,
Paul McKenzie
Graham
April 9th, 2002, 03:31 AM
I heartily concur with what NMTop40 and Paul McKenzie have said. In fact, as soon as I saw the words "the calling function does not know the size of the array to be returned", I immediately thought "std::vector". A quick check to make sure that this wasn't C (the presence of new confirms that it isn't), and vector becomes the answer. Also, if you ever find yourself writing "char somename[", then as soon as you hit that bracket, think "std::string".
I'll reiterate what Paul has said - C++ is not C, and if you start thinking in C, you won't write good C++. C++ is about OOP - Abstraction, Encapsulation, Modularity and Hierarchy, none of which C can really handle. OOP is about objects, and objects have state and behaviour - very difficult to model in C.
He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
Alexis Moshinsky
April 9th, 2002, 04:21 AM
I realy do not want to enter polemic "C aganst C++". For me this sounds like what better screwdriver or X-screwdriver :)
But, before You give tips of universe caliber, remeber that You care no responsibility for young programmer.
So, at least be careful with Your suggestions.
About from what to begin - i think studing of programming, like every other discipline should begin from bottom.
This mean Pascal, C, may be assembler. Please do not understand this, like i'm trying to say something against OO.
Object Oriented concept had prooved itself and not need to be defined anymore.
Paul McKenzie
April 9th, 2002, 08:55 AM
Graham is not saying don't learn 'C'. What he is saying is that if you want to learn C++, then learn C++, if you want to learn 'C', then learn C. It does you no good and could be harmful if you are a beginner and you want to learn 'C++', but instead 'C' is being taught, and vice-versa. If that is how a university teaches its C++ program (by teaching 'C' instead), then the university is doing the student a disservice.
I do teach at a university, and I can tell you that learning 'C' programming when you are supposed to be learning C++ is a big mistake. This has been emphasized by many, not just Graham and myself.
And who says that OOP is not "the bottom" (as you put it)? When you're a beginner, you can pick up OOP just as easily as any other beginning programming concept. Many experienced programmers don't learn C++ correctly, because they think procedurally and can't grasp the concept of OOP. A competent beginner can understand the concept of OOP more easily, because that is the first thing that they would be exposed to.
Regards,
Paul McKenzie
SGilcrist
April 9th, 2002, 10:43 AM
The debate of C v. C++ is a little erudite, but I really appreciate the code samples! I have read that programs that use CStrings (and I presume Vectors) are as slow as VB because of all of the overhead. I wrote this code as char[] because I believed, perhaps incorrectly, that this is the fastest way, short of assembly, to manipulate strings.
I would be very pleased to learn your experience.
Sam
Paul McKenzie
April 9th, 2002, 11:13 AM
Do not confuse CString (MFC/VC++) with std::string (standard string class that *all* C++ compilers support). They are different classes. One is from Microsoft and is coded only one way by Microsoft. It is also *not* a standard C++ library class. The other (std::string) is a standard C++ class, so all compilers support it. Now depending on who wrote the standard library for the particular compiler, std::string could be as slow as a turtle, or as fast as lightning. If you time operations with std::string for one compiler against std::string for another compiler, the operating time can be so different. The same thing with vector<>. You can't compare it to anything in VB, since there are so many implementations of vector<>. It all depends on who wrote the standard library for the compiler.
This is why you should also not compare VB with C++. VB is a proprietary language made by Microsoft, C++ is a language made by Bjarne Stroustrup that is used on a variety of operating systems. C++ is *not* a Microsoft language -- the only thing Microsoft has to do with C++ is that they have a compiler that compiles C++ source code called "Visual C++". So when someone says "VB and C++ blah blah", they don't know what they're talking about, since they are comparing a proprietary language to a general language. Borland C++ Builder's standard library may run rings around VC++'s standard library, maybe not. This is where comparisons should be made.
As far as std::string being slower than char[] -- you can only determine this if you time these things. This all depends on what you're doing. If you want to insert or delete characters from a string, it is more than likely that using char[] will be slower. The reason being is that a strcat() or a strcpy() is not all that's needed to do safe character insertion/deletion. You have to possibly allocate more memory for the array, etc.
But the bottom line is that std:string is proper usage of C++, as well as vector<> for dynamic arrays. You shouldn't use anything else unless there is a compelling reason to do so. Most modern C++ books introduce the standard classes way before talking about strcat() and strcpy() or using new/delete for dynamic arrays. This to me is the correct approach.
Regards,
Paul McKenzie
Alexis Moshinsky
April 9th, 2002, 02:18 PM
I do not add Graham saying "don't learn 'C'". And what is more, i'm standing on very same positions,
what concern confusion of C and C++.
Just expressions like 'think so' from experienced programmer seems a little bit command, at least with me.
Regarding from what to begin - each way has advantages and disadvantages. I feel not competent enough to discuss this.
With Respect,
Alexander Moshinsky
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.