Is correct ?

L

lasek

Hi all,
i have a trouble understanding this:

example:

#include....

char * testA(void);

char * testB(void);

char * testC(void);




int main(void)
{
char * acMsg=NULL;
....

strcpy(acMsg,testA());

strcpy(acMsg,testB());

strcpy(acMsg,testC());

return 0;
}

char * testA(void)
{
char * acMsg=NULL;

acMsg=malloc((14+1)*sizeof(char));

strncpy(acMsg,"THIS IS A TEST,14);

return acMsg;

// free???
}

char * testB(void)
{
char acMsg[]="THIS IS A TEST";

// this isn't correct (i hope)becaust i try to
return a local address(after this call i
lost the address of 'acMsg')

return &acMsg[0];
}

char * testC(void)
{
char acMsg[]="THIS IS A TEST";
char *acMsg_B=NULL;

// this isn't correct becaust i try to
return a local address(after this call i
lose thi variable address)(??)

acMsg_B=acMsg;

return acMsg_B;

// free??
}


Now...one of these examples are correct...because i feel confuse..

Thanks all and have i nice day.
 
I

infobahn

lasek said:
Hi all,
i have a trouble understanding this:

example:

#include....

char * testA(void);

char * testB(void);

char * testC(void);

int main(void)
{
char * acMsg=NULL;
....

strcpy(acMsg,testA());

Undefined behaviour. acMsg doesn't point to sufficient storage
to store 15 bytes.
strcpy(acMsg,testB());

Undefined behaviour. acMsg doesn't point to sufficient storage
to store 15 bytes, and testB's return value is the address of
a local object that no longer exists by this point in the code.
strcpy(acMsg,testC());

Undefined behaviour. acMsg doesn't point to sufficient storage
to store 15 bytes, and testC's return value is the address of
a local object that no longer exists by this point in the code.

return 0;
}

char * testA(void)
{
char * acMsg=NULL;

acMsg=malloc((14+1)*sizeof(char));

strncpy(acMsg,"THIS IS A TEST,14);

You forgot the closing "
return acMsg;

// free???

No, don't free the space until you've finished with it.
 
L

lasek

..Sorry i've forgot to allocate memory for
'acMsg' and for missing '"' for strncpy(acMsg,"THIS IS A TEST,14);
but i want to know if the returned value is correct, and when i must call
'free' if the pointer(testA) is allocated into a the same function.
 
I

infobahn

lasek said:
.Sorry i've forgot to allocate memory for
'acMsg' and for missing '"' for strncpy(acMsg,"THIS IS A TEST,14);
but i want to know if the returned value is correct, and when i must call
'free' if the pointer(testA) is allocated into a the same function.

Let's try to keep this as simple as possible - clc experts please
note that I am trying to be helpful here, not necessarily "complete".

lasek: I know you want to know about free(), but I think it would be
a good idea to start at the beginning! :) Your question does get
answered in this article, I promise.

When you want to allocate memory dynamically, you call malloc.
You tell malloc how much memory you want, and it returns either
NULL (which means "sorry, no can do") or a pointer to sufficient
storage to meet your request.

When using malloc, you must remember to:

#include <stdlib.h>

When (and only when) you have finished with the storage, you give
the memory back by calling free(), passing the pointer value that
you were originally given.

The best way to call malloc is like this:

p = malloc(n * sizeof *p);

which, if it succeeds, allocates sufficient storage to store
n objects of the appropriate size (i.e. the size of an object
of the kind to which p points). The only time this doesn't work
is when p is of type void *, in which case you need only do
p = malloc(n) instead, to get n bytes of storage.

When should you free this memory? ONLY after you have finished
with it. So, in the case where you do something like:

char *sduplicate(const char *s)
{
char *p = malloc(strlen(s) + 1) * sizeof *p);
if(p != NULL)
{
strcpy(p, s);
}
return p;
}

you should NOT free(p) within this function; to do so would destroy
the point of having the function in the first place! The code that
calls this function takes (and might pass along to another function)
the responsibility for freeing the space...

char *s = sduplicate("Hello, world!");
if(s != NULL)
{
reverse(s); /* do something to the copy */
printf("%s\n", s); /* display */
/* ... whatever ... */

/* we have now finished with the storage, so we can call free() */
free(s);
}


If you call malloc within a function and then decide that you no
longer require the storage, you can call free within the same
function. But if you do so, DON'T try to use that storage again.

I hope that answers your question.
 
C

Christopher Benson-Manica

lasek said:
char * testA(void)
{
char * acMsg=NULL;
acMsg=malloc((14+1)*sizeof(char));
strncpy(acMsg,"THIS IS A TEST,14);
return acMsg;
// free???

No, but...
strcpy(acMsg,testA());

....after strcpy() is called, you've lost the pointer that testA
returned, meaning that you no longer have the ability to free that
memory. You want something like

char *tmp=testA();
strcpy( acMsg, tmp ); /* assuming malloc() succeeded - you should check */
free( tmp );

Think about this in relation to infobahn's article :)
 
I

infobahn

Christopher said:
...after strcpy() is called, you've lost the pointer that testA
returned, meaning that you no longer have the ability to free that
memory.

Nice spot. There's always one more chocolate in the bottom of the box.
 

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
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top