![]() |
| | #1 |
| UK2 Join Date: Sep 2003
Posts: 110
| Array of pointers - stack dumping 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 | |
| | #2 |
| and the Hat of Guessing 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 | |
| | #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++);
}
FullName: Luke FullName: John FullName: Peter |
| steve1_rm is offline | |
| | #4 |
| and the Hat of Guessing 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 | |
![]() |
| Tags |
| pointers |
| Thread Tools | |
| Display Modes | |
|
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 |