tags:

views:

102

answers:

2

Hello,

I seem to be getting a stack dump in my function where I am allocating memory.

I am passing an array of pointers '**output' to my function. And then I allocate enough memory to assign into that memory a string. However, I am getting a stack dump.

Many thanks for any suggestions,

void display_names(char **names_to_display, char **output);

int main(void)
{
    char *names[] = {"Luke", "John", "Peter", 0};
    char **my_names = names;
    char **new_output = 0;

    while(*my_names)
    {
     printf("Name: %s\n", *my_names++);
    }

    my_names = names; /* Reset */
    display_names(my_names, new_output);

    // Display new output
    while(*new_output)
    {
     printf("Full names: %s\n", *new_output++);
    }

    getchar();

    return 0;
}

void display_names(char **names_to_display, char **output)
{
    while(*names_to_display)
    { 
     // Stack dump here
     *output = (char*) malloc(sizeof("FullName: ") + strlen(*names_to_display)); // Allocate memory

     // Copy new output
     sprintf(*output, "FullName: %s", *names_to_display++);
     printf("display_names(): Name: %s\n", *output++);
    } 
}

======================== Updated ========================

void display_names(char **names_to_display, char **output);

int main(void)
{
    char *names[] = {"Luke", "John", "Peter", 0};
    char **my_names = names;
    char *new_output[] = {0};
    size_t i = 0;

    while(*my_names)
    {
        printf("Name: %s\n", *my_names++);
    }

    my_names = names; /* Reset */
    display_names(my_names, new_output);

    // Stack dump here.
    while(*new_output[i])
    {
        printf("Full names: %s\n", *new_output[i]);
     i++;
    }

    getchar();

    return 0;
}

void display_names(char **names_to_display, char **output)
{
    while(*names_to_display)
    {   
        *output = malloc(strlen("FullName: ") + strlen(*names_to_display) + 1); // Allocate memory

        // Copy new output
        sprintf(*output, "FullName: %s", *names_to_display++);
        printf("display_names(): Name: %s\n", *output++);
    }   
}
+4  A: 

You have numerous errors, but the main one is that you are passing aNULL pointer to display_names:

char **new_output = 0;   // null pointer
...
display_names(my_names, new_output);

display_names then dereferences it:

*output = (char*) malloc(sizeof("FullName: ") 
             + strlen(*names_to_display)); // Allocate memory

resulting in the creash.

In addition, the above allocation is not big enough - you want to add 1 for the end of string marker, and you never seem to initialise the allocated memory. Also, it is a bad habit to get into using sizeof on character strings - it will work in this instance, but you should always use strlen.

anon
Hello. I have updated my answer. However, I am not getting any stack dump allocating the memory. However, when I return from the function I get my stack dump. I have declared my output from **output to *output[]. I am not sure, but are both of them the same thing? Many thanks.
robUK
Yes, both are effectively the same thing. You need to give it a size: - something like char * output[OUTSIZE] where OUTSIZE is defined to be the size of the array - make this something big, like 1024, at least to get started.
anon
+1  A: 
jpinto3912