Jump to content
  • Advertisement
Sign in to follow this  
gp343

Memset and Multidimensional pointers

This topic is 3784 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

Hi, I'm trying to learn to properly use multidimension pointers. For some reason it is not creating the sub arrays. Can somebody please help on this - Multidimensional Pointers problems?
void TestMultiDim()
{
	char** mArray;
	int rows = 50;
	char* p;
	char* q;
	char* data = "John Doe";
	size_t len;
	len = rows * sizeof(char*);
	mArray = (char **)malloc(len);
	if(mArray == NULL)
	{
		exit(EXIT_FAILURE);
	}

	memset(mArray, 0, rows);

	//Create the sub arrays
	for(p = mArray; *p!='\0'; p++)
	{
		*p = malloc(len);
		if(*p == NULL)
		{
			exit(EXIT_FAILURE);
		}
		memset(*p, 1, len);
	}

	//Populate Data
	for(p = mArray; *p !='\0'; p++)
	{
		*p = data;
	}

	//Print out List
	for(p = mArray; *p !='\0'; p++)
	{
		printf("%s\n", *p);
	}

}



Thanks, Jr

Share this post


Link to post
Share on other sites
Advertisement
For starters iterate through every position of your array like so:


for(p = mArray; p!=(mArray+rows); ++p)
{
....
}







You're also not populating your data correctly.


//Populate Data
for(p = mArray; p!=(mArray+rows); ++p)
{
*p = (char*)malloc(strlen(data)+1);
assert(*p);
strcpy(*p, data);
}




Note there is no need to repeatedly call memset. However if you find you actually need your allocation initialized call calloc instead.

[Edited by - fpsgamer on June 14, 2008 12:35:36 AM]

Share this post


Link to post
Share on other sites
Ok Guys, I've got it to work however, it is now failing on freeing up the resource that I created with malloc. The current problem now is how to I correctly free this without getting the heap corruption errors in Visual Studio?

void TestMultiDim()
{
char** mArray;
int rows = 50;
char* p;
char* q;
int i;
char* data = "John Doe";
size_t len;
len = rows * sizeof(char*);
mArray = (char **)malloc(len);
if(mArray == NULL)
{
exit(EXIT_FAILURE);
}

//Create the sub arrays
//for(p = mArray; *p!='\0'; p++)
for(i = 0; i < rows; i++)
{
//*p = (char*)malloc(len);
mArray = (char*)malloc(len);
//if(*p == NULL)
if(mArray == NULL)
{
exit(EXIT_FAILURE);
}
//memset(*p, 0, len);
}

//Populate Data
//for(p = mArray; *p !='\0'; p++)
for(i = 0; i < rows; i++)
{
mArray = data;
//*p = data;
}

//Print out List
//for(p = mArray; *p !='\0'; p++)
for(i = 0; i < rows; i++)
{
printf("%s\n", mArray);
}


//Clean Up:
for(i = 0; i < rows; i++)
{
free(mArray);
mArray = NULL;
}
free(mArray);

}

Share this post


Link to post
Share on other sites
The problem is you're attempting to delete a pointer that points to a string literal.

Also the condition you're using to terminate iteration of your loop is undefined. Heap allocated buffers do not end with a NULL terminator.

You should re-read all the stuff I described in my earlier post.

Share this post


Link to post
Share on other sites
Are you trying to allocate a list of strings?

char** createStringArray (int count, const char* string)
{
char** strs = new char*[strings];
assert (strs);

// allocate each string
size_t len = strlen (string) + 1;
for (int i = 0; i < strings; i++) {
strs = new char[len];
assert (strs);
strcpy (strs, string);
}

return strs;
}

void deleteStringArray (char** strings, int count)
{
for (int i = 0; i < count; i++) {
// delete the strings
delete [] strings;
}
// delete the string pointer array
delete [] strings;
}

void setStringInArray (char** strings, int index, char* new_string)
{
// since this code bases buffer length on string length get both
size_t idxlen = strlen (strings[index]);
size_t newlen = strlen (new_string);

// if not the same length reallocate to shorter or longer length
if (newlen != idxlen) {
delete [] strings[index];
strings[index] = new char[newlen+1];
}
strcpy (strings[index], new_string);
}


But this is a bad way of having lists of strings anyway. Instead, use std::vector<std::string>

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!