LinuxQuestions.org
Visit Jeremy's Blog.
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 01-02-2009, 04:04 PM   #1
bmdavll
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
C - incremental memory allocation


I'm trying to write a string concatenator that (re)allocates memory, but I'm getting a glibc error on free().

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *stracat(char *t, const char *s)
{
    if (t == NULL)
        if ((t = (char *) malloc(strlen(s)+1)) != NULL)
            *t = '\0';
    else
        t = (char *) realloc(t, strlen(t)+strlen(s)+1);
    if (t != NULL)
        strcat(t, s);
    return t;
}

int main(void)
{
    char *p;

    p = NULL;
    p = stracat(p, "123456789");
    p = stracat(p, "ABCDEF");

    printf("%s\n", p);
    free(p);

    exit(0);
}
The output with the error:

Code:
123456789ABCDEF
*** glibc detected *** a.out: free(): invalid next size (fast): 0x0804a008 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e01a85]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7e054f0]
a.out[0x8048583]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7dac450]
a.out[0x8048441]
======= Memory map: ========
...
The error disappears if the second call to stracat has fewer characters, or if stracat was implemented in the following way, so I'm assuming the problem is with the way I'm using realloc().

Code:
    char *p = (char *) malloc((t == NULL ? 0 : strlen(t)) + strlen(s) + 1);
    if (p != NULL)
    {
        strcpy(p, (t == NULL ? "" : t));
        strcat(p, s);
    }
    free(t);
    return p;
I'm using Ubuntu with glibc 2.7. Any ideas on how to correct it would be great, thanks.
 
Old 01-02-2009, 04:10 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by bmdavll View Post
Code:
    if (t == NULL)
        if ((t = (char *) malloc(strlen(s)+1)) != NULL)
            *t = '\0';
    else
First error: You're missing {} around the nested if. It should be
Code:
    if (t == NULL)
        {
        if ((t = (char *) malloc(strlen(s)+1)) != NULL)
            *t = '\0';
        }
    else
 
Old 01-02-2009, 04:55 PM   #3
bmdavll
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Original Poster
Rep: Reputation: 0
That fixed the problem, thanks.
 
  


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
memory allocation esael Linux - General 7 01-12-2008 12:12 PM
memory allocation gecoool Programming 2 10-24-2005 09:47 AM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:24 PM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:22 PM
memory allocation. raven Programming 5 09-08-2002 01:50 PM

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

All times are GMT -5. The time now is 07:03 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