C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-04-2009, 08:46 PM   #1
UK2
 
Join Date: Sep 2003
Posts: 110
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++);
    }   
}
steve1_rm is offline   Reply With Quote
Old 06-04-2009, 08:58 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Hopefully you're not expecting the new_output array to be able to hold more than one element. Right?
tabstop is offline   Reply With Quote
Old 06-04-2009, 09:10 PM   #3
UK2
 
Join Date: Sep 2003
Posts: 110
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
steve1_rm is offline   Reply With Quote
Old 06-04-2009, 10:21 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Reply

Tags
pointers

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:56 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22