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. Newbie Question on C Pointers

Newbie Question on C Pointers

Scheduled Pinned Locked Moved C / C++ / MFC
questionjsonperformancetutorialcode-review
6 Posts 3 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
    _NielsB
    wrote on last edited by
    #1

    Hello, I try to understand memory management and pointers in C on a simple level right now. Here is the task: concatenate 2 buffers (in this case buffA & buffB to buffAB). The values of the following code-snipets are small for debugging purposes ... imagine the size of each source buffer is much larger: how can I propper clean up the "mess" after creating buffAB? // buffA char buffA[] = {'a'}; char* p_buffA = &buffA[0]; // buffB char buffB[] = {'b','b'}; char* p_buffB = &buffB[0]; // buffAB char buffAB[] = {'x','x','x'}; char* p_buffAB = &buffAB[0]; // cpy memcpy(p_buffAB, p_buffA, 1); // copy buffA to buffAB memcpy(p_buffAB+1, p_buffB, 2); // copy buffB to buffAB // buffAB is the only thing we want ... how to clean up the rest? I am thankfull for all suggesting to improve the code.

    C C 2 Replies Last reply
    0
    • _ _NielsB

      Hello, I try to understand memory management and pointers in C on a simple level right now. Here is the task: concatenate 2 buffers (in this case buffA & buffB to buffAB). The values of the following code-snipets are small for debugging purposes ... imagine the size of each source buffer is much larger: how can I propper clean up the "mess" after creating buffAB? // buffA char buffA[] = {'a'}; char* p_buffA = &buffA[0]; // buffB char buffB[] = {'b','b'}; char* p_buffB = &buffB[0]; // buffAB char buffAB[] = {'x','x','x'}; char* p_buffAB = &buffAB[0]; // cpy memcpy(p_buffAB, p_buffA, 1); // copy buffA to buffAB memcpy(p_buffAB+1, p_buffB, 2); // copy buffB to buffAB // buffAB is the only thing we want ... how to clean up the rest? I am thankfull for all suggesting to improve the code.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      since you didn't dynamically allocate anything (with new or malloc, etc), there's nothing to clean up.

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • _ _NielsB

        Hello, I try to understand memory management and pointers in C on a simple level right now. Here is the task: concatenate 2 buffers (in this case buffA & buffB to buffAB). The values of the following code-snipets are small for debugging purposes ... imagine the size of each source buffer is much larger: how can I propper clean up the "mess" after creating buffAB? // buffA char buffA[] = {'a'}; char* p_buffA = &buffA[0]; // buffB char buffB[] = {'b','b'}; char* p_buffB = &buffB[0]; // buffAB char buffAB[] = {'x','x','x'}; char* p_buffAB = &buffAB[0]; // cpy memcpy(p_buffAB, p_buffA, 1); // copy buffA to buffAB memcpy(p_buffAB+1, p_buffB, 2); // copy buffB to buffAB // buffAB is the only thing we want ... how to clean up the rest? I am thankfull for all suggesting to improve the code.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        _NielsB wrote:

        // buffAB is the only thing we want ... how to clean up the rest?

        clean up is called free in C language and works on dynamic allocated memory, i.e. it should pair a previous call to malloc (or another allocation function, like calloc). For instance:

        char * pB = malloc(2);
        pB[0]='b';
        pB[1]='b';
        /* or *pb='b'; *(pb+1)='b'; */
        /* or memset(b, (int)'b', 2); */
        char * pA = malloc(1);
        pA[0]='a'; /* same options b has */
        char * PAB = malloc(3);
        memcpy(pAB, pA, 1 );
        memcpy(pAB+1, pB, 2);
        free(pA);
        free(pB);

        Note that I've omitted (to keep code compact) malloc return value check, but you must always do it in real programming, i.e.

        char *p=malloc(10);
        if ( ! p )
        {
        /* handle error */
        }
        /* allocation was fine, go on

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        [my articles]

        _ 1 Reply Last reply
        0
        • C CPallini

          _NielsB wrote:

          // buffAB is the only thing we want ... how to clean up the rest?

          clean up is called free in C language and works on dynamic allocated memory, i.e. it should pair a previous call to malloc (or another allocation function, like calloc). For instance:

          char * pB = malloc(2);
          pB[0]='b';
          pB[1]='b';
          /* or *pb='b'; *(pb+1)='b'; */
          /* or memset(b, (int)'b', 2); */
          char * pA = malloc(1);
          pA[0]='a'; /* same options b has */
          char * PAB = malloc(3);
          memcpy(pAB, pA, 1 );
          memcpy(pAB+1, pB, 2);
          free(pA);
          free(pB);

          Note that I've omitted (to keep code compact) malloc return value check, but you must always do it in real programming, i.e.

          char *p=malloc(10);
          if ( ! p )
          {
          /* handle error */
          }
          /* allocation was fine, go on

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          [my articles]

          _ Offline
          _ Offline
          _NielsB
          wrote on last edited by
          #4

          Hello again! Thank you for your adwises :) The real task I want to test is to append buffB to buffA. Asume there is a function-call that gives me 2 pointers to buffers (p_buffA and p_buffB) and the size of each of them (n_A and n_B). The function should retur the new pointer to buffA and the new size (f.e. saved in a struct). Now I am using c++ new / delete to allocate and free memory. // buffA int n_A = 30; char* p_buffA = new char[n_A]; memset(p_buffA, (int)'a', n_A); // buffB int n_B = 50; char* p_buffB = new char[n_B]; memset(p_buffB, (int)'b', n_B); // append buffB to buffA, use temp buffAB int n_AB = n_A + n_B; char* p_buffAB = new char[n_AB]; // cpy memcpy(p_buffAB, p_buffA, n_A); // copy buffA to buffAB memcpy(p_buffAB+n_A, p_buffB, n_B); // copy buffB to buffAB // buffA and buffB are no longer neaded -> delete delete [] p_buffA; delete [] p_buffB; // set buffA to buffAB p_buffA = p_buffAB; n_A = n_AB; Is this snipet ok? Are there better ways to solve this issue?

          C 1 Reply Last reply
          0
          • _ _NielsB

            Hello again! Thank you for your adwises :) The real task I want to test is to append buffB to buffA. Asume there is a function-call that gives me 2 pointers to buffers (p_buffA and p_buffB) and the size of each of them (n_A and n_B). The function should retur the new pointer to buffA and the new size (f.e. saved in a struct). Now I am using c++ new / delete to allocate and free memory. // buffA int n_A = 30; char* p_buffA = new char[n_A]; memset(p_buffA, (int)'a', n_A); // buffB int n_B = 50; char* p_buffB = new char[n_B]; memset(p_buffB, (int)'b', n_B); // append buffB to buffA, use temp buffAB int n_AB = n_A + n_B; char* p_buffAB = new char[n_AB]; // cpy memcpy(p_buffAB, p_buffA, n_A); // copy buffA to buffAB memcpy(p_buffAB+n_A, p_buffB, n_B); // copy buffB to buffAB // buffA and buffB are no longer neaded -> delete delete [] p_buffA; delete [] p_buffB; // set buffA to buffAB p_buffA = p_buffAB; n_A = n_AB; Is this snipet ok? Are there better ways to solve this issue?

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            It is ok. But you have always check for pointer validity, for instance

            char* p_buffA = new char[n_A];
            if ( !p_buffA )
            {
            // handle error
            }
            // go on with program
            ...

            You have also the option to use realloc:

            // buffA
            int n_A = 30;
            char* p_buffA = (char*) malloc(n_A);
            memset(p_buffA, (int)'a', n_A);

            // buffB
            int n_B = 50;
            char* p_buffB = (char*) malloc(n_B);
            memset(p_buffB, (int)'b', n_B);

            // append buffB to buffA, without use temporary
            char* p_buffA = realloc[p_BuffA, n_A + n_B];

            // cpy
            memcpy(p_buffA + n_A, p_buffB, n_B); // copy buffB to new buffA

            // buffB is no longer neaded, delete
            free(p_buffB);

            (as stated above you've always to check return value of allocation function) :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            [my articles]

            _ 1 Reply Last reply
            0
            • C CPallini

              It is ok. But you have always check for pointer validity, for instance

              char* p_buffA = new char[n_A];
              if ( !p_buffA )
              {
              // handle error
              }
              // go on with program
              ...

              You have also the option to use realloc:

              // buffA
              int n_A = 30;
              char* p_buffA = (char*) malloc(n_A);
              memset(p_buffA, (int)'a', n_A);

              // buffB
              int n_B = 50;
              char* p_buffB = (char*) malloc(n_B);
              memset(p_buffB, (int)'b', n_B);

              // append buffB to buffA, without use temporary
              char* p_buffA = realloc[p_BuffA, n_A + n_B];

              // cpy
              memcpy(p_buffA + n_A, p_buffB, n_B); // copy buffB to new buffA

              // buffB is no longer neaded, delete
              free(p_buffB);

              (as stated above you've always to check return value of allocation function) :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              [my articles]

              _ Offline
              _ Offline
              _NielsB
              wrote on last edited by
              #6

              Looks nice :D Thnaks alot.

              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