Passing char* not working

A

Ankit

Hello All,
I have code like this:
Class2 Class1::func (char* response)
{
printf( response);
Class2* d_ = new Class2;
d_->func (response);
//print response within Class2->func prints the response
return d_;
}

Snippet of Class2:
Class2 {
//constructor & destructor
void func(char*);
};

Above code works perfectly well on Windows. But not working in
Solaris
x86 v10g. with CC compiler 12with patch.
On solaris if I try to print the response within Class2, it doesnt
prints the content of char* passed as input parameter.
 
A

Alf P. Steinbach

* Ankit, on 21.05.2010 18:46:
Hello All,
I have code like this:
Class2 Class1::func (char* response)
{
printf( response);

Use a format string.

Class2* d_ = new Class2;
d_->func (response);
//print response within Class2->func prints the response
return d_;
}

Snippet of Class2:
Class2 {
//constructor& destructor
void func(char*);
};

Above code works perfectly well on Windows. But not working in
Solaris
x86 v10g. with CC compiler 12with patch.
On solaris if I try to print the response within Class2, it doesnt
prints the content of char* passed as input parameter.

The code above does not work with any compiler.

The denizens of this group are not telepaths: we /do not/ "see" your real code.

See the FAQ about how to post questions about code that doesn't work.


Cheers & hth.,

- Alf
 
A

Ankit

Basically if I pass char* from a Class to an object of another class
(as a part of input parameter to func) and is not received. Attempt to
print the char* in the received function on another class, prints
empty character.
 
A

Ankit

I am printing the received argument in the first line of
implementation.

I tried with simple example as following:excluding A.h and B.h
A.cpp
#include "A.h"
A::A () {}
A::~A (void) {}

void A::parse (char* str)
{
printf (str);
B b;
b.parse (str);
}

int main(int argc, char* argv[])
{
B b;
char* resp = b.get ();
A ac;
ac.parse (resp);
}

//B.cpp
#include "B.h"


B::B () {}
B::~B (void) {}

char* B::get ()
{
return "This is a long story";
}

void B::parse (char* str)
{
printf (str);
}

This works.

But exactly this withing large code base of mine doesn't works. It
works on windows but on solaris when we print (similar to printing
within function parse of B) it doesnt prints anything. And that print
line is first line.

regards,
Ankit
 
F

Fred

I am printing the received argument in the first line of
implementation.

I tried with simple example as following:excluding A.h and B.h
A.cpp
#include "A.h"
A::A () {}
A::~A (void) {}

void A::parse (char* str)
{
  printf (str);
  B b;
  b.parse (str);

}

int main(int argc, char* argv[])
{
  B b;
  char* resp = b.get ();
  A ac;
  ac.parse (resp);

}

//B.cpp
#include "B.h"

B::B () {}
B::~B (void) {}

char* B::get ()
{
  return "This is a long story";

}

void B::parse (char* str)
{
  printf (str);

}

This works.

But exactly this withing large code base of mine doesn't works. It
works on windows but on solaris when we print (similar to printing
within function parse of B) it doesnt prints anything. And that print
line is first line.

regards,
Ankit

Ankit ha scritto:
Since it is simply not possible for a function not to "receive"
something you pass to it, the error is in the code you did not show us.
For example, func() might do something bad with your char*. Of course,
I'm sure the code would work as expected if you just used std::string
instead of error-prone char*.

The string you print does not end with a newline. There is no
guarantee that anything will show up unless you flush stdout, or end
it with a newline (\n).
 
A

Ankit

I appended with "\n" everywhere. Still same issues.


