Narue 5,707 Bad Cop Team Colleague

Can anyone explain -in simple words as possible- what happens in the first code so that it prints the result that way?

Undefined behavior.

Narue 5,707 Bad Cop Team Colleague

you can talk to him if u want for this.

Invite him to Daniweb and I'll be happy to tear him a new one.

Narue 5,707 Bad Cop Team Colleague

graphics.h is certainly a problem, why do you use a graphics library from two decades ago?

Narue 5,707 Bad Cop Team Colleague

I am getting a hex values as my output and i want to convert it into ascii so that it is readable.. does this help you?

No, it doesn't. Your terminology is confused, which makes it very difficult to understand exactly what you want without specific examples. Please post the exact output you want to see, since you're clearly not describing the problem adequately.

Narue 5,707 Bad Cop Team Colleague

Shouldn't the strlen only return 2 (or 4 with 'ls'), shouln't the string in the system(); call be cut of, and shouln't I get a segmentation fault?

Yes, yes, and yes. Or no, no, and no. Or any combination thereof. Undefined behavior means any damn thing can happen, which includes working perfectly.

Narue 5,707 Bad Cop Team Colleague

Only one problem, what is a masked text box?

Here's a hint for the future: always try to research things like this on your own first. The only if you come up with nothing or fail to understand what you've found should you ask for clarification. In this case, "masked text box" is standard terminology, and when passed to Google will certainly tell you what you want to know in the first hits.

Narue 5,707 Bad Cop Team Colleague

But does that mean such a automatic aligning of the matrix in CONSOLE Application is impossible, No other robust method?

No, it just means I had neither the time nor the inclination to write a robust function for you when I think the whole concept is stupid. raw_gets() is a good starting point for adding the necessary checks and features to make it robust, but I'll leave that up to you.

Narue 5,707 Bad Cop Team Colleague

tell me this... is the following code correct?

#include <iostream>
using namespace std;

int main () {
  int n;
  n=48:45:4c:4c:4f:20:57:4f:52:4c:44;
  cout << "ASC " << n << endl 
  return 0;
}

Is this a trick question? Of course it's not correct, as any compiler will tell you. How about explaining exactly what output you were expecting from this code, and I'll tell you how to achieve it correctly.

Narue 5,707 Bad Cop Team Colleague

Wow! I never knew it was a C function. Thanks for sharing that, now onward i will not use that.

The fact that it's a function inherited from C is largely irrelevant. The important piece of information is that gets() is quite literally impossible to use safely. There's always a chance of buffer overflow, and you cannot avoid it. You can create the same problem using cin and C-style strings:

char buf[N];

cin >> buf; // No better than gets()

But at least here there's a fix for it:

char buf[N];

cin >> setw(N) >> buf; // All better

Of course, in C++ you should prefer to use the string class over C-style strings because they're easier to get right.

Narue 5,707 Bad Cop Team Colleague

Please provide some new information to justify a second thread on the topic.

Narue 5,707 Bad Cop Team Colleague

But why did you ask me to use at my risk?

Because it's example code and not robust enough to be usable in any kind of real program.

Narue 5,707 Bad Cop Team Colleague

I understand your confusion, but please don't be tricked into thinking that the code isn't broken when you provide insufficient space for strcat().

Narue 5,707 Bad Cop Team Colleague

as far as I can tell, strcat depends on the string pointed to by its first parameter to be large enough to hold the two strings.

Correct, it's your job to ensure that the destination has enough room.

but if I change the code to buff = malloc(sizeof(command)+2); (or pretty much any size at all, smaller than 42) the 'string' is expanded to be 42 characters long?

Expanded? No. C doesn't stop you from writing beyond the boundaries of an array. In Java, for example, you'll get an exception immediately, but C will happily write to memory you don't own and silently corrupt things. If you're lucky, you'll corrupt something that causes a fatal error, but in this case it seems as if you weren't lucky.

Narue 5,707 Bad Cop Team Colleague

Obviously I have some wrong assumptions, because this prints "strsize = 8" no matter what.

Good call. buff is a pointer to char, so sizeof(buff) will always tell you what the size of a pointer to char is, not the length of the string it points to (if it points to a string at all, which isn't a safe assumption). What you want is the strlen() function, not sizeof.

Also note that strcat() expects the destination to be a valid string, which means you code is broken. This will fix it:

char* buff = malloc(sizeof(command)+42);

buff[0] = '\0'; /* Make buff a valid string */

strcat(buff, "notify-send \"notif finished\" \"Command: '");

Finally, it's good practice to check malloc() for failure. Otherwise you'll attempt to dereference a null pointer.

Bladtman242 commented: Solved my (initial) problem :) +3
Narue 5,707 Bad Cop Team Colleague

getcwd() returns the current working directory. That's the directory to which you can expect relative paths to resolve. Thus, it certainly explains why your relative path isn't finding the file if the current working directory is different. An easy solution would be using chdir() to force the current directory you want rather than using whatever happens to be the default.

Narue 5,707 Bad Cop Team Colleague

Your compiler should support some form of getcwd(), check the documentation.

Narue 5,707 Bad Cop Team Colleague

AES isn't especially difficult to implement from the specification. Ignoring the usual caveats of reinventing the wheel on important stuff like encryption, you could write your own class:

#ifndef JSW_AES_PROVIDER_H
#define JSW_AES_PROVIDER_H

#include <vector>

namespace jsw {
    namespace encryption {
        typedef std::vector<unsigned char> bytes_t;
        
        class aes_provider {
        public:
            aes_provider(bytes_t key) { reset(key); }

            void normalize(bytes_t& bytes);
            bytes_t encrypt(const bytes_t& bytes);
            bytes_t decrypt(const bytes_t& bytes);
        private:
            static bytes_t::value_type Sbox[][16];
            static bytes_t::value_type Sbox_inverse[][16];
            static bytes_t::value_type Rcon[][4];
        private:
            int Nb; // Input block length in 32-bit increments (always 4)
            int Nk; // Key length in 32-bit increments (4, 6, or 8)
            int Nr; // Number of rounds corresponding to key size (4:10, 6:12, 8:14)

            bytes_t key;                // Seed key
            std::vector<bytes_t> w;     // Key schedule
            std::vector<bytes_t> state; // State matrix

            void reset(bytes_t key);
            void key_expansion();
            void add_round_key(int round);
            void sub_bytes(bytes_t::value_type Sbox[][16]);
            void shift_rows();
            void inverse_shift_rows();
            void mix_columns();
            void inverse_mix_columns();
            bytes_t sub_word(bytes_t word);
            bytes_t rot_word(bytes_t word);
            unsigned char gfield_mul(unsigned char a, unsigned char b);
        };
    }
}

#endif
#include <stdexcept>
#include <vector>
#include "aes_provider.h"

namespace jsw {
    namespace encryption {
        void aes_provider::normalize(bytes_t& bytes)
        {
            // Add null padding to the input (modulo 128-bits for AES)
            while (bytes.size() % 16 != 0)
                bytes.push_back(0);
        }
        
