Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. malloc ?

malloc ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingperformancetutorialquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _skidrow_vn_
    wrote on last edited by
    #1

    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

    G R 2 Replies Last reply
    0
    • _ _skidrow_vn_

      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

      G Offline
      G Offline
      Gary Kirkham
      wrote on last edited by
      #2

      _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

      _ T 2 Replies Last reply
      0
      • G Gary Kirkham

        _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

        _ Offline
        _ Offline
        _skidrow_vn_
        wrote on last edited by
        #3

        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:

        D L 2 Replies Last reply
        0
        • _ _skidrow_vn_

          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:

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          _skidrow_vn_ wrote: strcpy(p+150-1,"\0"); Per Check's suggestion on the MFC newsgroup, try: *(p + 149) = '\0'; _strset(p, 'A');

          1 Reply Last reply
          0
          • _ _skidrow_vn_

            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:

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Do it like Gary told you, instead of strcpy use p[149] = '\0';

            1 Reply Last reply
            0
            • _ _skidrow_vn_

              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

              R Offline
              R Offline
              Rick York
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • G Gary Kirkham

                _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

                T Offline
                T Offline
                Tim Smith
                wrote on last edited by
                #7

                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.

                G 1 Reply Last reply
                0
                • T Tim Smith

                  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.

                  G Offline
                  G Offline
                  Gary Kirkham
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups