return string from c++ to c

  • Thread starter Michael Rasmussen
  • Start date
M

Michael Rasmussen

Hi all,

Due to input from you I ended up with the solution shown below.

==00:00:00:04.137 7613==
==00:00:00:04.137 7613== ERROR SUMMARY: 130 errors from 7 contexts (suppressed: 17 from 1)
==00:00:00:04.171 7613== malloc/free: in use at exit: 0 bytes in 0 blocks.
==00:00:00:04.171 7613== malloc/free: 37 allocs, 37 frees, 9,839 bytes allocated.
==00:00:00:04.171 7613== For counts of detected errors, rerun with: -v
==00:00:00:04.199 7613== No malloc'd blocks -- no leaks are possible.

Errors mentioned is caused by Valgrind not able to handle an incomplete
struct - typedef struct Foo Foo.

Thank you all for usefull input.

In my class I added this method:
const char *get_cstring(const std::string &) const;

const char *Iwconfig::get_cstring(const std::string &s) const
{
return s.c_str();
}

Every time I need to return a string as const char * I then do this:
return Foo->get_cstring(s);
 
M

Michael Rasmussen

That's poor practice: you're asking for advice on some code A you don't
show, and show something else, B, instead, pretending it is A.
The question asked had nothing to do with proper initializing the struct.
It was strictly a mather of how to return a string from a c++ class as a
(const) char * to a C program.
Did you try 'lint'?
I do not have any program called lint available under Debian.
If not, and you absolutely don't want to do that (or cannot do that),
check the line that that comment applied to. There are two function
calls. One of them lacks an argument.

I have a program called splint. Is that the same?
I show among other thing this:
gui/main.c:61:39: New fresh storage (type char *) passed as implicitly temp
(not released): getsettings(iwconfig)
A memory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)

Is that what you were refering to?
 
A

Alf P. Steinbach

* Michael Rasmussen:
The question asked had nothing to do with proper initializing the struct.
It was strictly a mather of how to return a string from a c++ class as a
(const) char * to a C program.

I don't know what the omitted code was, and neither can anyone else but
you. That's the point. Code that isn't the code is ungood for
discussing the code.

I do not have any program called lint available under Debian.

Typing "debian lint" into Google immediately produced the names lintian,
linda and lclint.

I have a program called splint. Is that the same?

Looks like it.

I show among other thing this:
gui/main.c:61:39: New fresh storage (type char *) passed as implicitly temp
(not released): getsettings(iwconfig)
A memory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)

Is that what you were refering to?

No, I referred to the call

char *get_string(struct Foo *foo);

...
get_string()

Hth.,

- Alf
 

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

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top