I am printing the received argument in the first line of
implementation.
I tried with simple example as following:excluding A.h and B.h
A.cpp
#include "A.h"
A::A () {}
A::~A (void) {}
void A::parse (char* str)
{
  printf (str);
  B b;
  b.parse (str);

int main(int argc, char* argv[])
{
  B b;
  char* resp = b.get ();
  A ac;
  ac.parse (resp);

//B.cpp
#include "B.h"
B::B () {}
B::~B (void) {}
char* B::get ()
{
  return "This is a long story";

void B::parse (char* str)
{
  printf (str);

This works.
But exactly this withing large code base of mine doesn't works. It
works on windows but on solaris when we print (similar to printing
within function parse of B) it doesnt prints anything. And that print
line is first line.

The string you print does not end with a newline. There is no
guarantee that anything will show up unless you flush stdout, or end
it with a newline (\n).
 
P

Paul Bibbings

Ankit said:
I am printing the received argument in the first line of
implementation.

I tried with simple example as following:excluding A.h and B.h

Please do not exclude A.h and B.h. Provide complete, compilable code
that illustrates the exact nature of the problem you are encountering.
Having said that, see below.
A.cpp
#include "A.h"
A::A () {}
A::~A (void) {}

void A::parse (char* str)
{
printf (str);
B b;
b.parse (str);
}

int main(int argc, char* argv[])
{
B b;
char* resp = b.get ();
A ac;
ac.parse (resp);
}

//B.cpp
#include "B.h"


B::B () {}
B::~B (void) {}

char* B::get ()
{
return "This is a long story";
}

void B::parse (char* str)
{
printf (str);
}

This works.

But exactly this withing large code base of mine doesn't works.

Then essentially the issue here is that, in distilling the above code
example from your 'large code base', you have not actually captured that
part of the code where the error occurs. Until you can do this - and,
in doing so, provide a *complete* example that readers of this group can
compile to witness the same error - it seems hardly possible to imagine
how help can be given.

Regards

Paul Bibbings
 
A

Ankit

//A.h
#ifndef _A_
#define _A_

#include "B.h"
#include <vector>
#include <map>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace std;
class A
{
public:
A (void);
~A (void);

void parse (char* );

};

#endif /* ifndef */


//B.h
#ifndef _B_
#define _B_

#include <vector>
#include <map>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace std;
class B
{
public:
B (void);
~B (void);

void parse (char* );

char* get ();
};

#endif /* ifndef */


This code works correctly (as expected) on both Windows as well as
Solaris.

But similar code part of my code base is not working on Solaris.
When within A (function parse)
void A::parse (char* str)
{
printf (str);
B b;
b.parse (str);
}

And now when B's parse operation is called, and it tries to print the
parameter passed to it, is unable to.
 
A

Ankit

What I am telling is that this example (that I have provided with
complete source code) works on both Windows as well as Solaris and
gives expected result. I verified it.

But similar code (part of my big code base) is not giving expected
result on Solaris.
 
P

Paul Bibbings

Ankit said:
What I am telling is that this example (that I have provided with
complete source code) works on both Windows as well as Solaris and
gives expected result. I verified it.

So the code that we have seen "works"...
But similar code (part of my big code base) is not giving expected
result on Solaris.

....while the code that we *haven't* seen, doesn't.

Are you perhaps wanting us merely to agree with you, or to commiserate
with you?

As has been pointed out already, until we can see your code failing,
there is really little to do here.

Regards

Paul Bibbings
 
P

Paul Bibbings

Leigh Johnston said:
"printf(response);" is potentially unsafe if "response" is obtained
via user input (so can incorrectly contain format specifiers) which
could result in a crash. I was hit by this very problem the other
month (not printf but strftime).

For an outside viewpoint, I took Pete's "There's nothing wrong with
printf(response) if response has no format specifiers" to have already
precluded the scenario that you are presenting here in argument against
him. Effectively, with your "so can incorrectly contain format
specifiers," you are merely introducing back what is already excluded by
the claim (Pete's) as given.

Regards

Paul Bibbings
 
P

Puppet_Sock

This code works correctly (as expected) on both Windows as well as
Solaris.
[snip]

Lessee here. You want us to debug code that does not
work based on different code that does. That's special.

When you have code that fails, it's not really possible
to debug it by giving other folks a *similar* code snip.

You need to isolate the bug and show code that does
*not* work in the way that your code does not work.
Chances are excellent that in so doing you will
yourself uncover the bug.

When a pointer is messing up the chances are excellent it's
one of "the usual suspects." Things like off-by-one, or
dangling pointers, or over-written pointers, or stack
erorrs, or various other similar things. Do the usual
debug things to find the bug and kill it.

And consider dispensing with char* in favour of std::string.
Socks
 
I

Ian Collins

On 05/22/10 08:17 AM, Ankit wrote:

Please:

a) stop top-posting and
b) take the time to distil a compilable example the demonstrates your
problem.
 
P

Paul Bibbings

Puppet_Sock said:
And consider dispensing with char* in favour of std::string.

Or, at the very least, use const char* and skip all the "deprecated
conversion" warnings.

Regards

Paul Bibbings
 
R

red floyd

Or, at the very least, use const char* and skip all the "deprecated
conversion" warnings.

In addition, he should use legal include guards. _A_ and _B_ are
reserved to the implementation.

By the way Ankit, you have an error on line 42 of your code (the
version that we haven't seen).
 
D

Default User

This code works correctly (as expected) on both Windows as well as
Solaris.
But similar code part of my code base is not working on Solaris.

That's like having a problem with your car, and taking a different one to
the mechanic.




Brian
 
J

James Kanze

Because it will work in Windows and not on Solaris? :)
printf("A string\n");
will work because the first argument is a quoted string,
essentially a format specification.
char *astring = "A string\n");
printf(astring); // non-portable

Where do you get that from? The two are completely identical,
as far as the standard is concerned.

Note that in practice, you almost never use a string constant as
the format specifier. Something like:
printf(gettext("A string"));
is far more frequent.
 
J

James Kanze

"printf(response);" is potentially unsafe if "response" is
obtained via user input (so can incorrectly contain format
specifiers) which could result in a crash. I was hit by this
very problem the other month (not printf but strftime).

Any use of printf is potentially unsafe. In C++, there are
better alternatives. But in practice, I've not seen many
applications in C which pass string constants to printf. The
format string almost always comes from a file somewhere
(directly or indirectly). And yes, it can lead to some very
hard to diagnose errors---the program only fails when executing
in the Finnish local, and all you have to guide you are messages
in Finnish. (Which is fine if you speak Finnish, but that's not
my case.)
 
I

Ian Collins

Note that in practice, you almost never use a string constant as
the format specifier. Something like:
printf(gettext("A string"));
is far more frequent.

Eh? I don't think I've ever *not* used a string constant as the format
specifier for printf.
 
D

Default User

Pete Becker said:
Yes, but this thread has been restarted on comp.std.c++, so maybe there's
a better mechanic over there.

That sounds like taking the working car to a car designer and asking for
help repairing the broken one.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top