deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, you can certainly discuss those things. Though the topic is rather broad, and in the case of "is it okay to do/say XYZ" the general advice stands: if you have to ask, it's probably not okay.

p.s. The only way to feel comfortable in an interview is to not want the job and not care what the interviewer(s) think about you. So get used to feeling uncomfortable. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This would be a short version of my code:

You created CLibrary using the default constructor. mA is a null pointer.

@deceptikon
You may want to read an intro level tutorial to CPP
http://www.learncpp.com/cpp-tutorial/121-pointers-and-references-to-the-base-class-of-derived-objects/

Good. Now go to the next chapter and read about virtual functions, genius. I'd also appreciate if you reversed your negative rep on me when you eventually realize I'm right.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You must be high, at run-time the container object won't know which object is a derived type (versus being a base type) unless he defines the type within the class and casts the base class pointer to the derived class pointer.

The OP is storing pointers to a base class, not objects of the base class. Read up on how polymorphism and dynamic binding work in C++ and then look at my code again, because you're clearly confused.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

@deceptikon
Your solution is wrong.
In your solution, when he calls print() it will treat all of the pointers as articles no matter what they are.

You might want to educate yourself a bit more before trying to correct people who actually know what they're talking about.

I also tried this, using an overloaded assignement operator defined for each one of the classes CArticle,CBook,CMagazine, but it breaks somewhere.

The overloaded assignment operator is irrelevant here because you're working with pointers at the container level. Please post a small but complete program that exhibits your problem and I'll see if I can locate the error.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is there any reason why the Controller can't own the objects and then dole out references to them on request? Since you're storing pointers, and from the previous discussion, I'm assuming the objects aren't owned by the Controller, just loosely contained. Is that the case?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I would add one change to the structures/classes/members shown by decepticon, and that is to use a std::vector<CArticle> for the _articles member in the CLibrary class.

I would too, but given that the description of the actual classes suggests a dynamic array of pointers, I matched my example to that.

Also, it is STRONGLY recommended by standards to NOT use leading underscores for local/class/static variables as that is reserved for internal use by system libraries, etc.

Identifiers starting with two underscores or an underscore followed by an upper case letter are always reserved, period. An identifier starting with an underscore followed by a digit or a lower case letter is not reserved except in the global namespace. My data member names are perfectly legal and conforming.

However, with that said, it couldn't hurt to always avoid leading underscores as a general best practice. The only reason I do it is because I'm confident in my understanding of the rules and restrictions.

In decepticon's example, you would have to resize it manually in your addArticle() method if the size of the array has reached its previous limit.

Once again, my example wasn't intended to be a full solution. I assumed that the OP's classes were already otherwise feature complete since he didn't ask about anything except adding polymorphic items in the interface.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I see that a lot of people are already familiar with C++11 to the point that they don't think about it when they use its features. Is it now considered 'best practice' to use the new features?

Best practice depends on your needs. If you don't need to conform to legacy code or tools, then I'd say go for it. Otherwise, you really have no choice but to follow the supported standard. Note though, that C++11 isn't likely to go the way of C99. There's a huge demand for it (and also C++14 already) as well as a huge push to get conforming implementations shipped. It won't be long before most of the new example code out there is dependent on at least C++11.

Those Turbo C++ folks will just have to suffer and learn how to convert C++11 to pre-standard C++.

I am mainly concerned due to the lack of universal support for it and the fact that some C++ programmers have yet to learn the changes so I fear that C++11 code may not be understood.

Universal support is close enough at this point, with the majority of the major features supported by all popular compilers and the rest on the horizon. As far as not understanding code because one hasn't learned the changes...now is a good time to start. ;)

Is there some macro that can tell you if C++11 is supported (so that you can make a header work in C++03, …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I can't figure out what type of parameter should my addArticle() function have in order to work for this main. I would like to let the compiler choose if the object passed is a CBook or a CMagazine. Is that possible?

Perhaps I'm missing something, but if you want polymorphism, you should use polymorphism rather than trying something tricky and dangerous:

#include <iostream>

class CArticle {
public:
    virtual void print() const = 0;
};

class CBook: public CArticle {
    int _x;
public:
    CBook(int x) : _x(x) {}

    virtual void print() const override
    {
        std::cout << "CBook: " << _x << '\n';
    }
};

class CMagazine: public CArticle {
    int _x;
    int _y;
public:
    CMagazine(int x, int y) : _x(x), _y(y) {}

    virtual void print() const override
    {
        std::cout << "CMagazine: " << _x << ' ' << _y << '\n';
    }
};

class CLibrary {
    CArticle **_articles;
    int _size;
public:
    CLibrary() : _articles(new CArticle*[10]), _size(0) {}

    ~CLibrary()
    {
        delete[] _articles;
    }

    void addArticle(CArticle *article)
    {
        _articles[_size++] = article;
    }

    void print() const
    {
        for (int i = 0; i < _size; i++) {
            _articles[i]->print();
        }
    }
};

int main()
{
    CLibrary L1;
    CArticle *A1 = new CBook(1000);
    CArticle *A2 = new CBook(1001);
    CArticle *A3 = new CMagazine(1002, 3);
    CArticle *A4 = new CMagazine(1003, 6);
    CArticle *A5 = new CMagazine(1004, 8);

    L1.addArticle(A1);
    L1.addArticle(A2);
    L1.addArticle(A3);
    L1.addArticle(A4);
    L1.addArticle(A5);

    L1.print();
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

you'd think it wouldn't build correctly but I guess technically it was correct

This is a key lesson with C++. Just because it compiles doesn't mean it's correct, either semantically where the language is concerned or logically in terms of the application's intended behavior.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Understand accounting, know how to develop software, define the features you want, and implement them.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What error?

Begginnerdev commented: Why is this voted down? He provided no error in op. +9
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

First and foremost, identifier names that are varying case from a keyword like CLASS, INT, or CHAR, are extremely bad ideas. I strongly suggest using something more informative.

To answer your specific question, pointers to class types work exactly the same way as pointers to built-in types. For example, when you increment a pointer to a class type, it skips over the size of an object of that class rather than skipping through the members. When you say *(POINTER + 2), you're trying to access memory that you don't own just as readily as if you had an array of 1 (CLASS a[1];) and tried to execute a[2]:

#include <iostream>

namespace {
    class Foo {
    public:
        int a;
        char b;
    };
}

int main()
{
    using namespace std;

    Foo obj { 123, 'a' };
    Foo *p = &obj;

    // The same address will be printed twice
    cout << p << ' ' << &obj << '\n';

    // The same value will be printed three times
    cout << *reinterpret_cast<int*>(p) << ' ' << obj.a << ' ' << p->a << '\n';

    // Please don't do this in real code
    cout << *(reinterpret_cast<char*>(p) + sizeof(int)) << '\n';
}

cout << endl << *POINTER << " " << OBJECT.INT << " " << POINTER -> INT; //WILL THESE SHOW THE SAME?

In this exact case, yes (with an appropriate cast on *POINTER). But you should be very careful when assuming the underlying structure of a class unless it's

nmakes commented: BTW, I just used them for easy readability of my post :) And I know it is a POD. But I just wanted to understand the meaning of the statements. :) And about the compiler, I'll definitely get one after I finish my school (My education board reccomends BC4. +1
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The question wasn't what interests you in the company, it was a lazy and ambiguous "why us?". The answer to which is an obvious "You tell me; why should I work here?". When interviewing, don't forget that you're interviewing the company as well as being interviewed. A good piece of advice for the interview is "don't be a tool". That'll make a better impression than the legion of sycophants the interviewer sees on the regular.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Option 1 would be preferred in this case. In option 2 you're allocating a whole new data structure to hold the filtered results and also iterating the original list in its entirety once followed by the filtered results once.

In option 1 you have what essentially boils down to a lazy evaluated yielding iterator. So it doesn't allocate any new data structures and only traverses the original list once (filtering out items that fail your predicate as it goes).

I'd expect option 1 to be faster as well as less wasteful of resources. But the compiler may optimize things such that the resulting difference is negligible.

ddanbe commented: Great! +14
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The rules don't change just because you're nesting a control structure:

