is this correct

M

Martin Dickopp

CBFalconer said:
Martin said:
I might have another use for the original pointer value, for example:

char *const p = malloc (len);
if (p != NULL)
{
/* image 150 lines of code here */
free (p);
}

This is a much different case from the incoming parameters example.
[...]
Remember, I am talking about function parameter values.

Yes. For consistency reasons, I treat parameters and local variables the
same, as far as `const' is concerned.
Should such a maintenance case occur, I suggest that:

1. The routine is too long.

I just /knew/ you were going to say that. :)
3. Now is the time to add the const to the parameter. If it
raises no flags on trial recompilation the original value hasn't
been touched. If it does, we can restore the original parameter
definition, add a const temporary to save the value at entry, and
use that later. In either case the const goes.

Actually, when I need to modify an object, but also need the original
value later, I prefer it the other way around: The parameter retains
its value und a temporary is modified.
Code diddling has been minimized.

I find maintenance easier if the const qualifier is there permanently,
instead of adding it, doing a trial recompilation, and then removing
it again.

Martin
 
D

Darklight

Martin Dickopp wrote:>
You don't allocate enough memory for `link'. You need the length of `a',
plus the length of `b', plus one for the space character, plus one for
the terminating '\0' character.

You should also check if `malloc' returns a null pointer.

Martin
so it should be like this
char *link = malloc(strlen(a) + strlen(b) + 2);

using the above function what would you change
baring in mind i do not have your level of experiance
the book i am using is sams c for linux programming
i am at the end of day nine
 
M

Martin Dickopp

Darklight said:
Martin Dickopp wrote:>

so it should be like this
char *link = malloc(strlen(a) + strlen(b) + 2);
Yes.

using the above function what would you change

What you /must/ change in order to have correct code is to verify that
`malloc' didn't return a null pointer:

char *join (char *a, char *b)
{
char *link = malloc (strlen (a) + strlen (b) + 2);
if (link != NULL)
{
strcpy (link, a);
strcat (link, " ");
strcat (link, b);
}

return link;
}

Martin
 
P

Peter Nilsson

Martin Dickopp said:
What you /must/ change in order to have correct code is to verify that
`malloc' didn't return a null pointer:

char *join (char *a, char *b)
{
char *link = malloc (strlen (a) + strlen (b) + 2);
if (link != NULL)
{
strcpy (link, a);
strcat (link, " ");
strcat (link, b);
}

return link;
}

I realise you're just correcting previously posted code, but why not do a simple...

sprintf(link, "%s %s", a, b);

....instead of using strcat which is likely to be a performance lag for large a?
 

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,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top