Problem Allocating Strings in a Dynamic Array of Strings
Author |
Message |
araisbec
|
Posted: Tue Mar 02, 2010 1:41 pm Post subject: Problem Allocating Strings in a Dynamic Array of Strings |
|
|
Alright, I'm currently attending University of Guelph for computer science, and this is part of our second assignment. We were given a number of functions to create to accomplish specific tasks using dynamic memory (malloc()), and I am having some issues with storing strings in a dynamic array that I've created (The .exe crashes every time it gets to the same point). I am allocating memory using this function:
code: |
char ** createStringArray(int size){
char *strArray [size];
for (int i = 0; i < size; i++){
strArray [i] = malloc (sizeof (char)*15);
}
return strArray;
}
|
Passing the size of the array to the int size parameter. I am then storing values in the memory using this function:
code: |
void setStringArray(char **strArray, int pos, char *str){
strcpy (strArray [pos], str);
}
|
I believe the problem may lie in the second storing function, as that is when the .exe seems to crash every time. Any ideas? Thanks. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Tue Mar 02, 2010 1:58 pm Post subject: RE:Problem Allocating Strings in a Dynamic Array of Strings |
|
|
code: | char *strArray[size]; |
This creates an array on the stack, which is cleared** when the function returns. What you need to do is create this array on the heap (dynamic memory) as well.
Hints: you only need to change one line; use malloc to allocate the same amount of space that strArray uses as you've defined it.
**Edit: the memory is not cleared, but the values are not guaranteed to be what they are expected to be.
In short, if a function returns a pointer, that pointer better be the result of a malloc call. |
|
|
|
|
![](images/spacer.gif) |
araisbec
|
Posted: Tue Mar 02, 2010 6:10 pm Post subject: Re: Problem Allocating Strings in a Dynamic Array of Strings |
|
|
I'm not sure if I understand... I am aware of how stacks and the heap work, but we were specifically instructed to malloc the memory for the array in the function... does this mean that the memory is being allocated on the stack, and the pointer being returned is pointing to it? |
|
|
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Tue Mar 02, 2010 6:44 pm Post subject: Re: Problem Allocating Strings in a Dynamic Array of Strings |
|
|
araisbec @ Tue Mar 02, 2010 6:10 pm wrote: does this mean that the memory is being allocated on the stack, and the pointer being returned is pointing to it?
Correct, as it is written now. Each element of your array points to a heap address, which is correct, but the location of the array itself is on the stack. |
|
|
|
|
![](images/spacer.gif) |
DtY
![](http://compsci.ca/v3/uploads/user_avatars/8576159234be48b7a8b0e8.png)
|
Posted: Tue Mar 02, 2010 6:56 pm Post subject: RE:Problem Allocating Strings in a Dynamic Array of Strings |
|
|
In addition, that line (char *strArray [size] is not valid C, size has to be a compile time constant to create an array like that. Some compilers support creating dynamic arrays like that, but it is not guaranteed to work, it should be
c: | char **strArray = malloc(size * sizeof(int*)); |
(This is the same solution as methodoxx is describing, but for a different reason) |
|
|
|
|
![](images/spacer.gif) |
araisbec
|
Posted: Thu Mar 04, 2010 1:50 pm Post subject: Re: Problem Allocating Strings in a Dynamic Array of Strings |
|
|
Aha! Thank you all, I was confused as to how to allocate the memory for the array as well as what it's pointers pointed at, but now I understand and I have it working ![Very Happy Very Happy](http://compsci.ca/v3/images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|