*ptr = 0 what is the meaning

This is a discussion on *ptr = 0 what is the meaning within the C Programming forums, part of the General Programming Boards category; I'm very new to C and in this project, there's the following... Code: char *loc = NULL; char *tempname = ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    245

    *ptr = 0 what is the meaning

    I'm very new to C and in this project, there's the following...

    Code:
    char *loc = NULL;
    char *tempname = (char *) malloc(sizeof(char) * 255);
    tempname = strdup(name);
    char *oldname = strdup(name);
    int i;
    
    do
    {
    	for (i = 0; i < OPLEN; i++)
    	{
    	//checking operator OP[i]
    	        loc = strchr(oldname, OP[i]);
    		if (loc == NULL)
    			continue;
    		*loc = 0;
    		snprintf(tempname, 255, "%s%s%s", oldname, OP_REP[i], &loc[1]);
    		free(oldname);
    			oldname = strdup(tempname);
    		}
    	}
    while (loc != NULL);
    I'm unsure of the two bolded lines. it seems to me that the 2nd bolded line means "the address of the 2nd element in the dynamic array"..

    but I'm not sure how there can be such a value since we set what the pointer is pointing to to 0
    Last edited by dayalsoap; 09-17-2010 at 10:59 PM.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Well, from the code you posted, loc is not pointing to anything at all, so assigning 0 to *loc would be a bad thing to do. Did you weed out the assignment of an address to loc somewhere in "..... // bunch of irrelevant code"?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by kermit View Post
    Well, from the code you posted, loc is not pointing to anything at all, so assigning 0 to *loc would be a bad thing to do. Did you weed out the assignment of an address to loc somewhere in "..... // bunch of irrelevant code"?
    hmm i guess you're right. I've edited it to contain the rest of the code. sorry about that.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Here is the prototype for strchr():

    Code:
    char *strchr(const char *s, int c);
    "The strchr() function returns a pointer to the first occurrence of the character c in the string s"

    The pointer returned by strchr is assigned to the pointer loc. Then, 0 is assigned there. Presumably, this will terminate the string at that point. Consider the following:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char str[] = "Hello, world!";
        char *p = &str[5];
    
        puts(str);                  /* Print the whole string "Hello, world!" */
        *p = 0;                     /* Insert a 0 at str[5] */
        puts(str);                  /* Print the whole string "Hello" */
        puts(p + 1);                /* Move past the terminating null and print " world!" */
    
        return 0;
    }
    Last edited by kermit; 09-17-2010 at 11:12 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by kermit View Post
    Here is the prototype for strchr():

    Code:
    char *strchr(const char *s, int c);
    "The strchr() function returns a pointer to the first occurrence of the character c in the string s"

    The pointer returned by strchr is assigned to the pointer loc. Then, 0 is assigned there. Presumably, this will terminate the string at that point. Consider the following:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char str[] = "Hello, world!";
        char *p = &str[5];
    
        puts(str);                  /* Print the whole string "Hello, world!" */
        *p = 0;                     /* Insert a 0 at str[5] */
        puts(str);                  /* Print the whole string "Hello" */
        puts(p + 1);                /* Move past the terminating null and print " world!" */
    
        return 0;
    }
    Edit: Ah, i see what it's doing.
    Thanks for your help, btw.
    Last edited by dayalsoap; 09-17-2010 at 11:57 PM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > and in this project, there's the following...
    What exactly does "this project" mean?
    Is it your project, or just some random crap downloaded off the net which seems to do something you're interested in.

    I say crap, because the first bugs show up at the 2nd line of code.
    char *tempname = (char *) malloc(sizeof(char) * 255);
    tempname = strdup(name);

    Guess what happened to the 255 bytes - yup, they leaked away never to be seen again.

    > snprintf(tempname, 255, "%s%s%s", oldname, OP_REP[i], &loc[1]);
    255 huh?
    Well it was until the strdup, who knows how much is really available now.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    245
    No, it's in an open source project. It's called ADIOS:
    National Center for Computational Sciences ADIOS

    We're modifying it.. mostly other people, I just have a small part since i'm so new.

    Quote Originally Posted by Salem View Post
    > and in this project, there's the following...
    What exactly does "this project" mean?
    Is it your project, or just some random crap downloaded off the net which seems to do something you're interested in.

    I say crap, because the first bugs show up at the 2nd line of code.
    char *tempname = (char *) malloc(sizeof(char) * 255);
    tempname = strdup(name);

    Guess what happened to the 255 bytes - yup, they leaked away never to be seen again.

    > snprintf(tempname, 255, "%s%s%s", oldname, OP_REP[i], &loc[1]);
    255 huh?
    Well it was until the strdup, who knows how much is really available now.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    245
    I'm not sure I follow. Does the 255 leak away because "name" might not be 255 chars long?
    Quote Originally Posted by Salem View Post
    > and in this project, there's the following...
    What exactly does "this project" mean?
    Is it your project, or just some random crap downloaded off the net which seems to do something you're interested in.

    I say crap, because the first bugs show up at the 2nd line of code.
    char *tempname = (char *) malloc(sizeof(char) * 255);
    tempname = strdup(name);

    Guess what happened to the 255 bytes - yup, they leaked away never to be seen again.

    > snprintf(tempname, 255, "%s%s%s", oldname, OP_REP[i], &loc[1]);
    255 huh?
    Well it was until the strdup, who knows how much is really available now.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    245
    As you may know now, I'm not a computer scientist. We use these software packages for our scientific applications.. sometimes we have to hack away at them ourselves.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Quote Originally Posted by dayalsoap View Post
    I'm not sure I follow. Does the 255 leak away because "name" might not be 255 chars long?
    No, because you have a pointer with malloced memory, then you overwrite that pointer with some other memory value that is not from the same malloced area. Hence you've lost track of your allocated memory. It's gone. Leaked.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Consider a (hopefully) simpler example of what Salem & Elysia have pointed out:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        char *array;
        char my_string[] = "You just lost your malloced memory!";
    
        /* Get some memory allocated for "array" */
    
        array = malloc(128 * sizeof(*array));
        if (array == NULL) {
            fprintf(stderr, "%s", "malloc: Could not allocate memory.");
            exit(EXIT_FAILURE);
        }
    
        /* We got the memory, so now we can use it as was intended.  
         * Note that the only way to use it is by the pointer
         */
    
        strcpy(array, "My name is George.");
    
        puts(array);
    
        /* As you can see, the pointer stored in array is important, for without it, 
         * there would be no means to access the allocated memory.  Now consider what
         * happens when we make array point to something else:
         */
    
        array = my_string;
    
        puts(array);
    
        /* How do we go back now, and work on the memory allocated previously?  There is no longer
         * a way to do it.  This is especially troublesome, as now we cannot call free() to release
         * the allocated memory.  It is just hanging around, doing nothing.
         */
    
        return 0;
    }
    Note also, how I called malloc (in red); It is a good practice to call malloc in this manner. You can read why here. You could also have a look at the following entries from the C-FAQ:


  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by dayalsoap View Post
    I'm not sure I follow. Does the 255 leak away because "name" might not be 255 chars long?
    man malloc: The malloc() function allocates size bytes of memory and returns a pointer to the allocated memory.

    man strdup: The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it.

    So first, you are allocating memory for a string with malloc. Then you again allocate memory for a string, however, this is encapsulated in the strdup call. So, you are allocating memory twice.

    Then you overwrite the a pointer to the malloc allocated memory with the pointer to the strdup allocated memory, effectively making the malloc allocated memory inaccessible, meaning that you cannot free it, hence a memory leak.

    The code as posted by kermit has the right idea: if you malloc, then you strcpy. Either that, or use strdup without malloc. Remember to free the pointer in either case though.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by MWAAAHAAA View Post
    man malloc: The malloc() function allocates size bytes of memory and returns a pointer to the allocated memory.

    man strdup: The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it.

    So first, you are allocating memory for a string with malloc. Then you again allocate memory for a string, however, this is encapsulated in the strdup call. So, you are allocating memory twice.

    Then you overwrite the a pointer to the malloc allocated memory with the pointer to the strdup allocated memory, effectively making the malloc allocated memory inaccessible, meaning that you cannot free it, hence a memory leak.

    The code as posted by kermit has the right idea: if you malloc, then you strcpy. Either that, or use strdup without malloc. Remember to free the pointer in either case though.
    Ah, I see what you are saying. That explains it perfectly. Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!what is the meaning of "&" ???
    By zxlew in forum C Programming
    Replies: 2
    Last Post: 01-18-2009, 02:29 AM
  2. the meaning of " >> "
    By arian in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2005, 09:40 AM
  3. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 07:53 PM
  4. Replies: 8
    Last Post: 04-11-2003, 07:37 AM
  5. would you help me with Linked list, please?
    By unhwan in forum C Programming
    Replies: 1
    Last Post: 06-11-2002, 12:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21