Problem with argv...

I tried to compile this file but I've got something unexpected...

Filename: test.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
   char *test = (char *)malloc(1 * sizeof(char));

   printf("[%d]\n", strlen(test));

   free((void *)test);

   return 0;
}


In the command line (Windows)...

>test.exe
[0]
>test.exe abc
[3]

Why the strlen(test) become 3 when I inputting argv[1]??

I've tested that code in:
- GCC (using plain g++ and g++ -s -O3)
- MSVC6 (in MSVC6, either I inputted argv[1] or not, it show [6]!!)

What's wrong?
I getting down by doing string manipulations in C/C++...
It's complicated, I think...
Last edited on
the strlen() function relies on there being a '\0' termination character to mark the end of the string. You haven't put one in your variable 'test' so strlen() will keep going until it finds one somewhere in memory.

If you are trying to measure the size of the memory malloc'd it can't be done using standard library functions. You may be lucky in that your implementation has an extension to do it, otherwise you just have to track the size yourself.
Thank's a lot bnbertha!
Now, I understand what is the meaning of "NULL terminated string" in Windows SDK reference.

Something like PCHAR in Delphi :P

Finally, to know a valid C++ source file (from the extension),
I could write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv)
{
   if(argc < 2)
   {
      //Check the parameter length

      printf("%s\n", "Not enough parameter.");
      exit(-1);
   }

   int argv1_len = strlen(argv[1]);

   if(argv1_len < 5) //Minimum file name: strlen("a.cpp") == 5
   {
      //Is ".cpp" valid file name?

      printf("%s\n", "Invalid file name.");
      exit(-1);
   }

   char *extension = (char *)malloc(4 * sizeof(char));

   int i = (argv1_len - 1);

   for(; i>(argv1_len - 5); i--)
   {
      extension[argv1_len - i - 1] = argv[1][i];
   }

   /*
      We've got "ppc." from ".cpp"
      So, before comparing it, we have to reverse it.
   */

   strrev(extension);

   if(strcmp(extension, ".cpp") > -1)
   {
      //Finally...
      printf("\"%s\" is a C++ source file.", argv[1]);
   }
   else
   {
      printf("\"%s\" is not a C++ source file.", argv[1]);
   }

   //I still don't know what exactly this function for yet...
   free(extension);

   // Should I do this:
   // free(argv);

   // Or:
   // int i = 0;
   // for(; i<argc; i++)
   // {
   //    free(argv[i]);
   // }
}


Phew, that was a long thing!!
Last edited on
No, you don't need to free argv at all so forget line 54 onwards. You should return 0 at the end of your main() as it is defined as returning an int.

free() releases the memory back to the stack after you've finished with it from making a malloc type call, see Dynamic memory Management on this link for more info http://www.cplusplus.com/reference/clibrary/cstdlib/
From:
http://www.cplusplus.com/reference/clibrary/cstdlib/free.html


A block of memory previously allocated using a call to malloc, calloc or realloc is deallocated, making it availbale again for further allocations.


availbale should be available

How do I repair this?
Last edited on
Good spot.

Right at the bottom of the page you will see a small link "Spotted an error? contact us"

Thats what you need to do

EDIT: Do it from the page with the problem
Last edited on
Topic archived. No new replies allowed.