Monday, May 24, 2010

C++ Segmentation Fault?

Here is my code:


#include %26lt;iostream%26gt;


using namespace std;





typedef struct {


char* name; /* '\0'-terminated C string */


int num;


} SomeStruct;





void allocSpace(SomeStruct **);





int main ()


{


char * b, q, *r;


SomeStruct **arr;


allocSpace(arr);


for (int i=0;i%26lt;5;i++)


cout%26lt;%26lt;arr[i]-%26gt;num;


return 0;


}





void allocSpace(SomeStruct **x)


{


x = (SomeStruct**) malloc(sizeof(SomeStruct *) * 5);


for (int i=0 ; i%26lt;5 ; i++)


{


x[i] = new SomeStruct;


x[i]-%26gt;num = i;


cout%26lt;%26lt;x[i]-%26gt;num;


}


}

C++ Segmentation Fault?
Use new not malloc,


if it still does not work, may be you can contact a C++ expert at websites like http://getafrerelnacer.com/

umbrella plant

No comments:

Post a Comment