Skip to main content
gamedev.net
🔒 Locked

dynamic memory allocation..

Started by krad7 1 reply 700+ views
Original Post
krad7
krad7
I am an experienced C programmer and i have clear understanding in cpp.. lately i have been having problems with dynamic memory allocation, that too double pointers.. in C, its easy to use, using malloc.. how do i do it in CPP?? (using new not malloc) for example.. char **str; in C i use one malloc to allocate an array of pointers and then use a loop to allocate the individual pointers. how do i do the same in CPP using new operator? i tried doing str = new char[x][y], but it didnt work, it gave me some weird errors (i am using vc++ .net).. please tell me how do i allocate memory using ''new'' in cpp.. thanks in advance..
Slow and steady wins the race.
Warden
Warden
hmm... that's not really a good place to use a double pointer. You're making a two dimentional array, which is not, in anyway, shape or for, a double pointer. Try:

char *str;
str = new char[x][y];
then address as str[r][c]

or if that doesn't work try

char *str;
str = new char[x * y];
then address as str[r * x + c]

assuming c and r are row / collumn positions in an array.

[edited by - Warden on June 13, 2002 11:54:25 AM]

[edited by - Warden on June 13, 2002 11:55:23 AM]
-Warden

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.