Sign in to follow this  

Dynamically allocating memory for an array of strings

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been doing it statically like this: char X[30][40]; // I know there are 30 words to read from the file and none of them are longer than 40 chars and now i can access the fourth word with X[4] but what if i scan throgh the file and find out that there are 35 words now i have an integer "fileLength" that equals 35 how do i dynamically allocate the array using fileLength for 35 words? i know i have to use malloc but i'm not sure of the syntax. and no. this is not homework. i'm just trying to write a program that scans a file where i manually entered information about the periodic table, then it quizes me on it by picking random atomic numbers and asking me to give the name.

Share this post


Link to post
Share on other sites
Well you could create a matrix class and use it. A matrix is like a vector but it is a double vector. OR you could create a double pointer to character.

Share this post


Link to post
Share on other sites
how would i do it with a double pointer to char?

i have something like that already:

int fileLength = 35;
char **X;
X = (char**)malloc(fileLength);
for(int i=0; i<fileLength; ++i ) X[i] = (char*)malloc(50*sizeof(char));


i have no idea what i'm doing here to be honest.

is this the right way to do it?

it's hard for me to test. cuz most of my attempts are compiling and even working fine at runtime but i have no way of really knowing if i did it right (for example.. if i'm just getting lucky with not going out of the bounds of my assigned memory space)

EDIT: in the first malloc.. it gets seg fault so i fixed it to fileLength * 50.

so now my question is.. is there any redundancy here? i'm first saying that the size of X is going to be (amount of elements per array * size per element) but then in the for loop i allocate "size per element" for each element.. isn't this redundant?

[Edited by - sharpnova on June 14, 2007 3:32:25 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by sharpnova
how would i do it with a double pointer to char?

i have something like that already:

int fileLength = 35;
char **X;
X = (char**)malloc(fileLength);
for(int i=0; i<fileLength; ++i ) X[i] = (char*)malloc(50*sizeof(char));


i have no idea what i'm doing here to be honest.

is this the right way to do it?

it's hard for me to test. cuz most of my attempts are compiling and even working fine at runtime but i have no way of really knowing if i did it right (for example.. if i'm just getting lucky with not going out of the bounds of my assigned memory space)

EDIT: in the first malloc.. it gets seg fault so i fixed it to fileLength * 50.

so now my question is.. is there any redundancy here? i'm first saying that the size of X is going to be (amount of elements per array * size per element) but then in the for loop i allocate "size per element" for each element.. isn't this redundant?


X = (char**)malloc(fileLength*sizeof(char*));
replace ++i by i++

Share this post


Link to post
Share on other sites
Quote:
Original post by cheap freelancer
Quote:
Original post by sharpnova
how would i do it with a double pointer to char?

i have something like that already:

int fileLength = 35;
char **X;
X = (char**)malloc(fileLength);
for(int i=0; i<fileLength; ++i ) X[i] = (char*)malloc(50*sizeof(char));


i have no idea what i'm doing here to be honest.

is this the right way to do it?

it's hard for me to test. cuz most of my attempts are compiling and even working fine at runtime but i have no way of really knowing if i did it right (for example.. if i'm just getting lucky with not going out of the bounds of my assigned memory space)

EDIT: in the first malloc.. it gets seg fault so i fixed it to fileLength * 50.

so now my question is.. is there any redundancy here? i'm first saying that the size of X is going to be (amount of elements per array * size per element) but then in the for loop i allocate "size per element" for each element.. isn't this redundant?


X = (char**)malloc(fileLength*sizeof(char*));
replace ++i by i++


Ok I fixed my increment typo.

But why do I want it to be sizeof(char*)? Wouldn't that be allocating only "amount of words" * "4 bytes per word" for the whole array? each word is going to be more than four bytes.

Share this post


Link to post
Share on other sites
Is it because I am initially allocating "amount of words" * "size of the address of a character array" to X, then in the for loop i initialize each to be an array of 50 characters?

I think it just clicked. But please tell me if I'm wrong.

Btw, I've had this program completely functional from the start, just been trying to understand the workings better so I can make sure to only allocate the minimum memory necessary.

Share this post


Link to post
Share on other sites
Consider using a std::vector containing std::string objects. Then the whole thing would resize to fit automatically and you don't have to explicityly manage any memory.

Assuming you're using C++ of course.

Share this post


Link to post
Share on other sites
I'm only doing this to further my understanding of pointers and dynamic memory allocation. I'm doing it the hard way on purpose basically. I usually work in Java.

Share this post


Link to post
Share on other sites
If you really, honestly feel that doing it yourself, the hard way, is the only way you'll ever get a proper understanding, you could at least do yourself a favour and read this first. And this. Actually, probably all of this.

But even that doesn't explain everything you need to know about multidimensional arrays.

Share this post


Link to post
Share on other sites
Quote:
Original post by sharpnova
now i can access the fourth word with X[4]



I always had a problem with this and i think many people do. To access the fourth element of an array your supposed to write: X[3], since all arrays start from element #0 :) just thought id point that out because its frustrating to get those memory access violation errors and not knowing what the problem is.

Share this post


Link to post
Share on other sites
*sigh*

Sorry I meant X[3]. Or I meant the fifth word. It was just an oversight. I wouldn't make that kind of logical error in practice.

Zahlman, I didn't say I felt it was the only way.

I understand pointers and dynamic memory allocation. I was getting a little mentally tripepd up cuz there was a couple layers of indirection in the example I am working on so I was looking for a tip. I got the help I needed from cheap freelancer. My followup question (basically of the form 'is my understanding of this correct') was a simple yes or no question. And the answer was yes.

Share this post


Link to post
Share on other sites
Quote:
Original post by cheap freelancer
Quote:
Original post by sharpnova
how would i do it with a double pointer to char?

i have something like that already:

int fileLength = 35;
char **X;
X = (char**)malloc(fileLength);
for(int i=0; i<fileLength; ++i ) X[i] = (char*)malloc(50*sizeof(char));


i have no idea what i'm doing here to be honest.

is this the right way to do it?

it's hard for me to test. cuz most of my attempts are compiling and even working fine at runtime but i have no way of really knowing if i did it right (for example.. if i'm just getting lucky with not going out of the bounds of my assigned memory space)

EDIT: in the first malloc.. it gets seg fault so i fixed it to fileLength * 50.

so now my question is.. is there any redundancy here? i'm first saying that the size of X is going to be (amount of elements per array * size per element) but then in the for loop i allocate "size per element" for each element.. isn't this redundant?


X = (char**)malloc(fileLength*sizeof(char*));
replace ++i by i++


Please, as a favor to those that are beginners and are vulnerable to being led astray, consider learning the C/C++ language(s) properly before providing advice on the subject.

Share this post


Link to post
Share on other sites
Hmm.. His suggestion worked. Was there something wrong with the correction he made? (it's what i'm using now)

Share this post


Link to post
Share on other sites
No there was nothing wrong, you can check example given at kerningham and ritchie's c book.

//Removed spammish signature.

[Edited by - jbadams on October 27, 2007 6:31:16 AM]

Share this post


Link to post
Share on other sites
The correction regarding malloc was correct.

And, technically, the correction regarding the increment wasn't wrong in itself, but the initial code wasn't wrong to begin with either so no reason to change it. Just unecessary confusion added for no use at all.

Share this post


Link to post
Share on other sites

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this