Thread: problem using malloc function

  1. #16
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    No my main is bassically empty but it's not working with your code as well. Any idea what can be wrong?

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well what's your latest code?
    And who are you talking to when you say "it doesn't work"?

    From some of the things you've said, the first suggestion would be to stop using the C++ compiler to compile C code.
    Perhaps begin with changing your source file from prog.cpp to prog.c
    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.

  3. #18
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Try this and tell us what the output is.
    Code:
    #include <stdio.h> 
    
    int main(int argc, char *argv[])
    {
      int i = 0;
    
      printf("n\Argument count: %d", argc);
      printf("\nArguments:");
      printf("\n----------");
      for (; i < argc; ++i)
        printf("\nargv[%d] = %s", argc, argv[i]);
    
      return 0;
    }

  4. #19
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    My actual code looks like this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main()
    {
    char *filename;
    FILE *in;
        int i,q;
        int *reserve;
        int size;
        
        in = fopen(filename,"r");
        if(in==NULL)
        {
        perror("error reading data");
        //return 0;
        }
        for(i=0;;i++)
        {
        if(EOF==fscanf(in,"%i",&q)) break;
        }
        size = i;
    
    
        reserve = (int*)malloc(sizeof(int)* size);
         if (reserve==NULL)return 0;
        rewind(in);
    
    
        for(i=0;i<size;i++) fscanf(in,"%i",&reserve[i]);
        fclose(in);
    
    
    
    
    
    
    return 0;
    }
    When I compile the code from OldGuy2 I receive:
    warning: unknown escape sequence: '\A'
    printf("n\Argument count: %d", argc);

    and when i run the program, I receive this:
    nArgument count: 2
    Arguments:
    ----------
    argv[2] = test
    argv[2] = 5.7k-5.7.dat

    test is the name of the programm von OldGuy2 and 5.7k-5.7.dat is the file I try to read.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you need to move the backslash
    printf("n\Argument
    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.

  6. #21
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Perhaps you need to move the backslash
    printf("n\Argument
    Yes it was a silly typo.

    Have a closer look at line 8 and 14 - how can this work ?
    Last edited by OldGuy2; 11-12-2017 at 10:33 AM.

  7. #22
    Banned
    Join Date
    Aug 2017
    Posts
    861
    lets recap:
    you want to get the filename off the command line?
    your code in post #19 needs an assignment to the var filename
    Code:
    filename = "myfile";
    to work.
    to get it off the command line without using your var filename needs to be like this.
    Code:
    int main ( int argc, const char **argv)
    {
             if (argc < 2)
            {
                   printf("no file present\n");
                   exit(1);
             }
           filename = argv[1];
    //  or 
           in = fopen(argv[1],"r");    
    
       ..... more code here
    
    
     return 0;
    }
    your command line works on zero base counting. argc = 0, 1 , 2 , 3 , etc....


    command line

    ./programName filename command_line _options
    a good way to gain an understanding of this is. run this program
    Code:
    int main (int argc, const char **argv)
    {
    
      int i = 0;
      for ( i = 0; i < argc; i++)
            printf("argc = %d ; argv = %s\n", i , argv[i]);
    
    return 0 ;
    }
    then just type stuff on your command line hit enter then see what happens. while running this program of course.
    Last edited by userxbw; 11-12-2017 at 02:16 PM.

  8. #23
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    When i try to pass the filname in the code like userxbw told me, the programm can be compiled but when I try to run it, it never stops! I also tried your code userxbw and its working fine but we never learned about something written in the main() function in class so I would be very happy if this isn't necessary. it also seems like my anti virus program does not like my program.
    OldGuy2 what is wrong with line 8:

    Code:
    char *filename
    A pointer from type char which later will be the name of my file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me with malloc function
    By priyaxen in forum C Programming
    Replies: 2
    Last Post: 10-07-2013, 12:25 AM
  2. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  3. Can't malloc from within a function
    By synthetix in forum C Programming
    Replies: 5
    Last Post: 11-02-2011, 12:28 PM
  4. Malloc function problem
    By bouvett in forum C Programming
    Replies: 5
    Last Post: 09-13-2011, 03:05 AM
  5. using malloc in a function
    By cuizy in forum C Programming
    Replies: 10
    Last Post: 08-13-2009, 01:56 PM

Tags for this Thread