Thread: eliminating use of malloc/free when copying char arrays

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    eliminating use of malloc/free when copying char arrays

    This program works as expected:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
      
    typedef struct {
      unsigned long long int           address;
      float                            current;
      unsigned char                    pressure_units;
    } sensor;
     
     
    sensor createSensor(unsigned long long int address, float current, unsigned char pressure_units)
    {
     
     
      sensor temp;
      temp.address = address;
      temp.current = current;
      temp.pressure_units = pressure_units;
      return temp;
    }
     
     
    int main(int argc, char *argv[])
    {
        int i;
        sensor sensors[3];
        char msg[100];
        char *msgs[3];
        char wholeMsg[300];
         
        for(i=0;i<3;i++)
        {
            sensors[i] = createSensor(10000*i,i+3.0,i);
        }
        for(i=0;i<3;i++)
        {
            int n = sprintf(msg,"address:%llu,current:%f,pressure_units:%u", sensors[i].address, sensors[i].current, sensors[i].pressure_units);
            if(n <= 100)
            {
                msgs[i] = malloc(sizeof(msg));
                strcpy(msgs[i],msg);
            }          
        }
        sprintf(wholeMsg,"%s\n%s\n%s\n",msgs[0],msgs[1],msgs[2]);
        printf("whole message: %s",wholeMsg);
        for(i=0;i<3;i++)
        {
            free(msgs[i]);
        }
    }
    It correctly prints out the following:

    whole message: address:0,current:3.000000,pressure_units:0
    address:10000,current:4.000000,pressure_units:1
    address:20000,current:5.000000,pressure_units:2

    But I don't like how I had to use malloc and free. Is there a different way to accomplish copying the string into a char pointer without resorting to dynamic memory allocation?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    But I don't like how I had to use malloc and free. Is there a different way to accomplish copying the string into a char pointer without resorting to dynamic memory allocation?
    Certainly. Instead of:
    Code:
    char *msgs[3];
    Declare:
    Code:
    char msgs[3][100];
    Note that either way you aren't copying to a char pointer, but rather copying to an array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  2. Copying to free store
    By darren78 in forum C++ Programming
    Replies: 8
    Last Post: 07-28-2010, 01:10 PM
  3. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  4. malloc of char arrays
    By St. Jimmy in forum C Programming
    Replies: 3
    Last Post: 02-21-2005, 04:40 AM
  5. Initializing char arrays on the free store
    By roktsyntst in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2002, 06:22 AM