Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String manipulation & memory allocation - C

I am in the process of learning C. I have a method that takes 3 strings and combines them to do some operation. Following was my first implementation using a GCC compiler.

void foo(const char *p1, const char *p2, const char *p3)
{
    size_t length = strlen(p1) + strlen(p2) + strlen(p3);
    char combined[length + 1];
    memset(combined, 0, length + 1);
    strcat(combined, p1);
    strcat(combined, p2);
    strcat(combined, p3);
    printf("Result : %s", combined);
}

int main()
{
    foo("hello ", "world ", "how");
    return 0;
}

This works well. But when I compiled this using, cc -Wall -pedantic -g foo.c -o foo, I started getting warnings like ISO C90 forbids variable length array ‘combined’. MSVC was not compiling this code. Changed the code like

void foo(const char *p1, const char *p2, const char *p3)
{
    size_t length = strlen(p1) + strlen(p2) + strlen(p3);
    char *combined = (char *) malloc(length + 1);
    memset(combined, 0, length + 1);
    strcat(combined, p1);
    strcat(combined, p2);
    strcat(combined, p3);
    printf("Result : %s", combined);
    free(combined);
}

Questions

  1. Is this the correct implementation?
  2. If variable length arrays are not part of standard, why GCC implemented it? If the code is expected to compile only on GCC, using variable arrays will be a better alternative than using malloc?
  3. I think the thumb rule is, if memory required is knows at compile time, use arrays else use malloc to allocate required memory. Is this correct?
  4. My code is expected to compile on GCC and MSVC. I will be developing on GCC usually. So what are the compiler flags that ensures maximum portability? Currently I am using -Wall -pedantic. Should I use -ansi too? What would be the equivalent flags available in MSVC?
  5. What are the other common things to consider when writing portable C code?
like image 538
Navaneeth K N Avatar asked Jul 07 '10 08:07

Navaneeth K N


People also ask

Why is string manipulation used?

Functions for String Manipulation It's used to assess the length of a sequence - a list, tuple, etc. Since strings are lists, their length can also be assessed with the len() function! It takes any iterable sequence as an input and returns its length as an integer.

How many types of string manipulation are there?

There are two types of methods in String : shared methods and instance methods.

What is string manipulation C++?

In C++, a string is a sequence of characters. As you know, C++ does not support the built-in string type, and you have previously used null character-based terminated arrays to store and manipulate strings. These strings are termed C Strings. C++ often becomes inefficient at operating on strings.


1 Answers

This works well. But when I compiled this using, cc -Wall -pedantic -g foo.c -o foo, I started getting warnings like ISO C90 forbids variable length array ‘combined’.

Try compiling with -std=c99 option (gcc).

MSVC was not compiling this code. Changed the code like

If variable length arrays are not part of standard, why GCC implemented it?

VLAs are part of ISO C99(gcc and g++(as an extension) support VLAs). MSVC still only supports C89.

My code is expected to compile on GCC and MSVC.

Then you should not use VLAs in your code IMHO.

like image 147
Prasoon Saurav Avatar answered Oct 01 '22 21:10

Prasoon Saurav