delete char * in structure

My code keeps giving debug assertion error..
Here is my code..


#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"
struct Test
{
char *value;
}x;
int main(int argc, char* argv[])
{
printf("Hello World!\n");
x.value = (char*)malloc(sizeof("HelloWorld"));
x.value="HelloWorld";

cout<<"After allocation and assignment"<<endl;


free(x.value);

return 0;
}


The problem is it throws a _crtIsValidHeapPointer Debug assertion error.
Please help me to know how to delete the char * pointer inside the structure.
1
2
3
4
5
x.value = (char*)malloc(sizeof("HelloWorld")); //make x.value point to a memory block allocated by malloc
x.value="HelloWorld"; //now make it point somewhere else - not allocated by malloc


free(x.value);//now try and free the memory that was NOT  allocated by malloc - ERROR 



I didnt get you..Then how else can i initialize the memory allocated using the malloc?

Please tell the correct way to delete the memory..
x.value = (char*)malloc(strlen("HelloWorld") + 1);
Try including string.h and instead of using:
x.value = "HelloWorld";

Use this:
strcpy(x.value, "Hello World");
Now I have solved the early problem ..but For array of structures..
#define numberPoints 50
struct configStruct
{
char *pointName;
uint2 type;
}conf[numberPoints];

/*This code is executed as many times as number of values in file ,,ie all the pointNames may not be initialised*/
conf[configStructCounter].pointName=(char*)malloc(sizeof(token)); /*token value is read from file*/
strcpy(conf[configStructCounter].pointName,token);

/*Now trying to delete the values*/
for(int delCount=0;delCount<numberofValuesInFile;delCount++)
{
free(conf[delCount].pointName);
}


But here the program throws abort retry ignore screen...
Is this C or C++, if you're using C++ you can add class methods to make this safe. There's no point discussing it futher without knowing the language you're using.
What I am using is c++ only. Right now i am not having any class in the program.
Last edited on
I wrote a sample program for you.. here it is below.. It is running without error on VS 2005

#include <iostream>
#include <string>

#define numberPoints 50

struct configStruct
{
char *pointName;
int type;
}conf[numberPoints];

int main()
{
char* token = "HelloWorld";
for(int i = 0; i < numberPoints; ++i)
{
conf[i].pointName=(char*)malloc(sizeof("HelloWorld")+1);
strcpy(conf[i].pointName,token);
}

/*Now trying to delete the values*/
for(int delCount=0;delCount<numberPoints;delCount++)
{
free(conf[delCount].pointName);
}
return 0;
}
Thanks guys,

I figured out the problem.

char *token=strtok(str," ");
conf[configStructCounter].pointName=(char*)malloc(sizeof(token)); /*This was the problem*/

I was allocating memory only for token ie 4 bytes, but i needed memory for the entire array.
That is why it crashed while deleting.


Thanks behlkush..What i was doing was like , if in your program sizeof("Helloworld") was replaced by sizeof(token)..that was the problem.
Last edited on
You're asking for trouble by putting code like that into a program. configStruct should be responsible for the allocation and release of its own resources, not the user of configStruct.
hi kbw ,
i didnt understand what you are saying. how can a structure be responsible for the release of its resources.This is not C++ class.
This is not C#. Class syntax also apply to struct in C++. You can add methods as you would a class. In practice and syntax there isn't a difference between the two. There's only a semantic difference.
It can still have a destructor.

1
2
3
4
5
struct Test
{
    char *value;
    ~Test() { delete value; }
}x;


In C++ you should be using new and delete rather than malloc and free.
Topic archived. No new replies allowed.