what's the "sizeof ()" argv?

closed account (236Rko23)
hi there, i have this problem: i need to know the size of the argument strings of the program to be able to dinamicaly create the arrays for a command line and knot been limited to a fixed number of using useless extra memory.

1
2
3
4
5
void test (int argc, char *argv[]){
    cout << sizeof(argv) << "  " << argv << "  " << ;

exit(0);
}


what i get is "4" and 0xbf9da794

tryed with *argv, argv[1], *argv[1] and none gave me the correct result.
googled anyware but it seems noone did this before....
isn't there a way to do this without having to use more arrays and strcpy?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int argc, char * argv[])
{
    cout << "number of arguments: " << argc << endl;
    
    int i;
    for (i=0; i<argc; i++)
    {
        cout << "size of \"" << argv[i] << "\": " << strlen(argv[i]) << endl;
    }
    
    cin.get();
    return 0;
}
closed account (jwC5fSEw)
An array a is as big as sizeof(a)/sizeof(*a).

EDIT: Or did you mean the size of an individual element?
Last edited on
sizeof is often misunderstood

sizeof gives you the size in bytes of the type you give it. It does not give you the string length. It does not detect allocated buffer sizes. It just looks at the given type and says how big it is.

argv is a pointer. Therefore sizeof(argv) returns the size of a pointer (commonly 4)

master roshi has the right idea in his post.

Shadow Addict almost has the right idea, but it doesn't work in this situation. sizeof(a)/sizeof(*a) only works if 'a' is an array. The problem is that argv is not an array, but is a pointer.
closed account (236Rko23)
thanks for the replies, the case was what roshi said. I didn't know of strlen() so thanks again :P

edit: thanks disch for the explanation, i know that it gives the size in bytes but as a char is 1 byte i thought that if "abc" are three characters, it would be 3 bytes long (and it is) but what i didn't know was that it didn't work with pointers. :P
Last edited on
closed account (jwC5fSEw)
The problem is that argv is not an array, but is a pointer.


Well isn't char* p == char p[]? If argv is an array of pointers to chars, doesn't that mean it's also an array of char arrays?
Last edited on
Well isn't char* p == char p[]?


When passed as a parameter to a function, yes. char* argv[] is the same as char** argv.

But that just means they're both pointers. Neither of them are arrays.

If argv is an array of pointers to chars, doesn't that mean it's also an array of char arrays?


No, it's neither of those things. It's a pointer to a pointer to a char.

It just so happens that it points to the first element in an array, so it's kind of like it's pointing to an array. However the compiler (and therefore sizeof()) has no way to know this. Nor does it have any way to know how large the array being pointed to is.

Essentially, sizeof() can only give you the size of an array if you give it an array. argv is not an array, it's a pointer.
just want to add additional question Disch, consider this int * p = new int[10];, i wonder how does the compiler figures out how long the array was if you delete it, delete[] p;
Last edited on
Wouldn't that have to be

delete [] p;

?

I am also curious how it knows at runtime how much memory to free.
oh yeah, just miss it.. sorry about that.. i'm gonna fix it now..
i wonder how does the compiler figures out how long the array was if you delete it


The compiler doesn't figure it out. The delete[] implementation does.

Typically, when you new[] something, it actually allocates a little more memory than you request, and the extra memory is used to record the size of the allocated array so that it can be deallocated later.

For example -- say you do this: int* p = new int[10]; It might actually allocate room for 11 ints (the first one keeps track of how many ints were allocated), and then it actually gives you a pointer to the 2nd integer in the array.

However the exact details of how it works can vary depending on the implementation, and it's not something you should try to manipulate in your code.
wow my guess was right.. thanks Disch..
closed account (jwC5fSEw)
No, it's neither of those things. It's a pointer to a pointer to a char.

It just so happens that it points to the first element in an array, so it's kind of like it's pointing to an array. However the compiler (and therefore sizeof()) has no way to know this. Nor does it have any way to know how large the array being pointed to is.

Essentially, sizeof() can only give you the size of an array if you give it an array. argv is not an array, it's a pointer.


Thanks Disch, I understand it now!
Last edited on
closed account (236Rko23)
very intresting, thanks everyone ^^
Topic archived. No new replies allowed.