LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2011, 01:31 PM   #1
orion9114
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Rep: Reputation: Disabled
next size error in realloc()


hi guys, im going crazy for this problem!!
Code:
char* read_s(FILE* fs) {
    int mem = 20;
    char* str = malloc (mem * sizeof(char));
    int i = 0, r;
    char c;
    while ((r = fread(&c, sizeof(char), 1, fs)) != 0 &&  c != '\n') {
        str[i] = c;
        i++;
        if (i > mem - 1) {
            mem += mem/2;
            str = realloc(str, sizeof(char) * mem);
        }
    }
    if (r == 0)
        return NULL;
    return str;
}
this is my code it's a simple function that read a line of a file, if the line is longer than allocated memory for buffer i do a realloc on that buffer, but at the second iteration realloc goes wrong, and i don't know how to fix it!!
thank u guys!!

Last edited by orion9114; 11-07-2011 at 02:30 PM.
 
Old 11-07-2011, 02:54 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
I do not observe that to be the case; the program works as you have specified. I tried it with the following test code:

Code:
int main(int argc, char *argv[]) {
    char *string = NULL;
    FILE *filep = NULL;

    if (argc != 2 || argv == NULL || argv[1] == NULL) {
        return EXIT_FAILURE;
    }   

    if (NULL == (filep = fopen(argv[1], "r"))) {
        return EXIT_FAILURE;
    }   

    if (NULL == (string = read_s(filep))) {
        fclose(filep);
        return EXIT_FAILURE;
    }   

    printf("%s\n", string);

    return EXIT_SUCCESS;
}
 
Old 11-07-2011, 02:58 PM   #3
orion9114
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
meybe it works becouse realloc is invoked just 1 time! in fact it crash the second time it invoke realloc with this message:
crypt: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
 
Old 11-07-2011, 04:11 PM   #4
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
A few more thoughts.

(1) When you pass in a FILE * to a zero-byte file, r == 0 happens. At this point, you have a reference to some allocated memory str, which never gets freed. You should add free(str) to that if() statement:

Code:
    if (r == 0) {
        free(str);
        return NULL;
    }
(2) Also, since you're allocating memory in read_s(), you need to make sure that you free the memory elsewhere in the program. Basically, when you're done with whatever you're doing, free() the output of read_s().

As a final note in that vein, run your program through gdb and valgrind.
 
1 members found this post helpful.
Old 11-07-2011, 05:12 PM   #5
orion9114
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jhwilliams View Post
A few more thoughts.

(1) When you pass in a FILE * to a zero-byte file, r == 0 happens. At this point, you have a reference to some allocated memory str, which never gets freed. You should add free(str) to that if() statement:

Code:
    if (r == 0) {
        free(str);
        return NULL;
    }
(2) Also, since you're allocating memory in read_s(), you need to make sure that you free the memory elsewhere in the program. Basically, when you're done with whatever you're doing, free() the output of read_s().

As a final note in that vein, run your program through gdb and valgrind.
thank you very much!! now it works it's alway free's fault -.-
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] problem with realloc(): invalid next size golden_boy615 Programming 2 07-19-2011 05:24 AM
Won't get realloc to work - glibc: invalid old size ruh31 Programming 5 06-08-2011 05:39 PM
realloc error on Topas kennynoah AIX 3 08-19-2008 04:33 PM
*** glibc detected *** ./input: realloc(): invalid next size: custode Programming 4 05-04-2007 03:25 PM
Error::*** glibc detected *** realloc(): topworld Programming 2 04-26-2006 01:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration