Narue 5,707 Bad Cop Team Colleague

>So in essence, what is undefined in the Standard becomes
>defined in the context of a particular compiler.

That rather goes without saying, but doesn't change anything. The result can still be unpredictable, and it's certainly not portable.

>Doesn't it blur the difference between the two things,
>when the implementation of both, the undefined and the
>unspecified, is being handled in a fixed way by the compiler.

Not really. Unspecified means the standard offers multiple options to choose from, but the compiler must choose one of them. Undefined means the standard says "you're on your own" and the compiler can do anything.

>Is it that during the coding of the compiler, the undefined
>cases are simply not accounted for, and their result ,
>unintentionally, depends on the way a code is written

That's one possibility, yes.

Narue 5,707 Bad Cop Team Colleague

For the record, C is defined by ISO, not ANSI. Control of the language standard was taken over by ISO in 1990. The language that everyone refers to as "C89" is actually ISO C90 with the 1995 addendum. So C90 or C95 are more strictly correct (though not as well recognized). Then of course there's the ISO C99 standard, and the future C1x standard. The current official standard is C99 and the de facto standard is C95.

>But, the old value of c is being passed to a, when the
>rule says that the prior value of c should only be used
>for determining the value to be stored in c

For a = c++; , c is not being accessed twice. The value being assigned to a is the result of the expression c++ , not the value stored in c.

Narue 5,707 Bad Cop Team Colleague

>I think getch() is the most primitive input function
>of C, on which getchar(),etc. are based.

Such an implementation is certainly possible, but I haven't heard of one in the wild yet. Further, even if an implementation did exist that used getch as a primitive base for stdio, your statement is still wrong. Try again, and this time remember that getch is a non-standard library extension (on a select few compilers).

Narue 5,707 Bad Cop Team Colleague

>Its value in most systems is -1.
The actual value is irrelevant.

>In C, EOF is a macro defined as -1.
EOF is defined as a negative constant with a type of int, nothing more.

Narue 5,707 Bad Cop Team Colleague

EOF isn't a character from the stream.

Narue 5,707 Bad Cop Team Colleague

>I know that ++a is faster than a++ because a++ has to first store
>the previous value of a at some temporary location and then
>increment a and then return the saved value, whereas ++a simply
>increments a and returns the pointer to the incremented value.

That's a reasonable analysis, though you'll be interested to learn that giving ++a and a++ identical performance properties is an optimization that's been in place since Dennis Ritchie's first C compiler. It's on our list of "if you don't implement this, you fail as a compiler writer" bullet points. I would be appalled if I found a compiler where the difference between pre increment and post increment weren't vanishingly small (if measurable at all), even for cases where the expression is part of a larger expression.

>Is it translated simply to a=a+1 and does this have a speed penalty.
The following statements will generally be translated to identical machine code:

Naive: a = a + 1; Compound: a += 1; Pre increment: ++a; Post increment: a++;

Narue 5,707 Bad Cop Team Colleague

>You are so cute helping me
...

>I talke to Professor and he allowed to use C++ streams
So are you asking how to do it in with iostreams or is the question moot now?

Narue 5,707 Bad Cop Team Colleague

Think of console input as a two tier operation. At the top level is the shell (the console itself), which talks to the keyboard driver and buffers characters typed by the user. When the user signals the end of input (usually in the form of a newline), the shell stops gives control over to your program and the input stream begins buffering characters from the shell buffer.

The disconnect is at the newline. If the shell blocks until a newline, control doesn't reach your program until the user has signaled the end of input. Thus, there's no way your program can control how the user types characters without bypassing the shell and controlling input from the keyboard driver itself. This can be done, but not in a standard way. Here's one way using the conio.h library:

char base[31];
char *p = base;
int limit = 30;
int ch;

while ( --limit >= 0 && ( ch = getch() ) != '\r' )
  *p++ = ch;

Take note that by bypassing the shell buffer, you also need to manually handle things like backspaces.

Narue 5,707 Bad Cop Team Colleague

>Are you saying the other 85% should be on my hit list?
[sarcasm]
Of course not. That's silly. You should put everyone who doesn't bow down and kiss your feet on the ignore list! Stupid retarded pleebs who don't know their place deserve it. I mean really, don't they realize that we're the most awesomesauce d00ds on the planet and they should frame our posts after thanking us profusely for our time?!
[/sarcasm]

Narue 5,707 Bad Cop Team Colleague

>Can I use an overloaded assignment operator for copying character pointers.
Yes, of course. Just keep in mind that you'll probably need to do more than just copy the pointers themselves. If the memory is dynamically allocated (as I assume it is for you to be asking this question) then you basically have to duplicate the logic for the constructor with the added concern of self assignment:

#include <cstring>
#include <iostream>
#include <ostream>

class jsw_string {
  char *_base;
public:
  jsw_string ( const char *init );
  jsw_string ( const jsw_string& rhs );
  ~jsw_string();

  jsw_string& operator= ( const jsw_string& rhs );

  friend std::ostream& operator<< ( std::ostream& out, const jsw_string& s );
private:
  char *_alloc_base ( const char *src );
};

jsw_string::jsw_string ( const char *init )
{
  _base = _alloc_base ( init );
}

jsw_string::jsw_string ( const jsw_string& rhs )
{
  _base = _alloc_base ( rhs._base );
}

jsw_string::~jsw_string()
{
  delete[] _base;
}

jsw_string& jsw_string::operator= ( const jsw_string& rhs )
{
  if ( this != &rhs ) {
    char *save = _alloc_base ( rhs._base );

    delete[] _base;
    _base = save;
  }

  return *this;
}

char *jsw_string::_alloc_base ( const char *src )
{
  char *s = new char[std::strlen ( src )];

  strcpy ( s, src );

  return s;
}

std::ostream& operator<< ( std::ostream& out, const jsw_string& s )
{
  return out<< s._base;
}