if (foo)
{
    if (bar)
    {
        // Blah blah
    }
}
else
{
    if (baz)
    {
        // Blah blah
    }
    else
    {
        // Blah blah
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Thank you for your reply, but that is not the answer I was looking for.

I suspect the answer you're looking for is unreasonable. A better solution is to fix your design such that you don't need to wade into the shithole of pointers to arrays. For example, using a class for your bounding box and then having an array of objects of that class.

What would be the call if I did not use a typedef (and using only the new operator)?

Off the top of my head, I can't recall the correct syntax without using a typedef:

typedef double (*bb_t)[4][3];

double (**pCornerPoints)[4][3] = new bb_t[10]; // Or bb_t *pCornerPoints = new bb_t[10];
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

There's a way to systematically convert a recursive algorithm to an iterative one, but unless you're dealing strictly with tail recursion, some form of stack data structure is still required. Do your contest requirements exclude the high probability of virtual memory on the machine being sufficient to contribute to the definition of "infinite"?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What problem are you having such that you need help? Simply saying "Any help is appreciated" is very broad. For example, I'd do something completely different; does throwing away your code and starting over constitute "help"?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Ignoring for the moment that this is a horrible example of object orientation and brings into question whether you're qualified to be giving out examples as if you know what you're doing, what exactly is the benefit of an object oriented bubble sort here?

rubberman commented: Succinctly put D. :-) +12
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

At the moment, Baby Steps. Of all time, probably W Juliet or Day of Revolution.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I have Not Studied about Pooling in ADO.NET and So I don't Know about the Concept of Pooling

Then I guess you're not qualified to answer the question. Why should we help you cheat?

If Anybody can Solve It for me ?

Honestly, I don't think any of the provided answers are totally correct.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Besides, it is not part of the standard and your code might not work with other compliers. Before each scanf(), put fflush(STDIN); to make sure the input stream is empty.

Um...I hate to break it to you, but 1) C++ is case sensitive and STDIN is not a valid alias for stdin, and 2) fflush() isn't defined for input streams. So in one sentence you attacked non-standard code and then in the next sentence advocated non-standard code. ;)

Another thing to do is check the return value of scanf() which will tell you how many good values were read. If it does not match the number of variables that you were expecting, something went wrong; probably the user entered bad data.

Failure to check the return value of scanf() is probably one of the most common problems I see in beginner code.

David W commented: no need to be bashful ... it all needed to be corrected +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

in answer 4, can you elborate more that answer ? what can be the needs he can mention when i will ask that question to him ?

The point of the answer is to highlight that the question is too vague and take control over the interview by taking the interviewer out of their usual game.

in answer 6th, "getting angry" only means getting angry.

"Getting angry" is ambiguous. It could mean as little as a fleeting moment of irritation, or in excess of going postal and committing multiple homocides. My answer is a request for a more reasonable range of possibilities. Once again, this takes the interviewer out of their usual game and exhibits a critical skill for a developer: fully understanding the problem by asking questions rather than making assumptions and jumping to a poor conclusion.

in answer 5th, what else ? can we add something more to it ?

You're the one answering the question, add whatever you want.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So you saying a deliberately deceptive headline that catches an attention is a good thing?

I'm only saying that it's effective.

It's just another way to piss off some people who happen to like SO.

How about "Google stole our traffic and gave it to StackOverflow!"?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I just did not like the title of this post.

It caught your attention though, didn't it? :D

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

so those who love banners can enjoy them.

Spoken like someone who doesn't depend on ad revenue to stay in business. Hop in chat sometime and ask Dani to explain where Daniweb's funding comes from. She'll talk your ear off, but it's an eye opener.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

do u think 1 hour is sifficient ?

Certainly not, but as you're hopefully not trying to deceive the interviewer, that's not your problem, is it? ;)

1.Why amazon ?

Why not?

2.what will you do if you get offer from google after getting offer from us ?

I'll do a cost benefit analysis and choose the offer that better suits my current career goals.

3.what are your weakness ?

I tend to oversimplify, and as such need to take special care to consider all possibilities when designing a solution so as to avoid missing something important but not obvious.

4.how can you contribute to amazon ?

The job description was specific about duties, was it not? While I'm sure I can provide significant added value beyond that, it's difficult to enumerate such things when the question is vague, and more importantly, broad. Could you be more specific about needs that aren't listed in the job description?

5.what do you expect from amazon/microsoft/....etc..?

