views:

423

answers:

6

I have a old program in which some library function is used and i dont have that library.

So I am writing that program using libraries of c++. In that old code some function is there which is called like this

*string = newstrdup("Some string goes here");

the string variable is declared as char **string;

What he may be doing in that function named "newstrdup" ? I tried many things but i dont know what he is doing ... Can anyone help

A: 

newstrdup is probably making a new string that is a duplicate of the passed string; it returns a pointer to the string (which is itself a pointier to the characters).

MarkusQ
Weird, why wouldn't it just return a pointer to the characters?
@Iraimbilanja: it appears to be.
sfossen
A: 

It looks like he's written a strdup() function to operate on an existing pointer, probably to re-allocate it to a new size and then fill its contents. Likely, he's doing this to re-use the same pointer in a loop where *string is going to change frequently while preventing a leak on every subsequent call to strdup().

I'd probably implement that like string = redup(&string, "new contents") .. but that's just me.

Edit:

Here's a snip of my 'redup' function which might be doing something similar to what you posted, just in a different way:

int redup(char **s1, const char *s2)
{
    size_t len, size;

    if (s2 == NULL)
     return -1;

    len = strlen(s2);
    size = len + 1;

    *s1 = realloc(*s1, size);

    if (*s1 == NULL)
     return -1;

    memset(*s1, 0, size);
    memcpy(*s1, s2, len);

    return len;
}

Of course, I should probably save a copy of *s1 and restore it if realloc() fails, but I didn't need to get that paranoid.

Tim Post
+4  A: 
Johannes Schaub - litb
i tried ur program like this.i called newstrdup like this.*ersponse = newstrdup(buffer) where buffer ="chaithra"but while running it is giving as segmentation fault.
Chaithra
Chaithra, have you tried both versions?
Johannes Schaub - litb
no litb i tried only first version. now i ll try other one.
Chaithra
then i think you should add a null check: if str is NULL, just return NULL right away. because the only significant difference between the second and the POSIX strdup version is the check for null (beside the name, of course). would be a good reason for them to provide their own version.
Johannes Schaub - litb
ok.thanks its working if i call as response = newstrdup("some string"); where response is char*but if response is char ** and if i call*response=newstrdup("some string");it is segmentation fault. may be i am not allocating memory thanks anyway
Chaithra
Chaithra, if course, you have to make your pointer point to somewhere. like char *c; char **p = then you can do *p = newstrdup.... just doing char **p; *p = newstrdup... will write to some uncertain memory location and it will crash if you are lucky (or go silently wrong if you are not lucky)
Johannes Schaub - litb
if it is working if you do char *c = newstrdup... then the function works fine. the bug then is with making the char** point to somewhere sensible, of course.
Johannes Schaub - litb
A: 

I think you need to look at what is happening with the "string" variable within the code as the prototype for the newstrdup() function would appear to be identical to the library strdup() version.

Are there any free(*string) calls in the code?

It would appear to be a strange thing do to, unless it's internally keeping a copy of the duplicated string and returning a pointer back to the same string again.

Again, I would ask why?

James Fisher
ya, I was thinking the same thing, there has to be some corner case it's handling differently :)
sfossen
+3  A: 

there has to be a reason that they wrote a "new" version of strdup. So there must be a corner case that it handles differently. like perhaps a null string returns an empty string.

litb's answer is a replacement for strdup, but I would think there is a reason they did what they did.

If you want to use strdup directly, use a define to rename it, rather than write new code.

sfossen
yep, since it is called "new"strdup, i think it uses operator new instead of malloc (strdup uses malloc). they may also have their own for portability reasons. i don't know :)
Johannes Schaub - litb
It may also be called newstrdup() simply because strdup() is not standard, although it is widely available, and they wanted to stay clear. The use of 'newstrdup()' to indicate 'strdup-variant-using-new' is also plausible.
Jonathan Leffler
strdup() built in function is okay. but when i call the function using *response = newstrdup("Some String") i am getting segmentation fault.what may be he reason for this and how to overcome?
Chaithra
Can you post a snippet of the working code and of your testcases? Without more context, it's hard to say right now. Also, do you have the compiled library, since reversing it is also an option.
sfossen
A: 

The line *string = newstrdup("Some string goes here"); is not showing any weirdness to newstrdup. If string has type char ** then newstrdup is just returning char * as expected. Presumably string was already set to point to a variable of type char * in which the result is to be placed. Otherwise the code is writing through an uninitialized pointer..

R..