        bytes_t aes_provider::encrypt(const bytes_t& bytes)
        {
            state = std::vector<bytes_t>(4, bytes_t(Nb));

            // Input to state
            for (int r = 0; r < 4; r++) {
                for (int c = 0; c < Nb; c++)
                    state[r][c] = bytes[r + 4 * c];
            }

            add_round_key(0);

            for (int round = 1; round < Nr; round++) …
Narue 5,707 Bad Cop Team Colleague

I know that with the pointer you use the "->" operator to access the object methods, and with the other one you use the "." operator, but what exactly is the difference between them?

In the first declaration, rectangle is a reference to an address where a Shape object happens to be stored. rectangle is not a Shape, it's a container for an address that will be interpreted as a Shape object. This differs from the second declaration where rectangle is a Shape object.

I suspect you're confused by the type of the pointer, Shape*, which says "the address I hold, if not null, will represent an object of type Shape". It's necessary for both pointer arithmetic and type checking, but doesn't change the simple fact that a pointer holds an address, not an object.

Narue 5,707 Bad Cop Team Colleague

Wouldn't it be better if we just defined the functions before the main function instead of having a function prototype and a function definition in separate areas?

What if you want to break your program up into multiple files? What if you want to write a library that will be used in multiple programs?

Narue 5,707 Bad Cop Team Colleague

I can't flag the post as bad as it is not a post it is a reputation comment.

You can always PM a moderator or admin with any concerns. We won't bite, I swear. ;) But as far as reputation goes, it's inherently subjective, so we'll typically not retract rep unless the comment breaks Daniweb's rules (eg. it's offensive or obscene). But it never hurts to ask.

Narue 5,707 Bad Cop Team Colleague

At most this would be a popularity contest, and I think that's what the OP intended.

Maybe you people can show some statistics in a regular basis

Statistics don't say much. Going by numbers, the mod who formats and adds code tags to 100 posts had a greater impact than the mod who proactively banned a single spammer account. But in reality the potential negative effect of letting a spammer run rampant for even an hour makes the net impact of the ban unknowable.

Or consider a mod who resolves disputes through PM versus a mod who polices quality control in forums through replies rather than the infraction system? There are no numbers from that, and thus, no statistics despite the significant amount of effort involved.

After-all the ones working in the background should get some spotlight & should be acknowledged.

It's a thankless job to be sure, but rather than look for the "best" moderator, I think a better way to show your appreciation is simply to thank the whole team. We all work together for the greater good, after all. ;)

Narue 5,707 Bad Cop Team Colleague

is there a better way to store strings into an array than using strcpy()?

You could write directly to the array, but if you have an existing string in memory and need to copy it to an array then strcpy() is one of the better options. Though keep in mind that if the source string exceeds the length of your destination array, strcpy() doesn't stop you and you'll introduce a bug. You'll want to check lengths first:

char dst[MAX_LEN];

if (strlen(src) > sizeof dst) {
    /* Handle the error, usually by extending memory or truncating the source string */
}

strcpy(dst, src);

If you don't care about potentially losing data on long source strings, the conventional method for safely "storing" a string is strncat():

dst[0] = '\0' /* This part is important! */
strncat(dst, src, sizeof dst);

There are two interesting questions that arise from this convention:

  1. Why set the first element of dst to '\0'? Because strncat() concatenates strings, and because of this expects dst to be a valid string (unlike strcpy()). The definition of a valid string is a potentially empty sequence of characters terminated with '\0'. Thus setting dst[0] to '\0' makes dst an empty string, which forces strncat() to start copying at the beginning. Failure to do this could potentially result the serious bug of calling strncat() with an uninitialized destination string.
  2. Why use strncat() instead of strncpy()? If you go by names then strncpy() makes more sense because you want strcpy() …
Narue 5,707 Bad Cop Team Colleague

Yes, i want the space to be inserted when you press enter, without jumping to the next line. And when the maximum number of columns in a row is reached, it should jump on to the next line ie. the new row....

Assuming you want the console to auto-format itself according to the size of your matrix, this cannot be done without taking control of the console in a non-standard way. It's not terribly difficult, but I'd question the practicality of replacing the console with your own custom shell for the sole purpose of formatting matrix input.

Here's a very naive example of the concept:

#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <conio.h>

using namespace std;

string raw_gets()
{
    string result;
    int ch;
    
    // Naive algorithm, use at your own risk
    while (!isspace(ch = getch())) {
        result += (char)ch;
        cout << (char)ch;
    }
    
    return result;
}

int main()
{
    int mat[2][4];
    
    cout << "Enter the matrix:\n";
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
            stringstream conv(raw_gets());
            int x;
            
            conv >> x;
            mat[i][j] = x;
            
            cout << (j < 3 ? ' ' : '\n');
        }
    }
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++)
            cout << mat[i][j] << ' ';
            
        cout << '\n';
    }
}
PrimePackster commented: Thank U very much +3
Narue 5,707 Bad Cop Team Colleague

When the problem description includes "prints really weird lines" then the problem is very likely to be uninitialized strings. I didn't run your code, but I would guess that it stems from writing sporadically to name with the index i . i will increment whether you copy to name or not, which means you'll have holes. Try this instead:

strcpy(name[count],lol[i].name);
Narue 5,707 Bad Cop Team Colleague

yes i tried google but didnt find anything that was refered to console apps.

Did you not read my first post?

Narue 5,707 Bad Cop Team Colleague

I guess the reputation feature must have been removed

It wasn't removed, it just looks different. On every post you'll find an up and down arrow along with a counter. The counter is the number of people who have clicked the arrows to vote on a post (positive and negative votes correspond to up and down arrows, respectively). When you choose to vote on a post, you have the option of also giving reputation and including a comment, this is where you'll find the reputation feature now.

