0

Please, tell me, what is the correct way of copying allocated char array to "normal" char array? I have attempted to do the following, but it fails :

char * buf = (char *) malloc (BUFSIZ * sizeof(char));
// filling up the allocated array with stuff...
char temp[BUFSIZ];
strcpy(temp, buf); // strcpy doesn't work
8
  • 1
    What are you trying to accomplish? Also note that by the C standards, a char is equal to one byte, so the * sizeof(char) is not necessary. Commented Feb 8, 2012 at 13:42
  • 3
    use memcpy, since you don't have to analyze your buffer and search for 0-terminator Commented Feb 8, 2012 at 13:43
  • You should provide more info on what makes you think that strcpy did not work (e.g. a printf that didn't show what you expected, etc.) Commented Feb 8, 2012 at 13:43
  • 1
    C or C++? If you're really in C++, all this messing with memory and pointers is discouraged. Commented Feb 8, 2012 at 13:43
  • 1
    @AdityaNaidu: Only if the string is null-terminated. Commented Feb 8, 2012 at 13:45

5 Answers 5

1

First things first, you should not cast the return value of malloc (in C anyway) since it can hide errors.

Secondly, you never need to multiply by sizeof(char) since it's always guaranteed to be one - doing so clogs up your code.

And, as to the actual question, you can use:

memcpy (temp, buff, BUFFSZ);

to copy the entire character array.

I'm assuming that's what you want because you make no mention of handling C "strings", only a character array.

If indeed you are handling C strings, strcpy will work fine in this case, provided:

  • you have room at the end of the buffer for the terminating zero-byte; and
  • you've actually put the zero-byte in there.

For example, this little snippet works fine:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void) {
    // Two buffers.

    char buff1[4];
    char *buff2 = malloc (4);
    if (buff2 == NULL) {
        puts ("No memory!");
        return 1;
    }

    // Populate first buffer.

    buff2[0] = 'p';
    buff2[1] = 'a';
    buff2[2] = 'x';
    buff2[3] = '\0';

    // Transfer and print.

    strcpy (buff1, buff2);
    puts (buff1);

    // Free and exit.

    free (buff2);
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Casting the return value of malloc is necessary in C++, however.
Despite the C++ tag, this is almost certainly a C question. You would rarely have a need to use malloc or C-style strings in C++.
1

What you probably want is strncpy(), which copies up to a certain number of bytes from a string.

strncpy(temp, buf, BUFSIZ - 1);
temp[BUFSIZ - 1] = '\0'; // null terminate the string

If that too fails, possibly just use memcpy()

memcpy(tmp, buf, BUFSIZ - 1);
temp[BUFSIZ - 1] = '\0'; // null terminate the string

Comments

1

strcpy works with zero-terminated strings, not arbitrary lumps of memory.

If you have filled it with a terminated string, then strcpy should work; if it doesn't, please give more information about how it "doesn't work".

If you don't want a terminated string, then use memcpy:

memcpy(temp, buf, BUFSIZ);

Note that there's no need to multiply by sizeof(char), since that is 1 by definition.

If you're actually writing C++, then you probably want to use std::vector or std::string rather than messing around with raw memory.

Comments

1

strcpy is a function that copy a string.

A string is a sequence of character terminated by and including the null character.

 char *buf = malloc(BUFSIZ);

This malloc call allocates an array BUFSIZ of char but this is not a string. To copy an array use the memcpy function:

memcpy(temp, buf, BUFSIZ);

If your array holds a string (a sequence of characters terminated by a null character), you can then use strcpy to copy it.

Comments

0

You don't have a terminating NUL character in your buf. You should make sure to do buf[last_actually_used_char+1] = '\0';. This means buf will have to be one character larger than the data you wish to store.

This is necessary because strcpy finds the length of the information to be copied by searching for a terminating NUL.

I strongly encourage the use of the safer strncpy, or if you just want to copy data (no '\0' necessary) the faster and safer memcpy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.