Weird problem with debugger in MS visual C++

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Nguyen Viet Bang
New poster
Posts: 4
Joined: Tue Nov 04, 2003 8:06 pm

Weird problem with debugger in MS visual C++

Post by Nguyen Viet Bang » Fri Jan 16, 2004 3:12 pm

Hi all ,

I've just moved from Pascal to C++ . I face really weird problem with the debugger in Visual Studio C++ 6.0 . belose is my source : [cpp]

#include<iostream>
#include<string>

using namespace std ;

char * nextToken(char *b){
char static *tem ;

if (b==NULL) {

if (tem == NULL) return NULL ;

char * si = new char[(strchr(tem,' ')-tem)];
strncpy(si ,tem,(strchr(tem,' ')-tem));
si+='\0' ;
return si ;
tem += strlen(si) ;
while (tem[0] == ' ') tem++ ;

}
else {
tem = new char[strlen(b)] ;
tem = strcpy(tem,b);
cout<<tem ;
return NULL ;
}
}

void main(){

char s[100] , c;

do {
cout<<"Enter a string or q to exit\n" ;
cin.getline(s , 100) ;

if (strcmp(s,"q")==0) break ;
nextToken(s);

do {
cout<<"press n for next token , press s for stopping" <<endl;
cin >> c ;
char * s = nextToken(NULL) ;
if (c=='n' && s != NULL ) cout<< s ;
else break ;
} while (1) ;

} while (1) ;
}
[/cpp]

I should state firstly there's no silly compilation error here . Everything compiled OK but still got stuck while running .

It's just a simple one simulating the strtok func. in C++. But when I try to view the static "char * tem" variable in function " char * nextToken(char*) , the debugger dunt understand .(Error : symbol "tem" not found".

I tried to changed it into #include<string> instead of #include<string.h> , then try to view it by ::tem , but it doesnt help also .

Thnx for any reply :)

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Re: Weird problem with debugger in MS visual C++

Post by CDiMa » Fri Jan 16, 2004 5:30 pm

Nguyen Viet Bang wrote:Hi all ,

I've just moved from Pascal to C++ . I face really weird problem with the debugger in Visual Studio C++ 6.0 . belose is my source : [cpp]

char static *tem ;

[/cpp]

I should state firstly there's no silly compilation error here . Everything compiled OK but still got stuck while running .

It's just a simple one simulating the strtok func. in C++. But when I try to view the static "char * tem" variable in function "
hmmm, I don't understand what should be the meaning of "char static *tem".
I've never seen that before and I'm assuming you intended "static char *tem"; otherwise can you please explain me the meaning of your declaration?

Ciao!!!

Claudio

Nguyen Viet Bang
New poster
Posts: 4
Joined: Tue Nov 04, 2003 8:06 pm

Post by Nguyen Viet Bang » Fri Jan 16, 2004 7:15 pm

Thnx for ur reply , Claudio...

