Memset and Multidimensional pointers

Started by
5 comments, last by thre3dee 15 years, 10 months ago
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
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 Datafor(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]
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);

}
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.
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>

This topic is closed to new replies.

Advertisement