Like Tree6Likes
  • 2 Post By quzah
  • 2 Post By oogabooga
  • 1 Post By stahta01
  • 1 Post By stahta01

Why does this malloc give me a segmentation fault?

This is a discussion on Why does this malloc give me a segmentation fault? within the C Programming forums, part of the General Programming Boards category; Code: f = fopen("test1.txt", "r"); if (f == NULL) { printf("Could not open file.\n"); exit(1); } //Memory allocation for output ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    69

    Why does this malloc give me a segmentation fault?

    Code:
        f = fopen("test1.txt", "r");
        if (f == NULL) {
            printf("Could not open file.\n");
            exit(1);
        }
        //Memory allocation for output file name
        output_titles = (char*)malloc(sizeof(char*)*(strlen(strcat("test1.txt", ".titles"))+1));
    
        //Creation of output file name
        strcpy(output_titles, strcat("test1.txt", ".titles"));
    I'm getting a segmentation fault, and it seems to be happening because of wrong use of malloc. But why is this wrong?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Possible reason in link
    FAQ > Casting malloc - Cprogramming.com
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    @Stahta,

    I tried what is suggested but I'm still getting the same. :/

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Malloc isn't giving you a segfault. This is:
    Code:
    strcat("test1.txt", ".titles")
    Why aren't you just doing:
    Code:
    strlen( "test1.txt.titles" )

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Because normally, the user will name the file (which I'm stating as "test1.txt") as an argument. Meaning that, to run this, the use will type: /nameofprog test1.txt

    So in other words, the program can't initially be told how long the name of the input file will be. I guess I couldn't just write strlen("argv[1].titles") or something like that.
    Last edited by Xpl0ReRChR; 01-09-2012 at 09:31 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suggest then that you get the part where they take input working first. Because your test case is broken by you trying to strcat string literals.


    Quzah.
    iMalc and Salem like this.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Code:
    f = fopen(argv[1], "r");
        if (f == NULL) {
            printf("Could not open file.\n");
            exit(1);
        }
    Is that what you're referring to? Because I think this part is working.
    Last edited by Xpl0ReRChR; 01-09-2012 at 09:52 AM.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The point is that you can't concatenate anything onto a string literal, since it does not have the extra space allocated to hold anything else. So first strcpy "test1.txt" to a char array with enough space to hold your final string.

    Also, when you malloc your memory, you actually want sizeof(char) not sizeof(char*).
    stahta01 and Xpl0ReRChR like this.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    You have test1.txt\0 and .titles\0. Strcat supposedly creates test1.txt.titles\0, or "test1.txt.titles". So why am I unable to use strcat(argv[1], ".titles") + 1 as the space needed, and then do strcpy(output_titles, strcat("test1.txt", ".titles"))?

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Please follow and read link
    strcat - C++ Reference

    The destination must be a NON constant and have enough space to hold both C-strings followed by a zero byte.

    NOTE: You should NEVER write to the argv memory; doing so is undefined!

    Tim S.
    Xpl0ReRChR likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Code:
        output_titles = (char*)malloc(sizeof(char) * (strlen(argv[1]) + strlen(".titles") + 1));
        if (output_titles == NULL) {
            printf("Memory allocation error.\n");
            exit(1);
        }
        
        output_prices = (char*)malloc(sizeof(char) * (strlen(argv[1]) + strlen(".prices") + 1));
        if (output_prices == NULL) {
            printf("Memory allocation error.\n");
            exit(1);
        }
        
        
    
        strcpy(output_titles, argv[1]);
        strcat(output_titles, ".titles");
        strcpy(output_prices, argv[1]);
        strcat(output_prices, ".prices");
    I tried doing this but to no avail. Still getting a segmentation fault.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Works for me.

    Code:
    test.titles
    test.prices
    
    Process returned 0 (0x0)   execution time : 0.008 s
    Press any key to continue.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char *argv[]) {
    
        char *output_titles;
        char *output_prices;
    
        if (argc <= 1){
            printf("Syntax error.\n");
            exit(1);
        }
    
        output_titles = (char*)malloc(sizeof(char) * (strlen(argv[1]) + strlen(".titles") + 1));
        if (output_titles == NULL) {
            printf("Memory allocation error.\n");
            exit(1);
        }
    
        output_prices = (char*)malloc(sizeof(char) * (strlen(argv[1]) + strlen(".prices") + 1));
        if (output_prices == NULL) {
            printf("Memory allocation error.\n");
            exit(1);
        }
    
    
    
        strcpy(output_titles, argv[1]);
        strcat(output_titles, ".titles");
        strcpy(output_prices, argv[1]);
        strcat(output_prices, ".prices");
    
        printf("%s\n", output_titles);
        printf("%s\n", output_prices);
    
        return 0;
    }
    Tim S.
    Last edited by stahta01; 01-09-2012 at 10:41 AM. Reason: Added argc error checking
    Xpl0ReRChR likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    You're definitely right, it's working, thank you!
    I replaced argv[1] with "test1.txt" to be able to use the gdb and turns out the segmentation fault occurs later on, at a different stage.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't do you any good to be handed code that works, if you still don't comprehend why it works.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    I do get it, now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-12-2011, 07:28 PM
  2. Segmentation fault with Malloc
    By uniprog in forum C Programming
    Replies: 7
    Last Post: 12-02-2010, 09:22 AM
  3. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  4. malloc segmentation fault
    By BharathKumar in forum C Programming
    Replies: 5
    Last Post: 06-27-2007, 02:53 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21