malloc ?
-
malloc ? I want to allocate a memory block (such as 150 bytes) with all chars are initialize with "A" this is my way: char* p; p=(char*)malloc(150) //if( p == NULL ) // errhanlder("Insufficient memory available"); free(p); // All okay but all chars are disorder, not "A" but when I reset all chars in my string to "A" ..... ErrMessageBox is shown char* p; p=(char*)malloc(150) //if( p == NULL ) // errhanlder("Insufficient memory available"); _strset('A'); // set all char to "A" free(p); // Error in this line, if delete this line, it runs okay Why when I try to free memory block, Error MessageBox show ? >> when I debug , I get info: // _msize(p) => return 150 but // strlen(p) => return 154 ??????? // I try to set p+150 = 0x0(nullchar) but Error still show // strcpy(p+150,0x0) help me~ and is there a way to create StrArray[a][b] with a, b are variable example : StrArray[1][1] point to a string "hello" StrArray[1][2] point to a string "bye" StrArray[2][1] point to a string "good" StrArray[2][2] point to a string "very long string ...." ........ thanks
-
malloc ? I want to allocate a memory block (such as 150 bytes) with all chars are initialize with "A" this is my way: char* p; p=(char*)malloc(150) //if( p == NULL ) // errhanlder("Insufficient memory available"); free(p); // All okay but all chars are disorder, not "A" but when I reset all chars in my string to "A" ..... ErrMessageBox is shown char* p; p=(char*)malloc(150) //if( p == NULL ) // errhanlder("Insufficient memory available"); _strset('A'); // set all char to "A" free(p); // Error in this line, if delete this line, it runs okay Why when I try to free memory block, Error MessageBox show ? >> when I debug , I get info: // _msize(p) => return 150 but // strlen(p) => return 154 ??????? // I try to set p+150 = 0x0(nullchar) but Error still show // strcpy(p+150,0x0) help me~ and is there a way to create StrArray[a][b] with a, b are variable example : StrArray[1][1] point to a string "hello" StrArray[1][2] point to a string "bye" StrArray[2][1] point to a string "good" StrArray[2][2] point to a string "very long string ...." ........ thanks
_strset has the form _strset(p, 'A'); and the string also needs to be NULL terminated...so try p[149] = '\0'; _strset(p, 'A'); Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
_strset has the form _strset(p, 'A'); and the string also needs to be NULL terminated...so try p[149] = '\0'; _strset(p, 'A'); Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
strcpy(p+150-1,"\0"); _strset(p,'A'); >> when I debug: _msize(p)=150 strlen(p)=149 and Error MsgBox isn't shown thank youuuu:rose:
-
strcpy(p+150-1,"\0"); _strset(p,'A'); >> when I debug: _msize(p)=150 strlen(p)=149 and Error MsgBox isn't shown thank youuuu:rose:
_skidrow_vn_ wrote: strcpy(p+150-1,"\0"); Per Check's suggestion on the MFC newsgroup, try:
*(p + 149) = '\0'; _strset(p, 'A');
-
strcpy(p+150-1,"\0"); _strset(p,'A'); >> when I debug: _msize(p)=150 strlen(p)=149 and Error MsgBox isn't shown thank youuuu:rose:
-
malloc ? I want to allocate a memory block (such as 150 bytes) with all chars are initialize with "A" this is my way: char* p; p=(char*)malloc(150) //if( p == NULL ) // errhanlder("Insufficient memory available"); free(p); // All okay but all chars are disorder, not "A" but when I reset all chars in my string to "A" ..... ErrMessageBox is shown char* p; p=(char*)malloc(150) //if( p == NULL ) // errhanlder("Insufficient memory available"); _strset('A'); // set all char to "A" free(p); // Error in this line, if delete this line, it runs okay Why when I try to free memory block, Error MessageBox show ? >> when I debug , I get info: // _msize(p) => return 150 but // strlen(p) => return 154 ??????? // I try to set p+150 = 0x0(nullchar) but Error still show // strcpy(p+150,0x0) help me~ and is there a way to create StrArray[a][b] with a, b are variable example : StrArray[1][1] point to a string "hello" StrArray[1][2] point to a string "bye" StrArray[2][1] point to a string "good" StrArray[2][2] point to a string "very long string ...." ........ thanks
Here is what I would do :
#define MY_BLOCK_SIZE 150
char *p = calloc( MY_BLOCK_SIZE, sizeof(char) );
if( p )
memset( p, 'A', MY_BLOCK_SIZE-1 );calloc allocated and zeros the block of memory. The Ten Commandments For C Programmers
-
_strset has the form _strset(p, 'A'); and the string also needs to be NULL terminated...so try p[149] = '\0'; _strset(p, 'A'); Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks
-
That won't work since you are hoping that bytes 0-148 are not NULL. memset (p, 'A', 149); p [149] = 0; Tim Smith I'm going to patent thought. I have yet to see any prior art.
You are correct...I was explaining why _strset was bombing. Gary Kirkham A working Program is one that has only unobserved bugs I thought I wanted a career, turns out I just wanted paychecks