Thread: help with malloc

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    6

    help with malloc

    I'm trying to write a program that combines all command line arguments into one string.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    
    int main(int argc, char *argv[]) {
        int i;
        char *vector;
        size_t length;
        
        i = 0;
        while (i < argc) {
            length = length + strlen(argv[i]) + 1;
            i++;
        }
        
        vector = malloc(sizeof(char) * length);
        assert(vertor != NULL);
    
        strcpy(vector, argv[0]);
        i = 1;
        while (i < argc) {
            strcat(vector, argv[i]);
            i++;
        }
    
        printf("%s\n", vector);
        
        free(vector);
        
        return EXIT_SUCCESS;
    }
    My code compiles and run fine on iMac with gcc, but when I run it on my school system it gives me error message saying "uninitialized variable accessed".

    Also, what is the difference between (char *)malloc(sizeof(char) * length) and just malloc(sizeof(char) * length)? We were taught the previous in school, but everyone uses the second one online.

    Thank you so much!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,608
    On line 13 you appear to be using length before you assign an initial value.

    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,706
    > Also, what is the difference between (char *)malloc(sizeof(char) * length) and just malloc(sizeof(char) * length)? We were taught the previous in school, but everyone uses the second one online.
    A lot of people make the early mistake of compiling C programs with a C++ compiler (which makes the cast necessary).

    When compiling a C program with a C compiler, the cast is at best redundant and at worst dangerous.
    It is redundant if stdlib.h is included, which prototypes malloc as returning void*. Since void* can be cast implicitly to anytype*, the cast serves no purpose.
    It is dangerous if stdlib.h is NOT included, because the compiler will assume malloc returns int. The cast covers up the int to pointer conversion - which may lose information if int's are smaller than a pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    6

    Thumbs up

    Quote Originally Posted by jimblumberg View Post
    On line 13 you appear to be using length before you assign an initial value.

    Jim
    THANK YOU IT WORKS NOW!!! I thought I was using malloc wrong

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using malloc
    By zafy in forum C Programming
    Replies: 5
    Last Post: 11-12-2012, 08:12 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  4. Malloc() help?
    By scarlet00014 in forum C Programming
    Replies: 2
    Last Post: 09-21-2008, 10:20 AM
  5. malloc()
    By ammar in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2002, 03:58 PM

Tags for this Thread