Noting that as I've not worked for the mentioned companies, I try not to form expectations from a position of ignorance. However, I certainly expect the bare minimum of an employee/employer contract wherein I do my best to complete assigned tasks to the best of my ability and you compensate me fairly for that effort.

6.on what point do u get angry ?

Angry is ambiguous. Could you be more specific as to …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I thought that any change in pointer was reflected in main().

You can pass a reference to achieve that behavior.

Is same behaviour expected in C?

Yes.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is this an ASMX web service or a WCF web service? Is it active or do you just have the code/assembly?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

how it is used and why we use it

In theory you would use it just like char, and the purpose is to support extended character sets. The char type for character usage was really designed and intended for character sets (and language alphabets) that can fit into a single byte, such as ASCII or EBCDIC.

In practice, conforming to larger character sets such as Unicode means you're targeting internationalization as a goal, which is quite a bit more complex than just swapping char for wchar_t. Even when you stick to a single locale, conversion between char and wchar_t is often necessary.

And also what is the difference between char and wchar_t.

char is guaranteed to be a single byte. wchar_t is a typedef for one of the integer types, and it may be multiple bytes so that it can represent any value in a large character set. You can't assume any specific size for wchar_t.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Actually the C coding standard often used does not allow declaring variables inside a 'for loop' ...

It depends on which standard you're compiling for. Prior to C99 you cannot declare a variable in the initialization clause of a for loop. Starting with C99, you can.

Note that prior to C99 you might be able to do it, but your code would not be portable. This is usually the case when the compiler supports this feature as an extension, or if the code is being compiled as C++.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your description of the problem and solution seems dicey. Could you elaborate a bit on what this critical piece of code is so that it's easier to determine what IPC solutions would work best? Right now you basically have a race condition with your locking file, which is a bigger issue than CPU usage, in my opinion.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Consider this simple example of clearing the input line on an invalid conversion:

#include <ios>
#include <iostream>
#include <limits>

using namespace std;

int main()
{
    int num;

    while (true)
    {
        cout << "Enter a number: ";

        if (cin >> num)
        {
            break;
        }

        cerr << "Invalid input\n";

        // Clear the stream state so we can read again
        cin.clear();

        // Extract everything remaining on the line
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    cout << "You entered " << num << '\n';
}

When a conversion by the >> operator fails, it puts cin into an error state where it won't read anymore data. Also, the bad data is left on the stream. So you need to clear the error state and then discard the bad data before trying again.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Instead of deleting it, would it be best to move the file then delete it?

To what end? A move operation ends with deletion of the original file. You'll have similar issues as straight up deletion if the original file is in use.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I created a service, it deletes files, but every so often, it cannot delete a file.

In cases like this, rather than try to find a sweet spot for timing, I'll simply add the failed paths to a collection and retry later during a global cleanup operation. Most likely it's not imperative that these files be deleted immediately, so if they fail due to being in use, it's probably fine to postpone.

I still feel that 3 seconds is to short for mine.

Then use a larger value for retries. Once again, this is precisely why I made it a method parameter instead of hard coding it to 3. 3 has worked for me in the past, but I'm not naive enough to believe that it's the perfect value for all occasions.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But, are you sure that 3 is enough?

Reasonably sure. If it takes longer than that, I treat it as an error condition because disposal and unlocking are relatively quick. If disposal and unlocking are known to be slow for some reason, that's an edge case that warrants longer waits (hence adding retries as a method parameter). But under normal circumstances, and in my experience, 3 pauses of 100ms are more than sufficient to let cleanup happen.

This would be better:

No, it wouldn't be. What if the process doesn't have permission to delete files in that directory? You've hung your application in an infinite loop. In that case logging is a necessity because now there's a critical need to troubleshoot the bad behavior.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, but i still need to translate it to C++.

Since it's already valid C++ (assuming compiler support for non-portable stuff), you'll need to define what you mean by "C++".

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Daniweb rules clearly state that a thread cannot be requested to be deleted

You can request for a thread to be deleted by either flagging the first post or PMing a mod directly, but that request most likely won't be granted.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Really? How is it that so many people these days are getting into projects without knowing the first thing about C++? Here, this program will read and print every line in a file:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    ifstream in("myfile.txt");

    if (in)
    {
        string line;

        while (getline(in, line))
        {
            cout << line << '\n';
        }
    }
}

I strongly recommend getting a book on C++ and reading it. Alternatively you can search Google for tutorials. This forum is best suited for helping with specific problems, not teaching you from scratch.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You need to actually read every line, but you can ignore the lines at a specific "index" by simply not printing them.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So, please give me some advice.

My advice is learn the material before you're so pressed for time. If you don't know anything about how to work with classes, then there's nothing I can do for you in ~2 hours that doesn't constitute doing the assignment for you. Sorry, but this assignment is a loss due to your procrastination.

If you have a C++ book I'd suggest cracking it open and reading it as well as following the exercises. If you don't have a book, you should probably get one, but you can also search Google for tutorials.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So, I need to implement the following class

Okay. And?

Thanks you guys.

For what? You completely failed to ask a question. I'll take the high ground and assume that you're not expecting us to do your homework for you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

And, at least as far as I can tell, are also always contiguous in memory: one row followed by the other.

Nope, not guaranteed. The as-if rule is in effect. Provided the result of defined operations is as-if some assumption were true, the implementation need not actually conform to the assumption under the hood.

Anything else would be irrational.

What's irrational is your quoting of the standard on one hand and then appealing to rationality on the other. The standard is always right, even when it's not internally consistent.

This has the rather convenient advantage of allowing the pointer to a single row to be passed to a function as &A[row][0].

I don't see how that advantage benefits from contiguous storage of the first dimension. However, if you're somehow implying that one can do any variation of this:

#include <stdio.h>

void foo(int *p)
{
    while (*p)
    {
        printf("%d\n", *p++);
    }
}

int main(void)
{
    int a[2][4] =
    {
        {1, 2, 3, 4},
        {5, 6, 7, 0}
    };

    foo(&a[0][0]);

    return 0;
}

Then I'd direct you to §6.5.6.8 (C99), the last sentence of which is ambiguous enough to justify a padding element between "contiguous" arrays in memory: "If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated." Technically the arrays may be contiguous, but the presence of such a padding element is a confounding …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Hi, what is the value of "retries"?

Whatever you want, that's why it's a method parameter. I often use 3, though.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I am guessing however that the pointer in the while loop is always pointing to string data, until it
iterates on the final drive, and hits another \0 character, which causes the while test to be false.

Good guess. However, there's a bit of a trick here because the pointer you're using points to potentially multiple strings pasted next to each other. For example, the string might be: "C:\0D:\0E:\0\0". So what happens is the body of the loop will use strlen() + 1 to push the pointer over the first null character. When it reaches the extra null character at the end, the loop stops for the reason you guessed. But it won't stop on the previous null characters because of stepping the pointer by strlen() + 1 characters.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can use a va_list twice, provided it's properly disposed of with va_end() and re-initialized with va_start() or copied with va_copy(). What you can't do is assume that va_list represents a bidirectional read-only collection. It could easily be a generated array or list of arguments that are modified or destroyed after processing with va_arg.

nitin1 commented: nice one!! +2
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Double tabbing is more of a workaround I came up with when trying to accomplish the same thing than the "official" way to do it. Markdown isn't exactly meant for that kind of advanced formatting.

Shouldn't the code insertion tool have done that for me anyway?

The code insertion tool has no way of knowing that 1) you're in a list or 2) that you want the code to be part of the list. Technically you could highlight everything in the code editor and hit tab as well, but that's probably not what you meant. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

malloc guarntess that memory allocated is contigous right ?

Yes.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Make sure there's a blank line before and after the code, and double tab it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why do adults like watching cartoons?

Why do some adults think that you have to stop doing fun things at a certain age? ;)

KKK...well, probably not my thing unless it follows a similar pattern and humour as Lucky Star.

It's along the same genre, but I doubt it'll measure up to Lucky Star.

LL is going to be a yuri fan-service show and we both know it ;)

That's a fair prediction. I honestly don't expect much.

Additionally, did you watch the first 13 episodes of Shingeki no Kyojin?

It failed my 3 episode test (shonen isn't typically my thing), but ~s.o.s~ said it starts getting really good after about 6 episodes.