int main()
{
  jsw_string s = "this is a test";
  jsw_string r = s;

  std::cout<<"s = "<< s <<"\nr = "<< r <<'\n';

  s = "foo";

  std::cout<<"s = …
iamthwee commented: Does the 's' stand for sarah or sally? +11
Narue 5,707 Bad Cop Team Colleague

>I just want to know where it will have advantages.
So much for the pioneering spirit of hackers and hobbyists... :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

>But is it worthwhile learning C if I already know python?
Personally I think everyone can benefit from learning C, but your question suggests that you don't see any point in it. If you're a hobbyist, don't force yourself to learn something you don't really want to.

Narue 5,707 Bad Cop Team Colleague

>Why is this producing an error?
Stream objects aren't copyable at the object level. As jonsca said, you need to pass a reference to the ifstream.

Narue 5,707 Bad Cop Team Colleague

>would you happen to know any good example on how to thread?
Proper multi-threading is non-trivial, so you're not likely to find one example that will teach you what you need. My recommendation would be to browse over to codeproject.com and read up on the threading articles.

Narue 5,707 Bad Cop Team Colleague

Check the class of whatever aPort is to see if it has a non-blocking read, or some way to query the port for incoming data. If not, you might want to look into spawning a separate process or thread for inputting data. At least that way the whole program won't lock up while waiting for input.

Narue 5,707 Bad Cop Team Colleague

All you're doing with the divide and conquer is breaking up the list into smaller lists, then putting the sorted smaller lists back together. This breakdown and rebuilding is an O(logn) operation:

0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7
0 1 2 3
0 1
    2 3
0 1 2 3
        4 5 6 7
        4 5
            6 7
        4 5 6 7
0 1 2 3 4 5 6 7
                8 9 A B C D E F
                8 9 A B
                8 9
                    A B
                8 9 A B
                        C D E F
                        C D
                            E F
                        C D E F
                8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 A B C D E F

Now add to that the O(n) cost of sorting each partial list, and you've got the classic O(n * logn) complexity.

Narue 5,707 Bad Cop Team Colleague

>I've seen a few odd programs (emphasis on the odd)
>that actually use the bool return type for main...

Once again, it's a requirement that main return the int type. bool main() is just as wrong as void main() . Some people might use it, but that doesn't mean you should take it into consideration in your explanations unless you're correcting someone else. ;)

Narue 5,707 Bad Cop Team Colleague

>is this possible?
Is this a troll? Or a bad joke? Of course it's possible, as are most things that noobs use the "is it possible?" question to ask about.

Narue 5,707 Bad Cop Team Colleague

>In most, if not all OSes; if a program's main function returns an int, 0
>indicates that program execution ended normally.
>Any other value indicates that the program execution ended
>abnormally. In other words an error occurred.

That's required behavior for a C++ implementation. It has nothing to do with the OS, because the calling code is (rather than the OS itself) a C++ runtime that interprets the return value of main and converts it to something suitably equivalent for the system.

>Likewise if the main function of a program returns boo
The main function doesn't return bool, and suggesting that it does, or that the integer value returned represents a boolean value, is terribly confusing.

Narue 5,707 Bad Cop Team Colleague

>I've tried stuff like srand(time) in main, however, that doesn't apply to the class
And if you do it in, say, the constructor, what do you expect will happen when you create two objects of that class? The seed is global. Anytime you call srand, the seed will change for every call to rand, regardless of what it applies to in your program's logic.

Depending on your needs, you might want to do one seed at the beginning of main, or you might want to reseed every time an object is created, or you might want to give users of the class the option to reseed at will. But saying that srand should apply to the class is likely to surprise you down the road when it doesn't really apply just to your class.

Narue 5,707 Bad Cop Team Colleague

So basically you don't know how to write the program and want us to hold your hand (or do it for you). Sorry, but no go. You'll have to work a little harder than that for your code to count as proof of effort.

Narue 5,707 Bad Cop Team Colleague

>I'm not arguing with you but global variables must be an accepted misconception..
It's well understood terminology, and everyone knows well enough what you mean when you say "global variable", so there's no point in being strictly correct when talking about them. It's like how dynamic arrays aren't really arrays. But everyone knows that when you say "dynamic array", you mean a simulated array with pointers and dynamic memory, so it's a convenient way to say the same thing with fewer words.

However, something unconventional like differentiating between "external variables" and "global variables" is an entirely different matter. It's the first time I've heard that particular distinction, so I felt the need to clarify. ;)

Narue 5,707 Bad Cop Team Colleague

>In my case, I obtain errors (undefined reference to `myVar') when I use extern in "global.h."
Are you forgetting the definition? I added a globals.c file that forces an absolute definition for myVar. If you don't have that definition somewhere, the linker won't be able to reference the object.

>This may not be 100% but I always though of global
>and external variables as indicators to the linker

Technically there's no such thing as a global variable. What people usually mean by "global" is an object with file scope and external linkage which can be made visible in other scopes using an external declaration.

Narue 5,707 Bad Cop Team Colleague

>I've read that global variables should be defined once and declared
>in evry file using those variables using the keyword extern?

Yes, that's a safe guideline. I'd do it like the following though, because it's easier to be explicit than get bitten by some of the more obscure declaration/definition rules:

/* globals.h */
extern int myVar; /* Declaration for use everywhere */
/* globals.c */
#include "globals.h"

extern int myVar = 0; /* Explicit definition with an initializer */
/* main.c */
include "global.h"
...
int someFct()
{
  myVar = something;
}
include "global.h"
...
int otherFct()
{
  if (myVar == myCondition)
  {
  }
}
Narue 5,707 Bad Cop Team Colleague

>Well then be more vocal about which features you use and how you use them.
Do you want a weekly status report about how we use the site? When you change things in a seemingly arbitrary way without asking for specific feedback from us, it's hard to give you the information you want. You could start a thread asking what we think of your ideas for future changes, but that would make far too much sense. A "conference" with some imaginary group of people who apparently know better than your highest volume users is clearly a much better choice. :icon_rolleyes:

>When you first visit DaniWeb, how do you begin your visit?
I have the main forum list bookmarked. The home page doesn't exist as far as I'm concerned, so any improvements you make there don't affect me.

sknake commented: agreed +0
Narue 5,707 Bad Cop Team Colleague

>How, without using a library written by someone else <snip> can I
>possibly write my model files on one platform, and load on another
>without either: using text files, or wasting space in my file?

Your problem is binary. Any binary output is inherently non-portable, and the only way to fix that is to normalize your output to a common format. This normalization is usually done with text conversions, but you can manually deconstruct and reconstruct your objects in a byte-wise manner as well. You seem to be leaning toward the latter, which means you need to pick a binary format, then when porting to a different system convert between that format and the system format when reading and writing your files.

Naturally this incurs an overhead cost of doing the conversion, so add that to your list of things to consider.

DarthPJB commented: narue continues to save us from programming hell! +1
Narue 5,707 Bad Cop Team Colleague

>I was thinking stdin
The stream in question is whatever input stream you want to clean up in a line-oriented fashion, but yes, it was written with stdin in mind.

>Will it be something like this ->
Close:

printf("\n\n\tPress Enter To Continue ... ");
interactive_flush(stdin);
getchar();

The idea is for interactive_flush to clean up any extraneous characters in the stream. Note that the order you had won't work if there's more than one extra character. getchar will read the extra character, then interactive_flush will clear out the rest without blocking. The thing that causes the program to pause is the input request waiting (blocking) for the user to type something. If something is already there to read, the request won't block.

In the correct order, interactive_flush cleans out the extra characters and then getchar blocks.

Narue 5,707 Bad Cop Team Colleague

>I belive three, for(Iterator = something ; while-condition; do something to limit the loop ) ?
That's correct. Now take a look at your code and see if it matches what you just described.

Narue 5,707 Bad Cop Team Colleague

>Lines 124 and 132 contain the same two errors.
I'll answer your question with another question: How many clauses does a for loop have?

p.s. You also need to include the <string> header.

Narue 5,707 Bad Cop Team Colleague

You can specialize myclass::function based on the template parameters of myclass:

#include <iostream>

struct mystruct {};

template <class T>
class myclass
{
public:
  T function()
  {
    std::cout<<"Generic template\n";
    return T();
  }
};

template<>
int myclass<int>::function()
{
  std::cout<<"int specialization\n";
  return int();
}

template<>
double myclass<double>::function()
{
  std::cout<<"double specialization\n";
  return double();
}

template<>
mystruct myclass<mystruct>::function()
{
  std::cout<<"mystruct specialization\n";
  return mystruct();
}

int main()
{
  myclass<int> a;
  myclass<double> b;
  myclass<mystruct> c;
  myclass<char> d;

  a.function();
  b.function();
  c.function();
  d.function();
}
Narue 5,707 Bad Cop Team Colleague

>So, in a nutshell, I'm looking for an alternate of the non-standard getch();
Unfortunately, you can't do it in one line without making some assumptions or rolling the whole thing up into a function. Assuming you want to convert this program:

#include <stdio.h>
#include <conio.h>

int main ( void )
{
  puts ( "Hello, world!" );

  fputs ( "Press [Enter] to continue . . .", stdout );
  fflush ( stdout );
  getch(); /* Not portable, we want to replace this line */
  
  return 0;
}

This is the equivalent portable program:

#include <stdio.h>

int main ( void )
{
  puts ( "Hello, world!" );

  fputs ( "Press [Enter] to continue . . .", stdout );
  fflush ( stdout );
  getchar();
  
  return 0;
}

There are two immediate problems with the equivalent solution:

  1. If there are any characters in the stream, getchar won't block, but getch will. This is because getchar is a part of the stdio library (and is bound to the shell) while getch reads directly from the keyboard buffer.
  2. getch will return on any single character while getchar pretty much always requires the [Enter] key to be pressed. This is because of line-oriented input from the shell.

Problem 2 isn't really a problem since you stated that you want [Enter] to be the control key for this prompt, but problem 1 is still a big problem. Unless you're careful to keep your stream clear, you may need to flush the input stream …

Narue 5,707 Bad Cop Team Colleague

>Narue, WHY are you ranting about it?
Because I'm trying to help you, and I think ranting is the only way you'll pay attention. You should be flattered. Normally, I wouldn't bother replying to someone so stubbornly clinging to an idiotic solution.

>No I'm not a professional...
Thank God.

>It's just a question, get over it and get over yourself!
As you wish. Don't expect me to help you in the future. You can learn the hard way, for all I care.

Narue 5,707 Bad Cop Team Colleague

Did you check your handy C++ library reference? Because it's all explained there, and a more specific question than "can you tell me everything I want to know?" is likely to encourage high quality answers.

Narue 5,707 Bad Cop Team Colleague

>Because once I press ignore, check the log file, see the record
>where the problem occurred, and fix the format, then I don't have a problem anymore.

Then you're parsing the file incorrectly. The issue still remains that your code is broken and needs to be fixed.

>which causes the i to go out of bounds.
Um, did it never occur to you to check the value of i before using it?

I honestly can't articulate how utterly retarded your solution to the problem is. Especially since you know exactly where the error is coming from and can reproduce it consistently. I can only hope you're not a professional working on wall street, in the medical field, or any other area where stupid programmers mean destroyed/lost lives.

Salem commented: ****applause*** +17
Narue 5,707 Bad Cop Team Colleague

>No see it's ok if it's ignored.
No, see it's not. A debug assertion doesn't mean you hit an expected runtime error condition, and it doesn't mean that your files aren't formatted the way your program expects. It means your code is fucking wrong! Programmers put assertions into code so that they can debug impossible conditions that break invariants. If you didn't place the assertion, it means you're getting it from one of the libraries you use. Ignoring that error is not okay.

>Any ideas for an automatic ignore?
Sorry, I don't help programmers do stupid things. I teach them to do smart things.

Narue 5,707 Bad Cop Team Colleague

>is there a way to make it ignore automatically? Without the message box appearing?
Yes, fix the error that causes the message in the first place. Silencing an error doesn't make the error go away, it just makes it harder to fix when ignoring it really breaks something.

Narue 5,707 Bad Cop Team Colleague

>1. Line #8 works (at least my compiler doesn't comply), but line #12 is not allowed. Why?
Because the types are incompatible. It's legal to add a const qualifier as such:

int *p = 0;
const int *q;

q = p; // Okay

Note that p and q have the same type (pointer to non-const int). However, if you add a level of indirection, things get sticky:

int **p = 0;
const int **q;

q = p; // Bzzt! Type mismatch

Now const is a type qualifier, it actually changes the type of the object. As such, int* and const int* are two different types. Here's where your error comes in. When you declare a pointer to a type, the pointer is more or less required to point to an object of that type. So let's look at the code again:

// p is a pointer that points to an object of type int*
int **p = 0;
// q is a pointer that points to an object of type const int*
const int **q;

// const int* and int* are different types, thus
// q and p point to different types and are incompatible
q = p; // Bzzt! Type mismatch

>2. Line #15 is equal to int** const . I thought it should be rather const int**.
When you create a typedef, the type is set in stone. Any qualifiers applied after that apply to the typedef as a whole. For typedef …

Narue 5,707 Bad Cop Team Colleague

>I only posted because I agreed with the OP about our instructor.
>Then you proceed to call me a sock puppet...

Exactly. I call it like I see it, and if you have a problem with that, you're welcome to kiss my ass. :)

>That's why I say you have a poor attitude.
It's always amusing when some wannabe net nanny joins Daniweb and immediately tries to tell us how to do things. This is a meritocracy. Until you've earned the right to have an opinion, nobody gives a damn what you think.

So as to avoid driving this thread off topic for too long, please PM me if you want to continue being verbally abused. kthxbye.

Narue 5,707 Bad Cop Team Colleague

>Why even bother posting when someone is asking for help, you have a very poor attitude.
Pardon? You must have missed the fact that I was the first person to help Mattpd in this thread. You must have also missed the fact that the OP hasn't replied saying that he still needs help. What right do you have to complain about my attitude when you've contributed absolutely nothing to the thread?

Narue 5,707 Bad Cop Team Colleague

>I can vouch for this guy
Yea, that's not suspicious at all. :icon_rolleyes: From past experience, I'm more inclined to believe that you're a sock puppet rather than a concerned classmate.

Narue 5,707 Bad Cop Team Colleague

>I am using Visual C++
More specifically, it looks like you're using C++/CLI. I don't recommend mixing standard C++ libraries (such as ifstream) and .NET libraries (like System::String). It's more of a headache than it's worth, if you can avoid it.

Narue 5,707 Bad Cop Team Colleague
std::string name;

// Get the name...

std::ifstream in ( ( name + ".txt" ).c_str() );
Narue 5,707 Bad Cop Team Colleague

I can't do anything about your eyesight. But it does show an example of how to define the function under the heading "Compound Assignment Operators += -= *=".

Narue 5,707 Bad Cop Team Colleague

>I cannot find on the web.
You didn't look very hard, did you? This is the first hit from google that I got using keywords from your thread title.

Narue 5,707 Bad Cop Team Colleague

I can't tell you where your problem is. I'm not quite that psychic. But I can tell you that if you have a custom allocator on m_argument, it's very possible that a bug in the allocator is leaking memory or somehow creating false positives for your leak detectors. If you don't have a custom allocator, that snippet is not likely to be the problem (assuming m_argument is a standard container). Memory leaks typically come from places where you manage memory manually, not where it's managed for you in a library.

Standard libraries are generally quite mature and obvious problems like this are weeded out because they're used a lot by a lot of people. So your first course of action is to blame your code, and not any libraries you use.

Narue 5,707 Bad Cop Team Colleague

>at this point of time i'm not sure on your question
So are we talking about a codebase that's a team effort or otherwise isn't written entirely by you?

Narue 5,707 Bad Cop Team Colleague

>Below is the block of code for your reference.
Are you using a custom allocator?

Narue 5,707 Bad Cop Team Colleague

>Sry i was trying to quote from this answer and i accidentally gave -1 at the answer.
I fixed it for you. Now it's back at 0. :)

>Can u write the de-allocation of memory in 1 in code pls??

// Allocate
T **p = new T*[ROWS];

for ( int i = 0; i < ROWS; i++ )
  p[i] = new T[COLS];

// Release
for ( int i = 0; i < ROWS; i++ )
  delete[] p[i];

delete[] p;
Narue 5,707 Bad Cop Team Colleague

>Kindly let me know of alternatives of how I could solve this problem.
You're not actually appending to the file, you're appending to lines in the file, which amounts to inserting into the file. Unless you're using a system with record oriented files, your two best options are probably the following:

  1. Store everything in memory until you're ready to save, then overwrite the file.
  2. Write a new file with your changes, then replace the original file with the new file.
Narue 5,707 Bad Cop Team Colleague

Dude, your code is severely broken in several ways. I can only assume that MyFunction is equally crappy. Start by normalizing your expected/actual types, and fix your overflow errors. I'd help you more, but I have no idea what the hell you're trying to accomplish with this cluster of a program.