how to retain an allocated memory

This is a discussion on how to retain an allocated memory within the C Programming forums, part of the General Programming Boards category; I'm trying to make my own strcpy . A version of strcpy that will allow this, PHP Code: [ code ...

  1. #1
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17

    how to retain an allocated memory

    I'm trying to make my own strcpy. A version of strcpy that will allow this,
    PHP Code:
    [code]
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main(void) {
        
    char *content1;
        
    char *content2 "hello world";
        
        
    printf("%s\n"content2);
        
        
    strcpy(content1content2);
        
        
    printf("%s\n"content1);
        
        return 
    0;
    }
    [/
    code
    Of course the app crashes upon strcpy attempt because content1 doesn't have memory allocated in it. It's why I wrote this function.
    PHP Code:
    [code]
    void my_strcpy(char *dest, const char *src) {
        
    dest = (char *) malloc(sizeof(strlen(src)));
        
        while(*
    src) {
            *
    dest++ = *src++;
        }
        *
    dest '\0';
    }
    [/
    code
    where I was hoping my_strcpy(content1, content2); would work as expected. But no. content1 is NULL;

    How do I retain the allocated memory in my_strcpy's dest parameter?

    One solution is to make my_strcpy a non-void function.
    PHP Code:
    [code]
    char *my_strcpy(char *dest, const char *src) {
        
    dest = (char *) malloc(sizeof(strlen(src)));
        
    char *destcpy dest;
        
        while(*
    src) {
            *
    dest++ = *src++;
        }
        *
    dest '\0';
        
        return 
    destcpy;
    }
    [/
    code
    But I would have to use content1 = my_strcpy(content1, content2);, and not as simple as my_strcpy(content1, content2); like strcpy does.

    How do I pass-by-reference and retain allocated memory?
    Last edited by creek23; 06-24-2010 at 10:40 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    dest is a local variable, so any changes you make to it will not be visible outside the function. Make it a double pointer instead if you want to change the value of the pointer.

    Code:
    void my_strcpy(char **dest, char *src)
    {
        ...
        *dest = malloc(...);
    }
    Last edited by Memloop; 06-24-2010 at 05:45 AM.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Try passing char** dest and allocating for *dest.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    In C, you'll have to use the suggestions provided earlier. In C++ you could call by reference.

    However the function makes no sense at all. Why not use "strdup" for that?

  5. #5
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    Quote Originally Posted by EVOEx View Post
    However the function makes no sense at all. Why not use "strdup" for that?
    I'm actually not after the replication of the functionality of strcpy nor strdup. I'm after the passing-by-reference in C.

    Quote Originally Posted by EVOEx View Post
    In C, you'll have to use the suggestions provided earlier. In C++ you could call by reference.
    Exactly how do I do that? I'm getting segmentation fault with this test.
    PHP Code:
    [code]
    void my_strcpy(char **dest, const char *src) {
        *
    dest malloc(sizeof(strlen(src)));
        
        while(*
    src) {
            **
    dest++ = *src++;
        }
        **
    dest '\0';
    }

    int main(void) {
        
    char *content1;
        
    char *content2 "hello world";
        
        
    printf("%s\n"content2);
        
        
    my_strcpy(&content1content2);
        
        
    printf("%s\n"content);
        
        return 
    0;
    }
    [/
    code

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This line is not even close to being right:
    Code:
    dest++ = *src++;

  7. #7
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    I actually just forgot the **. edited the post, please check again.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Code:
    malloc(sizeof(strlen(src)));
    You're not allocating enough space for the terminating NUL character. strlen does not count it.

  9. #9
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    Quote Originally Posted by Memloop View Post
    Code:
    malloc(sizeof(strlen(src)));
    You're not allocating enough space for the terminating NUL character. strlen does not count it.
    already added 1 -> malloc(sizeof(strlen(src) + 1));

    still the same. app crashes.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    This line is wrong:
    Code:
            **dest++ = *src++;
    It may be easier to write the pointer to a temporary "char*" before you play with it. Otherwise, you'll get a line like:
    Code:
            *(*dest)++ = *src++;

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    There's no passing by reference in C.
    You can simulate it by use of pointer.
    You cannot just straightaway code in C without learning it.
    Read a good tutorial/book on C.

  12. #12
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    Quote Originally Posted by Bayint Naung View Post
    There's no passing by reference in C.
    You can simulate it by use of pointer.
    Saying there is none and saying you can simulate it is a contradicting statement. Just as saying there no pointers in Java but you can fake one.

    Of course, I'm aware that C uses pointers to fake passing by reference. "reference" is actually synanymous to "pointer".

    Quote Originally Posted by Bayint Naung View Post
    You cannot just straightaway code in C without learning it.
    Read a good tutorial/book on C.
    It's not like I put random codes like:
    PHP Code:
    [code]
    int char * () {
      include <&var> = 
    "helloworld";
      echo **var;
    }
    [/
    code
    Of course, I'm reading some reference and tutorials. But much of what I want to know are very specific and most of the tutorials and books are discussing very general thought. Like when I wanted to know how to write resizeable array of resizeable strings, it will be very hard to find a tutorial that discuss that specific topic. It's why I'm asking in this forum for help. And I'm thankful that there are people responding to my inqueries.

    (I may sound like I'm ranting but I'm not )

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It's not like I put random codes like:
    I see no difference

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by creek23 View Post
    Saying there is none and saying you can simulate it is a contradicting statement. Just as saying there no pointers in Java but you can fake one.

    Of course, I'm aware that C uses pointers to fake passing by reference. "reference" is actually synanymous to "pointer".


    It's not like I put random codes like:
    PHP Code:
    [code]
    int char * () {
      include <&var> = 
    "helloworld";
      echo **var;
    }
    [/
    code
    Of course, I'm reading some reference and tutorials. But much of what I want to know are very specific and most of the tutorials and books are discussing very general thought. Like when I wanted to know how to write resizeable array of resizeable strings, it will be very hard to find a tutorial that discuss that specific topic. It's why I'm asking in this forum for help. And I'm thankful that there are people responding to my inqueries.

    (I may sound like I'm ranting but I'm not )
    Actually neither C nor Java are "faking" anything. They are both pass-by-value languages.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by creek23 View Post
    Of course, I'm aware that C uses pointers to fake passing by reference. "reference" is actually synanymous to "pointer".
    Ultimately, ALL forms of "pass by reference" (in any language) use this method -- a reference is the location of a data structure. Logically, there is no other way to do this.

    So pass-by reference vs. value discourse is somewhat chimeric and rhetorical. It's essentially about normative nomenclature. In C it's "fake", since a pointer is a distinct datatype of it's own, whereas a reference (which C doesn't have) is more just a syntactical method used in other languages (and many of those, nb, use interpreters written in C, so C can be used to implement references, they are just not part of the syntax).

    I wanted to know how to write resizeable array of resizeable strings, it will be very hard to find a tutorial that discuss that specific topic.
    You need to learn about memory allocation and re-allocation -- malloc(), realloc(), free(). And get comfy with pointers. At that point it will be self-explanatory.
    Last edited by MK27; 06-24-2010 at 10:24 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 05:04 AM
  2. pass by reference the allocated memory in heap
    By nik2 in forum C Programming
    Replies: 4
    Last Post: 03-20-2010, 01:55 PM
  3. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 11:48 PM
  4. Check if there is memory allocated
    By The Wazaa in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2006, 06:48 AM
  5. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 05:14 PM

Tags for this Thread


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