Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Problem Allocating Strings in a Dynamic Array of Strings
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
araisbec




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
chrisbrown




PostPosted: 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.
araisbec




PostPosted: 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?
chrisbrown




PostPosted: 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.
DtY




PostPosted: 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]Wink 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)
araisbec




PostPosted: 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
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: