Dynamic memory allocation problem

This is a discussion on Dynamic memory allocation problem within the C Programming forums, part of the General Programming Boards category; Originally Posted by anduril462 The new code seems to do what it's supposed to, but it's still a bit messy. ...

  1. #16
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Wink

    Quote Originally Posted by anduril462 View Post
    The new code seems to do what it's supposed to, but it's still a bit messy. Here is what I envisioned (a bit cleaner I think):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int     c,
                i,
                size = 0;
        char    *buf = NULL,
                *temp;
    
        printf("Enter String: ");
    
        do {
            c = getchar();
            temp = realloc(buf, size+1);
            if (!temp) {
                printf("No space in heap!\n");
                return 1;
            }
    
            buf = temp;
    
            if (c == '\n') {
                buf[size++] = '\0';    // we're done, null terminate the string
            }
            else if (c != EOF) {
                buf[size++] = c;
            }
        } while(c != EOF && c != '\n');
    
        for (i = 0; i < size; i++) {
            putchar(buf[i]);
        }
    
        free(buf);
    
        return 0;
    }
    Your code is nice ......There is also another bug in my code ....If the first chara is '\n' it will print it twice.......... ...Also I don't check for EOF ...Your code is better than mine Thankuuuuuuuuuuu!!!

  2. #17
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Quote Originally Posted by anduril462 View Post
    The new code seems to do what it's supposed to, but it's still a bit messy. Here is what I envisioned (a bit cleaner I think):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int     c,
                i,
                size = 0;
        char    *buf = NULL,
                *temp;
    
        printf("Enter String: ");
    
        do {
            c = getchar();
            temp = realloc(buf, size+1);
            if (!temp) {
                printf("No space in heap!\n");
                return 1;
            }
    
            buf = temp;
    
            if (c == '\n') {
                buf[size++] = '\0';    // we're done, null terminate the string
            }
            else if (c != EOF) {
                buf[size++] = c;
            }
        } while(c != EOF && c != '\n');
    
        for (i = 0; i < size; i++) {
            putchar(buf[i]);
        }
    
        free(buf);
    
        return 0;
    }
    Your code is nice ..........There are two more bugs in my code.....If the first chara is '\n' it will be printed twice.................. I don't check for EOF....


    Thank u for u r code........

    I didn,t understand what you say about my realloc ... please clarify........

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Doh! I guess I left that thought unfinished. I said H/2 - 1, but in retrospect, it should be floor(H/2) bytes. What I was getting at is this:

    If H is the maximum size of the heap in bytes, you can only ever have a string that is floor(H/2) bytes in length (counting the null). If you allocate n bytes, then reallocate n+1 bytes, the computer must allocate the n+1, copy everything and free the old n bytes. That means you always need room for a second copy of what you have (plus the extra byte you're asking for). Lets take an example:

    If your heap (H) is 1000 bytes and your current string is 499 bytes, when you ask to realloc 500 bytes, the computer leaves your 499 bytes alone, allocates 500 bytes (we're now using 999 bytes), copy the data and free the old 499 bytes. Now lets pretend your string is 500 bytes. You ask to realloc 501 bytes, but that would require a total of 1001 bytes on the heap (too many), so the realloc fails. You can't get beyong floor(H/2) bytes if you realloc(p, size+1).

    It's true that realloc may try to simply extend the end of your allocated memory segment, instead of allocating a second, bigger chunk and copying, but that behavior is not guaranteed. Besides, the allocation/garbage collection algorithms are probably OS dependent, so you can't count on there being more floor(H/2) bytes available.

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

Similar Threads

  1. Structs and dynamic memory allocation
    By PaulCocaine in forum C Programming
    Replies: 3
    Last Post: 11-22-2010, 10:14 AM
  2. Find Size Of Dynamic Memory Allocation?
    By appleGuy in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2007, 10:34 AM
  3. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 09:09 PM
  4. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 02:07 AM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM

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