Jump to content
  • Advertisement
Sign in to follow this  
sharpnova

Dynamically allocating memory for an array of strings

This topic is 4013 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
Advertisement
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 = (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 = (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 = (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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!