So rather than removing a feature, Daniweb added one. :) Votes are a way of saying you like or dislike a post without being forced to give out reputation, and reputation can still be given out if you also want to leave feedback in the form of a comment.

Narue 5,707 Bad Cop Team Colleague

Yes but i tried learning opengl and it is pain in the ass to make it work!

If you're going to give up as soon as it gets hard, then programming isn't for you.

What were u saying about double buffering?

Double Buffering.

Narue 5,707 Bad Cop Team Colleague

The console isn't meant to support graphics engines. If you want graphics, get a graphics library where you can use tricks like double buffering to avoid the flicker effect. If you want to use ASCII art graphics, be prepared to suffer the limitations of the console. It's that simple.

Narue 5,707 Bad Cop Team Colleague

Assuming Turbo C's bin directory is in your PATH and the presence of C:\MyCode containing your source files:

C:\MyCode>tcc myprog.c
Narue 5,707 Bad Cop Team Colleague

I'm guessing with gcc there will be no optimization, unless called with -O or similar parameters?

It's best not to guess. GCC is very aggressive when it comes to optimization, so a test using assembly output would be the better approach.

Bladtman242 commented: This is great :) +3
Narue 5,707 Bad Cop Team Colleague

I hope I'm posting in the right forum :)

I'm primarily concerned with C compilers and sun's JDK, but general info is more than welcome (Documentation for self-education as well).

Well, if i wrote a program like

int i = 5
printf("i is $d", i);

Would i=5 be stored separately in memory, or would it exactly the same the following?

printf("i is 5");

(perhaps ("i is $d", 5) is more accurate)

