Thread: Array of pointers - stack dumping

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Array of pointers - stack dumping

    Hello,

    I have a function that I would like to copy the contents off the pointer array into the output pointer array (and change the contents). However, it works, but I get a stack dump when I try and display the contents in the main function.

    Many thanks for any suggestions,


    Code:
    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++);
        }   
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Hopefully you're not expecting the new_output array to be able to hold more than one element. Right?

  3. #3
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    I thought the code below would copy the contents of each element in the array. I declare memory with enough space and use sprintf to copy the new contents over.

    Code:
    while(*names_to_display)
    	{	
    		// Stack dump here
    		*output = malloc(sizeof("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++);
    	}
    The output in the main function should be:
    FullName: Luke
    FullName: John
    FullName: Peter

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your code does attempt to set new_output[0], new_output[1], and new_output[2]. Since new_output[1] completely fails to exist, it is unable to do so. (You cannot realloc an array, which you aren't attempting to do in this code admittedly.) new_output is defined as an array of length 1, which it will therefore always and forever be.

    Either your new_output array needs to be long enough to start with, or you need to make that dynamically-sized as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 06:30 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Array Stack Problem
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2001, 06:58 PM

Tags for this Thread