0

How do I, and is it even possible, get actual maximum size of character array passed to a function as a pointer?

int size=0;
while (char_array[size++])

this won't work, as it will return number of characters in the array, and I need its actual size.

Also I cannot use any libraries (specifically string) or my own classes or basically anything useful.

EDIT as it turns out, I will have to explain the assignment quickly. And I am told not to do it in the comments. So here it is:

I have to implement a function that has three character arrays passed as pointers. Cannot change that in any way. First one is a string with commands included in that string (important). The commands are simple: delete x letters, and so on, but some are tricky, like add x letters from second/third character array to the right. Why tricky? Because those arrays may contain more commands, which will then be added to the string I'm working on. So one way to do it is to work on the string passed to a function, but that would be very slow, and I would have to add/delete characters in the middle, which basically means moving the rest of the letters many times. So I would have to have m*n opertions, where n is size of string and m is ammount of commands. But only n operations if I could write to another string. However I don't know how much space would I need, because the commands could add more commands, which would add more letters to initial ammount of characters. Checking the ammount and type of commands beforehand would be slow and make the whole thing rather pointless. However I know that the string passed to the function will have enough space (it's in the assignment specifications). So if I just could access it's size, I could declare another string with same size, knowing it would fit the resulf for sure.

So what will I do? I will declare this local string that I'm writing into with initial size and then if the result is greater I will rewrite it to a bigger string. Which will give me n times z operations, where z is a number of rewrites, which is still better than n times m, but worse than just n.

  • 1
    Why not pass array size to your function? – SergeyA Dec 05 '16 at 15:01
  • Because I'm not allowed to. I agree this kind of problem would never appear in serious programming and is trivial to work through if one's allowed slightest ammount of flexibility, but I am not. – user3019593 Dec 05 '16 at 15:04
  • 1
    The `std::string` class keeps track of its length and capacity; so use `std::string` instead of character arrays. – Thomas Matthews Dec 05 '16 at 15:05
  • Again, I cannot use string or it's functions. – user3019593 Dec 05 '16 at 15:07
  • I think you need to provide the precise wording of your question. Looking at the various answers you have been given, and your response of "no, not allowed to do that", you need to indicate if "it is not possible to do that" is a possible answer - because I think it is the correct answer. – Martin Bonner supports Monica Dec 05 '16 at 15:24

1 Answers1

3

If you can change the function signature, you can use some template magic:

template<int N>
int foo(char (&arr)[N])
{
    int size = N;
}

This assumes you actually pass in an array, not a plain pointer.

If you're passing a pointer, you'll also need to pass in the size, or use non-standard hacks (such as inspecting the memory before the actual array, where the size is usually stored)

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • I'm not allowed to do that. So basically, I can't do what I want in a standard way (that will not be rejected when my assignment is evaluated)? – user3019593 Dec 05 '16 at 15:14
  • 1
    @user3019593 then it's not clear to me what the requirements are... – Luchian Grigore Dec 05 '16 at 15:16
  • 1
    The "non-standard hack" will only work if the array has been allocated directly on the heap with `new char[47]` or `malloc`. There is no size information if the array is a local variable on the stack, or if it is a static or global variable (or it is a member variable of a larger structure on the heap). – Martin Bonner supports Monica Dec 05 '16 at 15:22
  • @user3019593, don't use comments to refine your question. Instead, **EDIT** original question and add all relevant details there. – SergeyA Dec 05 '16 at 15:29
  • Done. Sorry about it. – user3019593 Dec 05 '16 at 15:36