0

I have created the following test program:

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

char **messages = NULL;
int messageCount = 0;

void addMessage(char *message) {
    messages = realloc(messages, messageCount * sizeof(char *));
    if(messages == NULL) {
        printf("Unable to realloc");
        return;
    }
    messages[messageCount] = malloc(255 * sizeof(char));
    if(messages[messageCount] == NULL) {
        printf("Unable to alloc");
        return;
    }
    memset(messages[messageCount], '\0', 255);
    strncpy(messages[messageCount], message, 255);
    messageCount++;
}

void listMessages(void) {
    if(messageCount == 0) return;
    int i = 0;
    for(i = 0; i < messageCount; i++) {
        printf("%s\n", messages[i]);
    }
    printf("\n");
}

int main(void) {
    addMessage("This");
    listMessages();
    addMessage("is");
    listMessages();
    addMessage("a");
    listMessages();
    addMessage("test");
    listMessages();
    addMessage("for");
    listMessages();
    addMessage("dynamic");
    listMessages();
    addMessage("memory");
    listMessages();
    addMessage("allocation");
    listMessages();
}

It is pretty self explanatory as to what I am trying to do.

I get the following output before the program crashes:

This

This
is

This
is
a

This
is

Note that I am not receiving an "Unable to realloc" or an "Unable to alloc" message.

So why does it crash?

4
  • Where are you incrementing "messageCount"? Commented Aug 25, 2013 at 22:19
  • So I know how many messages I have, and thus know how much memory to allocate? Commented Aug 25, 2013 at 22:21
  • I meant that you're incrementing it after trying to write to memory allocated based on its value, rather than before....but I see that you've already accepted the answer pointing that out. Commented Aug 25, 2013 at 22:45
  • Ah, sorry I thought you said "Why are you..." Commented Aug 26, 2013 at 10:52

3 Answers 3

1

Here's what your code is doing:

  1. Calling addMessage with "This"
  2. Calling this function with messageCount = 0: messages = realloc(messages, messageCount * sizeof(char *));

And then continually writing into unallocated memory.

So, do a ++messageCount; at the top of addMessage and you can remove it from the bottom of the function.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice catch! This has been driving me mad; I kept the messageCount++ at the bottome, just did a realloc(messages, (messageCount + 1) * sizeof(char *)) instead. Works a treat.
1
  1. messageCount ++ before you alloc the memory.

  2. messages = realloc(messages, messageCount * sizeof(char *));

    if(messages == NULL) {

        printf("Unable to realloc");
        return;
    }
    messages[messageCount] = malloc(255 * sizeof(char));//here is wrong `
    

You alloc a memory of messageCount, the max index you can use is messageCount-1!

You need always use:

messages[messageCount-1] = malloc(...)

Also other place with message[messageCount] should replaced by messages[messageCount-1].

Or, you have a array overflow.

Comments

0

The problem: in the the first realloc, messageCount is zero. messageCount should be incremented before the call to realloc.

I had written before that messages being NULL the first time was the culprit, but it seems that's not really a problem (by @user814064).

1 Comment

Realloc() operates like this normally when passed a null pointer: pubs.opengroup.org/onlinepubs/7908799/xsh/realloc.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.