On Tue Nov 22, 2005 at 13:26:24 +1100, ashley maher wrote:
>G'day,
>
>I know this is not even C 101 level but would some kind soul please
>explain to me why this is not even close to working.


So you are basicalyl missing what pointers mean. char * is
a pointer to some memory that can hold a bunch of chars. It
is not an actual string. I try to explain further:

>
>int somefunction(char *string1)
>{

So this is some function that gets pointer to some memory. It
gets a *copy* of an agument of the stack, and shoves it in a 
local variable called string1. string1 is a pointer to some memory.

>        char *string2 = "some words\0";

Here you allocate variable string2, and point it to some memory with the data 
"some words".
(b.t.w: you don't need \0).

>        string1 = (char *)calloc(strlen(string2 + 1), sizeof (char));

Here you allocate zome memory from the heap, specifically enough to a 
copy of the memory pointed to by string2. In doing so it redefines what
ever value was passed in to the function. Oh wait, even more subtle than that, 
I missed it. You actually allocate 2 less than need to store string2, becuase
you actually take the length of string2 + 1, which is the length of the
string "ome words", which is 2 less than need to store null terminated
string2.

>        strcpy(string1, string2);

It now copies the contents of the memory pointed to by string2 into
the new memory pointed to by string1. (Note, no need to use calloc,
you could have just used malloc, since you just overwrote all that memory
anyway.)

>        return 0;

Up to here you haven't done anything that is visible globally or to the
calling function, exception allocate some memory on the heap. And now
since you return you have created a memory leak, because there is no
longer any reference to string1.

>}
>
>
>int main ()
>{
>        char *string;

Ok, this decalres a string point. That is a pointer to some memory.
But since you didn't initialise it, it currently point to some random
place in memory.

>        somefunction(string);

You call this function that doesn't modify the string at all.

>        printf ("\n\nString is: %s\n\n", string);

And then you print out some random data.

>        free (string);

And then you try and free something not previously allocated by
malloc, so it dies in a heap.

>        return 0;
>}
>

I suggest compiling with -Wall, which will point out a lot of these
errors before you execute the code.

Another style point is that it is generally consider bad to allocate memory
in one function and free it in another.

I suspect the closest thing to what you wanted to do here would be:

int somefunction(char **string1)
{
 /* a function that takes a pointer to a pointer to memory */
        char *string2 = "some words";
        
        *string1 = malloc(strlen(string2) + 1);

        /* allocate memory and assign it to the char pointer, to which string1 
points
          to. Known as dereferencing. */

        if ((*string1) == NULL) /*SHould at least check return value! */
             return -1;

        strcpy(*string1, string2);
        /* Copy the string */

       return 0;
}


int main(void) {
   char * string;
   int r;

   r = somefunction(&string);
   /* Pass the address of string to somefunction */
        
   if (r != 0) {
        fprintf(stderr, "Couldn't allocate memory\n");
        return 1;
   }

    /* now we know that string actually points somewhere */
    printf("String is %s\n", string);


   free(string);
  return 0;
}


Of course thatisn't really the correct way to write it either, that would depend
on what you are actually trying to achieve which I can't grok from the code.

Benno
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to