If not, one might balance readablity/maintainability against performance (small improvement perhaps, but I'm a sucker for learning these things, if not using them :) )

An optimizing compiler would likely choose your final option, assuming there's nothing in the code that would require i to exist as an actual entity. There are a huge number of factors involved when it comes to optimization though, so without a full example it's hard to tell you what the possible outcomes are.

Narue 5,707 Bad Cop Team Colleague

No user-defined function can run outside main().

Assuming you mean that no user-defined function can run before main (ignoring that you seem to have misunderstood the OP), consider this:

#include <iostream>

class foo {
public:
    foo() { std::cout << "Me first!" << std::endl; }
} obj;

int main()
{
    std::cout << "In main()" << std::endl;
}
LRRR commented: Whenever I read your post, I always learn something new. +2
PrimePackster commented: Can't resist adding reps... +0
Narue 5,707 Bad Cop Team Colleague

I wanna know what type of parser is used in the gcc and turbo c++ compilers. whether a top-down or bottom-up parser is used..... ?

GCC uses a hand written recursive descent parser (ie. it's top-down). I wouldn't be surprised if Turbo C++ worked the same way, though it might use one of the parser generators rather than a hand written approach.

Narue 5,707 Bad Cop Team Colleague

Is it that exit(10); has no role in the code?

That's correct. However, there's a use case where the opposite situation would remove a warning. Some compilers will warn about the following with something akin to "no return from a function with non-void return type":

static int error_code;

...

int main(void)
{
    ...

    exit(error_code);
}

In such a case a common solution is adding a redundant return that will never be executed due to the call to exit():

static int error_code;

...

int main(void)
{
    ...

    exit(error_code);
    return 0;
}

And is the function actually printing anything?

The side effect of calling printf() is that it prints something. :D

Narue 5,707 Bad Cop Team Colleague

Let's use a more complete example:

class Foo {
public:
    typedef int SubType;
};

template <class T>
class MyClass {
    typename T::SubType * ptr;
};

int main()
{
    MyClass<Foo> obj;
}

Foo::SubType is clearly a type, right? But in a dependent name context C++ will assume that the dependent name designates an object unless you qualify it with the typename keyword. A dependent name is a name that depends on a template argument (odd that, huh?), and won't be resolved until the template is instantiated. T::SubType is a dependent name because the parent class represented by template parameter T needs to be instantiated before SubType can be successfully resolved. In other words, T::SubType depends on the instantiation of T .

The end result is that MyClass<Foo>::ptr is a pointer to int because T::SubType resolves to Foo::SubType , which is a typedef for int .

Narue 5,707 Bad Cop Team Colleague

But if the function doesn't work, the exit function will end it up and return a code error 10

That's completely wrong. The call to exit() will never occur because there's an unconditional return immediately prior.

what is the relation between int k and return 0?

There's no relation.

why it is returning 0?

Because the convention of C is to return a status code from main. If the status code is 0, that means successful termination. Anything else is unsuccessful.

where it is passing it?

The C runtime environment. Some people will say the operating system, but that's not strictly correct.

who is checking if the function has succeded or not?

Potentially nobody, but it stands to reason that the user or application who initially ran your program should have access to the final status code, right?

Narue 5,707 Bad Cop Team Colleague

what is CSV?

Comma Separated Values. Google it for more details.

Narue 5,707 Bad Cop Team Colleague

I found out that the element pick random address and two of the address is str_iteration's address and curr_point's address.

str_iteration and curr_point are both separate entities. They each have a unique address that has nothing to do with the addresses of the pointed to objects.

Wait a minute, so it is actually a multi-dimension array?

Not technically, but you can use it as one because it's an array of simulated arrays. Only the first dimension is an actual array, but the second dimension is a pointer with memory allocated by new, which is functionally very similar to an array.

thought so not really understand why not this one?
for (char **p = **curr_point)

Because there's a type mismatch. **curr_point is type char , not char** . You could certainly say char **p = curr_point , but that's no different from your current loop.

I assigning one pointer to another pointer? so, parr is a pointer? hmm now my mind will come out with (array == pointer) =S I missing something here???

I went to great lengths to stress that arrays are not pointers, yet you still gravitated toward that misconception. No, array != pointer, ever. When you say curr_point = parr , it really means curr_point = &parr[0] .

but if I delete either through curr_point(pointer) or the parr(array) itself, that parr that always refer to the first element will refer to garbage value because the value's there being 'deleted' already... well, if …

Narue 5,707 Bad Cop Team Colleague
for (char **curr_point = parr, **bound_point = parr + maxStrings;
curr_point < bound_point; ++curr_point)

Please someone correct me, if I am wrong but, I don't think it is safe to use such addition :
**bound_point = parr + maxStrings; maxStrings = 5 it's an int.

The size of a char is 1 byte, but if it was an int (4 byte), this method wouldn't work. In the end of the loop, it would be pointing to the memory area of the second element of the ""array""(int) instead of the desired bound point. (The bound point would be pointing in the second int.)

I believe you're trying to say that addition on a pointer will always use a step size of one byte, which isn't how pointers work. The step size of a pointer depends on the size of the pointed to type, so p + n under the hood is more along the lines of (char*)p + (n * sizeof *p) .

Narue 5,707 Bad Cop Team Colleague

1- element of a vector/array can "share" same address with the vector/array it was put into. (Well, I always thought each value has its own address)
(reference = 2)

The address of the array and the address of the first element are the same. This makes sense when you think of array indexing as an offset from a base address. (base + 0) == base , right? So &base[0] == base as well.

2- from what I learned, when we setup an array, the pointer will referred to the first element automatically, and that's what (I thought) happen on the pointer's pointer's value(**parr and **curr_points)
(reference = 5 / 7)

That's what happens. Though **parr will always produce the same result because you're always accessing parr[0][0] .

3- based on '2-', can anyone give an idea on how to iterate through the pointer's pointer's value? (does ++**curr_point works?) :P

If I understand your question, you still need some form of iterator. You can't use *curr_point directly, but you can use it as a base:

for (char *p = *curr_point; *p != '\0'; p++)
    std::cout << *p << '\n';

The reason you can't use *curr_point directly is somewhat subtle. If it's an actual array then the pointer isn't modifiable and your code will fail. If it's a simulated array as in your example then modifying it will break subsequent code that relies on parr . This is because curr_point is referencing parr , so changes to …

Narue 5,707 Bad Cop Team Colleague

I would like to know the best beginner book for C++.

The best book is the one you understand that makes the fewest mistakes. I (and most clueful C++ programmers) recommend Accelerated C++ as a first book due to quality, but that doesn't mean it will be a good fit for you.

My teacher has referred Robert Lafore and The Complete Reference.

This suggests that your teacher isn't very good. My condolences. While Robert Lafore doesn't write horrible books, they're still bottom shelf quality-wise. And Herbert Schildt's books have been the butt of jokes for decades.

Narue 5,707 Bad Cop Team Colleague

The easiest way would probably be split and trim:

string[] parts = LFname.Split(',');

if (parts.Length != 2) {
    // Handle unexpected input
}

textbox1.Text = parts[0].Trim();
textbox2.Text = parts[1].Trim();
Narue 5,707 Bad Cop Team Colleague

It does discusses how the * and & operators do not function identically(?) for chars and ints.

Once again, this is different from pointers in general. It's a specific matter of using pointers to view an object as a different type:

int i = 123;
char *p = (char*)&i; // View i through char colored glasses

This is called type punning, and while pointers make it possible, it doesn't change how pointers work. The * and & operators still do the same thing in the same way. Note that I used a cast to force the result of &i (which is int*) into char*. That's what Stroustrup is talking about with the reinterpret_cast being a better choice. It doesn't change the effect, it just makes your intentions more clear:

int i = 123;
char *p = reinterpret_cast<char*>(&i); // View i through char colored glasses

I will read more on how types affect pointer and de-reference operators.

Read more on pointers vs. references, that's what the difference is. It's a shame that the same operators are reused for completely different purposes, because that's likely to confuse you.

Narue 5,707 Bad Cop Team Colleague

What exactly did you read, and what exactly didn't you understand? Command line parameters are exceedingly simple, which suggests that you didn't try very hard or are over-complicating things.

Narue 5,707 Bad Cop Team Colleague

there is problem , in main

What is problem? :icon_rolleyes: Do you take your car to the mechanic and say "Something is wrong"? Of course not, because the mechanic will find all kinds of things to fix and charge you a buttload of money for being too stupid to give a specific description of what you want fixed. It's something of an idiot tax.

Around here we won't charge you a buttload of money, but we will ignore your pleas for help. Given that you have over 50 posts, you should know how things work and ask a smart question.

Narue 5,707 Bad Cop Team Colleague

You've specified the type of the object, not the type of the literal. An integer literal without any suffix has the type signed int, which means you can't have a value that exceeds the range of signed int[1]. That's why the suffixes are there, so that you can create a literal that fits a larger type than the default.


[1] Compilers will generally warn you to that effect, but still treat the code as if you used a suffix. This is a convenience only, and not required.

Narue 5,707 Bad Cop Team Colleague

Does nesting/recursion work for the dereferencing operator as well?

Certainly:

#include <iostream>

int main()
{
    int i = 123;
    int *p1 = &i;
    int **p2 = &p1;
    int ***p3 = &p2;
    
    std::cout << &p2 << " -- " << p3 << '\n';
    std::cout << &p1 << " -- " << *p3 << '\n';
    std::cout << &i << " -- " << **p3 << '\n';
    std::cout << i << " -- " << ***p3 << '\n';
}
int &i = *p1;
int &&i = **p2;
int &&&i = ***p3;

Applying the & operator to a type is something completely different. It only works as the address-of operator when applied to an object. Specifically, the first line defines a reference to an int, the second line defines an rvalue reference to an lvalue (not legal), and the third line is a syntax error.

Narue 5,707 Bad Cop Team Colleague

A nested pointer **i is the same thing as a two dimesional array int i[][] .

No, that's wrong. Though pointers can be used to simulate arrays of varying dimensions, they're not the same thing.

Narue 5,707 Bad Cop Team Colleague

what is a not flat text file?

CSV is an example of a flat file. XML is an example of a non-flat file.

Narue 5,707 Bad Cop Team Colleague

Can I use my own code that I slapped a GPL on and provided to sourceforge in my own commercial application?

What does being commercial have to do with anything? The GPL doesn't stop you from charging money, it's all (mostly) about the openness of the source code. Ideally you would use the LGPL for a library that's both open source and used in the commercial application because it's easier to draw a clean line between licenses.

In that way, does the GPL somehow relinquish my own rights to my IP?

Yes. You could use the code to a certain extent and with sufficient changes not be legally bound by the GPL, but by placing the code under the GPL you've given up your right to make a verbatim copy under closed source.

However, prior art plays a part too. If you take code from a commercial application where you own the IP and release it as a new project under the GPL (assuming you're not violating the license of the commercial application), the commercial application isn't in violation of the GPL as long as you can prove that it existed prior to the GPL release. As for taking code from the commercial project that exists in the GPL project after both exist, you'd probably need to hire a lawyer to make sure that there's a legal leg to stand on. ;)