Well , actually I have noticed it before and it is also one of my efforts to fix the problem . To my knowledge , "char static * tem" means that "tem" is a static pointer points to the string of characters , meanwhile , "static char * tem" refers to a pointer (not static at all) pointing an array of STATIC characters . Can you get it ? (Pls correct me if I'm wrong).

But it doens't matter since when switch between 2 things , the problem 's still there (means the debugger cannot "see" the tem variable). Up to now I have solved it already using Borland C3.1 (I get use to Borland stuff :oops: ). But still wonder why it doesnt' work with Visual C++ (which I believe should be more up to date). The debugger there is noisy right :-?

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Post by CDiMa » Mon Jan 19, 2004 11:40 am

Nguyen Viet Bang wrote:Thnx for ur reply , Claudio...

Well , actually I have noticed it before and it is also one of my efforts to fix the problem . To my knowledge , "char static * tem" means that "tem" is a static pointer points to the string of characters , meanwhile , "static char * tem" refers to a pointer (not static at all) pointing an array of STATIC characters . Can you get it ? (Pls correct me if I'm wrong).
Well, static means that the scope of a variable is restricted to the source file where it is declared. So I don't understand how this applies to the data pointed by a variable...
Maybe you intended "const"?
Nguyen Viet Bang wrote:But it doens't matter since when switch between 2 things , the problem 's still there (means the debugger cannot "see" the tem variable). Up to now I have solved it already using Borland C3.1 (I get use to Borland stuff :oops: ). But still wonder why it doesnt' work with Visual C++ (which I believe should be more up to date). The debugger there is noisy right :-?
Indeed it's strange that "static" affects the behaviour of the debugger... What happens if you drop it entirely?
About VC++ and Borland C3.1 I can't say anything, I'm more a gcc/gdb freak ;)

Ciao!!!

Claudio

Farqaleet
New poster
Posts: 15
Joined: Wed Jan 28, 2004 11:24 pm

Post by Farqaleet » Wed Jan 28, 2004 11:44 pm

Yes the problem is quite strange to even many experience people. Well here it is in some detail

char* = A non-const pointer to non-const memory. Both are modifiable

const char* = A non-const pointer to a memory locatio that is const i.e. You can modify the pointer but not the memory it points to.

char const* = A const pointer to non-const memory. i.e. you can modify the memory it points to but not the pointer itself. Arrays are of this type. These can only be initalized at declaration.

const char const* = A const pointer to const memory. None are modifiable. These can only be initialized during declaration.

so char static * = A static pointer to a non-static memory location.

In this special case as the memory is allocated with new, thus the memory is also kind of "static" from the point it is created

A few basic errors are there check these out.

[cpp]char * si = new char[(strchr(tem,' ')-tem)];[/cpp]

This statement allocates 1 less byte which is required by the null terminator.

you are trying to concatenate '\0' to si

[cpp] si+='\0' ;|[/cpp]

Either change this to

[cpp]si[tem+1] = 0;[/cpp]

or

use strcpy() which automatically adds a null terminator but do not forget to always allocate 1 extra byte for strings as a rule of thumb and that arrays are indexed at 0 so you get 1 less for an index from all functions.

Also the following statements never execute

[cpp] tem += strlen(si) ;
while (tem[0] == ' ') tem++ ;[/cpp]

The return statement before it exits the function and they never get to execute.

I donot quite understand how nextToken returns NULL once tem is allocated.

Hope that this will help you[/cpp]

Farqaleet
New poster
Posts: 15
Joined: Wed Jan 28, 2004 11:24 pm

Post by Farqaleet » Wed Jan 28, 2004 11:53 pm

I am sorry. The correction I wanted to suggest was:

[cpp]si[(strchr(tem,' ')-tem)] = 0; [/cpp]

not si[tem+1]=0;

Also as [cpp]tem += strlen(si);[/cpp] is after return thus, the infinite loop.

Remember strlen uses null terminator to determine length. If you don't be careful of having null at the end, this can also cause infinite loops as it moves wildly in your computers memory.

You will also get many exceptions using this technique as you never correctly deallocate tem

Rather you should create another pointer, assign the pointer equal to tem, play around with the new pointer and then deallocate tem when exiting.

[cpp]char static* tem2 = tem;[/cpp]

then when returning for the final time do:
[cpp]delete [] tem;[/cpp]

I hope that this will help you in your problem.

Kire Sopov
New poster
Posts: 7
Joined: Wed Sep 15, 2004 2:01 am

Post by Kire Sopov » Sat Sep 18, 2004 5:33 pm

Farqaleet is wrong about something;
Namely, in C++, the following two lines are entirely equal:

const char *pChar;
char const *pChar;

whereas

char * const pChar; --- is something else :wink:

Example:
----------------------------------------------------------------------
[cpp]
#include <stdio.h>

int main()
{
const char *pC1 = "abcd";
char const *pC2 = "efgh";

pC1++, pC2++; // Legal
// *pC1 = 'X', *pC2 = 'X'; // Illegal

char *const pC3 = "Test";

*pC3 = 'X'; // Legal
// pC3++; // Illegal

return 0;
}
----------------------------------------------------------------------
[/cpp]
Try compiling the code (uncomment the illegal lines too)

As for the 'static' thing, I can also state that the following two lines are entirely equal as well:

static int *pInt;
int static *pInt;

:lol:

nghiank
New poster
Posts: 31
Joined: Wed Nov 20, 2002 3:10 pm
Contact:

cu Bang ngu qua'

Post by nghiank » Wed Oct 06, 2004 6:23 am

aaaa
Last edited by nghiank on Sat Apr 28, 2007 3:00 am, edited 1 time in total.

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Wed Oct 06, 2004 11:12 am

No. C++ doesn't delete memory allocated on heap unless it is told to. You can return si with no danger.

nghiank
New poster
Posts: 31
Joined: Wed Nov 20, 2002 3:10 pm
Contact:

Post by nghiank » Wed Oct 06, 2004 1:35 pm

bbbb
Last edited by nghiank on Sat Apr 28, 2007 3:01 am, edited 1 time in total.

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Wed Oct 06, 2004 3:54 pm

It all depends. The following function is correct (but it looks bad, so I don't recommend writing code this way):
[cpp]int * func(){
int * i;
i = new int;
*i = 123312; // here i is valid
return i;
}

int main(){
int * ptr = func();
printf("%d\n", *ptr); // the pointer is still valid
delete ptr;
}

[/cpp]
Don't forget to free the memory with delete - that's the biggest danger with pointers that aren't smart enough.

func is safe because heap memory isn't deleted when dynamic scope is left. However, the following code is incorrect:
[cpp]int * func(){
int * i;
int j;
i = &j;
*i = 123312; // here i is valid
return i;
}

int main(){
int * ptr = func();
printf("%d\n", *ptr); // the pointer is invalid.
}

[/cpp]
Using the pointer returned by func will result in undefined behaviour. Everything can happen.

tat tvam asi
New poster
Posts: 30
Joined: Sat Nov 30, 2002 1:04 pm

Post by tat tvam asi » Thu Oct 07, 2004 11:05 pm

helo!

ah yes now i see the point: never move from pascal to c++ ...

peace,
csaba noszaly

Post Reply

Return to “C++”