views:

533

answers:

6

I have a string in C++ of type EDIT: (const char *) passed as argument to strlen, but it returns void..

damn i can't get formatting to work >.<

it goes like

strlen(astruct.string);

Thanks..

EDIT: Did some checking,

strlen("test");

still gives void.. why?

EDIT: Here's the image http://img14.imageshack.us/img14/1808/strlen.png

Sorry for being unclear previously. Formatting was not working quite well. Anyway, the confusion was solved by both Evan Teran and Vlad Romascanu. Also take a look at Brian R. Bondy's answer.

Thanks. Feel free to close.

A: 

strlen returns an integer, so I assume you mean it returns "0".

Also, the way that you specify your data type, I can't quite tell if it's a const char * or const char **. If it's the latter, then you need to make sure you're dereferencing the ** to a single *.

My guess is that the string starts with a null byte, which is why it's returning 0.

NilObject
Hi, no, it returns void, not 0.. I don't think the string starts with a null byte.. actually its a file path.. it starts with a D..data type is const char *..
krebstar
It can't return void, it returns whatever size_t is. Perhaps your cstring header file is bad?
colithium
Try adding a line before strlen that says printf("%hhu", *astruct.string); Report what this printed. My guess is that it prints "0".
NilObject
You can't return void, it is the lack of a return value.
Ed Swangren
+3  A: 

Referring to your screen shot: your debugger is displaying <void> for strlen(...) when in fact it should display an error.

You cannot call methods and display their results in the debugger watch. The debugger will only display existing variables and data. It cannot invoke arbitrary methods on demand since the methods can alter the state of the program being debugged in ways that were not anticipated by either the author of the code nor by the debugger.

What you can do is, in your code, temporarily add:

size_t tmp_len = strlen(struc.string);

then compile, and add tmp_len to the watch.

Cheers, V.

vladr
Dude, I know.. But in my code, it returns void.. Is there any reason why it would do this?
krebstar
Does this mean that when you attempt to compile <<size_t x = strlen("abcd")>> you actually get a compilation error like "cannot assign void to size_t"?
vladr
Are you checking the length in the function or in the return from the function? Sound like your return is using data on the stack.
BenB
K, i get it.. thanks
krebstar
I don't think he added a "strlen("whatever");" to the watch. This looks like he pulled up the "automatics" window (the left one clearly is) where is says "strlen returned <void>"
Evan Teran
Yes, that's right evan, but I also did a strlen("whatever") in the watch.. both said <void>..
krebstar
Indeed, so while Vlad's answer is true, mine is also correct.
Evan Teran
Yes, thanks.. Too bad I can't mark both correct
krebstar
You can call functions in the watch. Please see my description for what I believe to be the correct answer.
Brian R. Bondy
For example: int testfunc(char *) { return 5; }. You can call testfunc("hi") in the watch and it will return 5.
Brian R. Bondy
Brian is right..
krebstar
A: 

It can't return void. Void is the lack of a return value, so you can't, err, return it.

How do you check for void anyway? Void isn't a value. Please demonstrate how you are getting void. Is it compile time or run time?

If you do in fact have a system where strlen is declared with a void return type, run as fast as you can in the other direction.

Bernard
+4  A: 

strlen is not of return type void, it's your debugger that is not giving the right message.

Why your debbuger is showing void?

The implementation of strlen that you are using is probably wrapped around a #define strlen someothername_strlen.

The debugger probably does not support #define properly or some other modifiers on the function.

You will have to do something like iLen = strlen("test") then check iLen in your watch.

Normally you can call functions in your watch. For example try to define the following function then call it in your watch:

int testFunc(char*)
{
  return 5;
}

You will probably get 5 in your watch as a result.

Brian R. Bondy
I did the code sample you have, you are right. When I right click on strlen and click go to definition it brings up the STRING.H file in VC98\Include. it defines strlen as "size_t __cdecl strlen(const char *);"
krebstar
I get the same results as krebstar. It's probably that the CRT is not built with the right debug settings (or maybe that strlen() is an assembly routine). If I have a myStrlen() function that does nothing but wrap a strlen() call, the watch window displays the result on that just fine.
Michael Burr
Thanks for the additional verification michael..
krebstar
+6  A: 

You are confused by the crappy debugger of visual studio 6.0. Don't rely on this. It likely couldn't get the return value due to inlining or something similar.

Print the value out to get the real value.

EDIT: Also, from your example, it seems that you may not be storing the result of strlen anyway. This also may be a reason why the debugger isn't seeing the return value. It's entirely possible that the compiler decided that it doesn't need to actually execute the strlen if you aren't using the value.

NOTE: at this point there is no real excuse for still using VC++ 6.0. It is an ancient compiler and IDE which is an embarrassingly poor c++ compiler. The newer versions of the visual c++ compiler are free (without the IDE), use them.

Evan Teran
Why do you have to be so rude about it? My company uses VC6, okay? It's not like I can just change the software on company property. Okay, so I work for a crappy company, doesn't mean you should be mean about it.
krebstar
This is actually the right answer. But you could be nicer.. >.<
krebstar
I wasn't trying to be rude, I was attempting to state the facts about the toolset you are using. If you are being forced to use it, I'm sorry to hear that. You may want to consider bringing up the antiquity of the toolset to your team.
Evan Teran
Ok. I don't think I can do that, anyway, thanks.
krebstar
-1 incorrect reason stated for why the debugger displays <void> when a method invocation is typed in the watch
vladr
care to explain vlad?
krebstar
@Vlad, I beg to differ, the left window in the screen shot is clearly the "automatics" window showing what was *executed*
Evan Teran
+1 cause I can't choose two answers
krebstar
@Evan, you are correct about the left pane, and it is most definitely a misfeature of VC for displaying <void> there; the right pane is an actual watch, and VC should not have accepted the insertions. +1.
vladr
I think we've collectively solved this for you.
Evan Teran
A: 

In C++, functions always return a value of the type they are declared to return. Since the strlen function declaration looks something like this:

size_t strlen(const char *);

the only thing it can possibly return is a size_t. The compiler uses this information at compile time to determine how to handle the return value when you call the function. The point here is that if the strlen function is declared as above, it cannot decide to return void sometimes and a value of type size_t other times.

This is generally a characteristic of statically typed languages like C++. In dynamically typed languages (Perl, Python, Ruby, PHP, etc) a function can decide to return a value of any type each time it is called.

Greg Hewgill