Thread: how to retain an allocated memory

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > already added 1 -> malloc(sizeof(strlen(src) + 1));
    No, it should just be strlen(src)+1

    Taking the sizeof that whole expression just reduces it to sizeof(size_t) or something like it.

    > **dest++ = *src++;
    Try something like
    char *d = *dest;

    Then use *d++ = *src++;

    What you DON'T want to do is actually move the pointer to your allocated memory.
    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.

  2. #17
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    Thanks Salem! That was actually helpful. Here's the working code below.
    PHP Code:
    [code]
    #include <stdio.h>
    #include <stdlib.h>

    void my_strcpy(char **destchar *src) {
        *
    dest = (char *) malloc(strlen(src) + 1);
        
        
    char *= *dest;
        while(*
    src) {
            *
    d++ = *src++;
        }
        *
    '\0';
    }

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

  3. #18
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Oh just don't forget to free() after you're done.

  4. #19
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    Finally wrote the function I wanted to make with help of pass-by-reference.

    The code below would allow to load a ASCII file content (hopefully with binary file too) into an uninitialized string.

    PHP Code:
    [code]
    #include <stdio.h>
    #include <stdlib.h>

    int flen(FILE *flen_file); //returns length of a file
    int fget(char **destFILE *fget_file); //returns all the content of a file
    int fload(char **dest, const char *file_name); //loads file_name into dest

    int main(void) {
        
    char *content;
        
        
    int err fload(&content"test.txt");
        
        if (
    err == 0) {
            
    printf("%s\n"content);
        }
        
    free(content);
        
        return 
    0;
    }

    int flen(FILE *flen_file) {
        
    int size;
        
        
    fseek(flen_file0SEEK_END);
            
    size ftell(flen_file);
        
    fseek(flen_file0SEEK_SET);
        
        return 
    size;
    }

    int fget(char **destFILE *fget_file) {
        
    int LOF flen(fget_file);
        
        *
    dest malloc(sizeof(char) * (LOF 1));
        
    char *= *dest;
        
    int i 0;
        while (
    LOF) {
            *
    d++ = fgetc(fget_file);
            
    i++;
        }
        *
    '\0';
        
        return 
    0;
    }

    int fload(char **dest, const char *file_name) {
        
    FILE *tmp_file fopen(file_name"rb");
        
        if (
    tmp_file != NULL) {
            if (
    fget(desttmp_file) == 0) {
                return 
    0;
            } else {
                return 
    2;
            }
        } else {
            return 
    1;
        }
    }
    [/
    code
    Now tell me those where created from random commands.

  5. #20
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    whoa, don't forget those who guided you from random crap to code.

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, 04:04 AM
  2. pass by reference the allocated memory in heap
    By nik2 in forum C Programming
    Replies: 4
    Last Post: 03-20-2010, 12:55 PM
  3. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  4. Check if there is memory allocated
    By The Wazaa in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2006, 05:48 AM
  5. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM

Tags for this Thread