Pelles C forum

C language => Beginner questions => Topic started by: vedro-compota on May 24, 2012, 04:33:24 pm

Title: view source code during debugging
Post by: vedro-compota on May 24, 2012, 04:33:24 pm
Hello)

guys, please tell me - how to view source code during  debugging ?
I see only puch-mov-push-mov-mov etc - but when i try to set tick near "show sourse code" (to debug through the lines) - nothing happen....
please help..........
Title: Re: view source code during debugging
Post by: DMac on May 24, 2012, 05:57:06 pm
Hello Mr. Vedro

It sounds like you are right clicking to view the disassembly while in Debug mode.

Debugging in Pelles works in three steps.

Step one Compile in debug mode. (See step 1)

Step two Set break point and click debug/go. (See step 2)

Step three Hit F10 to step through code and view values in the watch window. (See step 3)
Title: Re: view source code during debugging
Post by: CommonTater on May 24, 2012, 05:59:54 pm
Edit: I see DMac beat me to it :D
 
Since not all of us use the DBG/REL/PRF AddIn, here's how to set the options manually...
 
On the main menu...
Project -> Project options -> Compiler -> Debug information = full

Plus...
For 32 bit projects:
Project -> Project options -> Linker -> Debug information  = Code View and Coff
For 64 bit projects:
Project -> Project options -> Linker -> Debug information = Code View

Set at least one breakpoint.
Then click the Go Debug button on your toolbar.
Title: Re: view source code during debugging
Post by: vedro-compota on May 25, 2012, 12:51:47 am
DMac , thank you) but my IDE have different from yours buttons....but i've tuned Pelles C as CommonTater (thank you too!) said  and I can walk through the lines in debug mode, but I can understand why on several custom function Pelles C leave the source code and run through.....push-mov - assambler as I understand)
Title: Re: view source code during debugging
Post by: CommonTater on May 25, 2012, 04:43:31 am
DMac , thank you) but my IDE have different from yours buttons....but i've tuned Pelles C as CommonTater (thank you too!) said  and I can walk through the lines in debug mode, but I can understand why on several custom function Pelles C leave the source code and run through.....push-mov - assambler as I understand)

Because that segment of code is probably in a library or dll that does not have debugging symbols.
Title: Re: view source code during debugging
Post by: vedro-compota on May 25, 2012, 07:33:51 am
hm. so in this case i should debug through assembler?
because of this I was try to debug in Visual Studio - but it doesn't like lines as:
Code: [Select]
struct user
int zone;
int city;
int node;
int point;
char* name;
};
struct user  users[b]; //  [i][b]error C2057: expected constant expression[/b][/i]
users[n].name=malloc(sizeof(char));
but Pelles C doesn't worry because of  "constant expression" why it's so or may be it's difference between C and C++ ?.....

Title: Re: view source code during debugging
Post by: Bitbeisser on May 25, 2012, 08:08:21 am
hm. so in this case i should debug through assembler?
because of this I was try to debug in Visual Studio - but it doesn't like lines as:
Code: [Select]
struct user
int zone;
int city;
int node;
int point;
char* name;
};
struct user  users[b]; //  [i][b]error C2057: expected constant expression[/b][/i]
users[n].name=malloc(sizeof(char));
but Pelles C doesn't worry because of  "constant expression" why it's so or may be it's difference between C and C++ ?.....
With just that snippet, it is impossible to tell. The basic problem is that you define and array with "b" elements, with a definition of b nowhere in sight. You can't dynamically define an array like this, neither in C or C++ (FAIK), unless "b" refers to a "const" defined elsewhere in your code...

With bad programming practices, compiler/development environment behavior is random at best...

Ralf
Title: Re: view source code during debugging
Post by: CommonTater on May 25, 2012, 08:11:27 am
Vedro... C++ and C are two completely separate languages... they are maintained independently, each to their own separate set of standards.  It is a terrible mistake to think that because something works one way in C that C++ should play along or because something works in C++ that it should work in C too.... It just doesn't work that way.

Yes C++ compilers will compile some C code... but it's not C code at the same standards as true C.  For example, Microsoft's VC++ compiler won't compile C-99 or C-11 code... the two current C coding standards.

Also note that debugging is of no value on code that produces compile-time errors and warnings...
The compiler's warnings are about being able to build the executable.
The debugger is about finding behavoural problems in code that compiles correctly.
Title: Re: view source code during debugging
Post by: vedro-compota on May 25, 2012, 08:14:28 am
so - as i understand - it's incorrect to write -

Code: [Select]

struct user
int zone;
int city;
int node;
int point;
char* name;
};
n= salen(arr); // get number of str

printf("[[[ Numer of useful strings = %i ]]] \n",n);
int b=n;
struct user  users[b];
users[n].name=malloc(sizeof(char));
Title: Re: view source code during debugging
Post by: CommonTater on May 25, 2012, 08:15:58 am
With just that snippet, it is impossible to tell. The basic problem is that you define and array with "b" elements, with a definition of b nowhere in sight. You can't dynamically define an array like this, neither in C or C++ (FAIK), unless "b" refers to a "const" defined elsewhere in your code...

Try this....

Code: [Select]
int main (void)
  {  int c = 11;
      int array[c];

      return 0; }

C-99 and C-11 will let you define local array sizes at run time, using variables...

Quote
With bad programming practices, compiler/development environment behavior is random at best...

As it was put to me many years ago:  No program is ever better than the worst programmer who worked on it.
Title: Re: view source code during debugging
Post by: vedro-compota on May 25, 2012, 08:19:53 am
Quote
Try this....
Code: [Select]
int main (void)
  {  int c = 11;
      int array[c];
      return 0; }
but i need to create array dynamically!
Title: Re: view source code during debugging
Post by: CommonTater on May 25, 2012, 08:21:29 am
so - as i understand - it's incorrect to write -

So lets take a look....
Code: [Select]

struct user      <--- missing brace
   int zone;   
   int city;
   int node;
   int point;
   char* name;
};
n= salen(arr); // get number of str
   
   printf("[[[ Numer of useful strings = %i ]]] \n",n);
   int b=n;                                                          <-- unnecessary variable
   struct user  users[b];                                        <-- may cause stack overflow if n is too big
   users[n].name=malloc(sizeof(char));                     <-- allocates only 1 character for name storage
           
Title: Re: view source code during debugging
Post by: CommonTater on May 25, 2012, 08:22:03 am
Quote
Try this....
Code: [Select]
int main (void)
  {  int c = 11;
      int array[c];
      return 0; }
but i need to create array dynamically!

Then learn how to use malloc() correctly.

Title: Re: view source code during debugging
Post by: vedro-compota on May 25, 2012, 08:31:58 am
>>>>> users[n].name=malloc(sizeof(char));                     <-- allocates only 1 character for name storage

yes, but in that case i needed firstly to "set dot of array" - EOF in last element name - before  initialization of  others elements

>>>>>>>>>> int b=n;                                                          <-- unnecessary variable

yes - you are right --  i was try to get round exeption of "constant expression" by this stupid way))

>>>>>>>>>>struct user  users;                                      <-- may cause stack overflow if n is too big

yes , but that is my problem, as I think - because of how can i create array dynamically in other way.........

Quote
Then learn how to use malloc() correctly.
ok. i'll do as you said
Title: Re: view source code during debugging
Post by: vedro-compota on May 25, 2012, 08:34:33 am
Quote
how to use malloc() correctly.
malloc()? but may be calloc for array is better....ok - that's my problem - i'll solve it ........ big thanks!
Title: Re: view source code during debugging
Post by: CommonTater on May 25, 2012, 09:07:39 am
yes , but that is my problem, as I think - because of how can i create array dynamically in other way.........

Like this...
 
Code: [Select]
typedef struct t_USER
  { int zone;   
     int city;
     int node;
     int point;
     char name[32]; }
   USER, *pUSER;
 
int nUsers = 0;
pUSER users;
 
// create array of users
nUsers = GetUserCount();
users = malloc(nUsers * sizeof(USER));
 
// dispose of array when done
free(users);

nUsers could be in the millions with no risk of overflowing anything.
 
Title: Re: view source code during debugging
Post by: vedro-compota on May 27, 2012, 07:44:29 pm
Quote
>>>>>>>no risk of overflowing anything
because - if it'll be no space malloc() will just return null ?
as I know
Code: [Select]
char s [] ;
char *s;

are equal  - so if  i write
Code: [Select]
char s [5] ;
char *s = malloc (5 * sizeof(char));
will these both be equal ?

as you said - not - but why  - only because we can't check that 
Code: [Select]
char s [5] ; works properly in a particular situation ?
Title: Re: view source code during debugging
Post by: CommonTater on May 27, 2012, 10:29:32 pm
Quote
>>>>>>>no risk of overflowing anything
because - if it'll be no space malloc() will just return null ?
as I know
Code: [Select]
char s [] ;
char *s;

are equal  - so if  i write
Code: [Select]
char s [5] ;
char *s = malloc (5 * sizeof(char));
will these both be equal ?

as you said - not - but why  - only because we can't check that 
Code: [Select]
char s [5] ; works properly in a particular situation ?

They are not equal...
char string[255]; is created on the program's stack.
char* s = malloc(255 * sizof(char));  is created in the system memory pool (called the "Heap").
 
The program's stack has a practical size limit of about 1 megabyte, which can get used up pretty quickly in busy code.  The Heap is the size of your system's memory (less that used by other programs) and on a 4gb machine you often have 2 or 3gb of memory available for malloc().  However, since this is memory taken from the system pool, you have to free() it as soon as you're done with it.
 
malloc() only returns null if your memory allocation fails.  If it succeeds the return value is a pointer to the block of memoy you've just allocated for your array, struct, whatever...  The size of the block is *at least* the number of bytes you asked for... eg. 255 * sizeof (char) == 255 bytes of reserved space.
 
What you need to understand here is C's rules of Scope... Basically a scope is created by passing the { brace and ends when you pass the } brace (it's more complex than that, but that's good enough to give you the idea)... You cannot access stack based variables that are in a different scope... the result is undefined behaviour and --usually-- programs that misbehave in wildly mysterious ways... like my little array-return sample.

 

Edit:
In all due respect...
 
As I've suggested before... you seriously need to get a decent C programming book and work through it page by page, do all the examples, take all the quizzes; repeat the parts you don't understand until you do understand them... then turn to the next page.
 
It may sound like I'm scolding you... but what I'm really trying to do is get you started off in the right direction. Trust me on this... C is not something you can learn by fiddling around with it.  It takes deliberate study.
 
Title: Re: view source code during debugging
Post by: vedro-compota on May 27, 2012, 10:43:26 pm
Dennis Ritchie and Brian Kernighan - the c programming language
and
the C standard (as you advised  me earlier)
Title: Re: view source code during debugging
Post by: CommonTater on May 27, 2012, 10:49:59 pm
Dennis Ritchie and Brian Kernighan - the c programming language
and
the C standard (as you advised  me earlier)

Ok... so spend the time... Do the work.  You won't learn C by simply reading the books... you really do have to type up and compile all the examples, then play with the code, learn what works and what does not work before you move on to the next section... otherwise you just confuse yourself.
Title: Re: view source code during debugging
Post by: Bitbeisser on May 28, 2012, 05:14:54 am
I think this is all a perfect example where someone is trying to "program" without first learning how to...

To learn "how to program" does not actually involve any programming language or specific compiler. It requires to learn the concepts/logic/"basics" that are pretty much the same for all programming languages.
Once you understand those "basics", the next step would be to learn how to implement those basic in a specific programming language, followed by learning the specifics of a certain compiler implementation along with specific for a certain programming environment (like Windows)...

To many people certainly skip on the first step here, don't bother much with the second one and then struggle when they move straight to step 3 or worth, step 4...

Ralf
Title: Re: view source code during debugging
Post by: CommonTater on May 28, 2012, 05:34:30 am
It's not all that bad.  On, ahem, another forum I once found myself in the rather inenviable position of having to ask: "How do you plan to program a computer you don't even know how to operate?"

Vedro's been here for a while and he's got the books... about the only reason he would now find himself in his present bind is that he's not doing the work of learning.

I'm not much to talk on this since I went straight from DOS/Turbo Basic to Pascal/Windows API programming without even a sideways glance at the books... From that, moving to C was positively unnerving... and out came the books... time to do it right.  It might be possible to dumbass your way through Pascal but not C... It's far too obedient and doesn't complain nearly enough...



Title: Re: view source code during debugging
Post by: Bitbeisser on May 28, 2012, 05:45:04 am
Vedro's been here for a while and he's got the books... about the only reason he would now find himself in his present bind is that he's not doing the work of learning.
Well, it certainly doesn't help to have those books sitting on the shelf. But neither helps just typing up sample programs out of those books without understanding the purpose/logic behind them...
Quote
I'm not much to talk on this since I went straight from DOS/Turbo Basic to Pascal/Windows API programming without even a sideways glance at the books... From that, moving to C was positively unnerving... and out came the books... time to do it right.  It might be possible to dumbass your way through Pascal but not C... It's far too obedient and doesn't complain nearly enough...
Not wanting to start a "language war" here, but there are a lot of programming languages better suited to "learn the ropes" than C.
I just keep seeing people trip over a lot of the C specific peculiarities, not to mention the tripwires laid out by programming in a specific language implementation (like to have to start with a "project" in Pelle's C) or a specific target environment (Windows as far as Pelle's C is concerned). That has folks trying to learn struggling with a bunch of issues which are outside and distracting from their goals they are trying to achieve...

Ralf
Title: Re: view source code during debugging
Post by: vedro-compota on May 28, 2012, 06:03:24 am
I understand "those basics" , I've been wrote in Delphi, C#(Paskal before as first lang.), PHP - and -  as you see - in C))))
My problem in practice knowledge  - I know about "stack" "heap" - local and global vars, by value  and by reference variable  passing.....
but - as I understand in this thread  - I have never before return reverence  to REAL local value  -

that was so because I though that here - in C -  like in C#  - where all  arrays are always availible by its pointer  - and always store in heap.

But you've helped me) big thanks, guys...
Title: Re: view source code during debugging
Post by: Bitbeisser on May 28, 2012, 06:16:57 am
I understand "those basics" , I've been wrote in Delphi, C#(Paskal before as first lang.), PHP - and -  as you see - in C))))
My problem in practice knowledge  - I know about "stack" "heap" - local and global vars, by value  and by reference variable  passing.....
but - as I understand in this thread  - I have never before return reverence  to REAL local value  -

that was so because I though that here - in C -  like in C#  - where all  arrays are always availible by its pointer  - and always store in heap.

But you've helped me) big thanks, guys...
Well, going by some of your questions/problems you have posted here, I am not so sure that you understand "the basics". Dynamically allocating an array is one of those things, the concept is the very same in all programming language, just the command(s) itself practically changes. Likewise the issue with returning a pointer to a local variable/array (or the futility thereof)...

It doesn't matter how many programming languages you have tried to use, if you do not understand the basics, you will struggle in one way or the other switching from one programming language to another. And I doubt that you "returning a pointer to a local defined array" is working as you expect in C# either, even by chance. It is just not logical, regardless what programming language you use, well, as long as it understands/supports local variables to begin with. "Local" variables are called this way for a reason, it would be against their very existence if returning a pointer to them would yield a valid result as you seem to imply...

Ralf
Title: Re: view source code during debugging
Post by: vedro-compota on May 28, 2012, 06:26:34 am
Quote
I am not so sure that you understand "the basics".
here it is - http://stackoverflow.com/questions/6794371/array-of-valuetype-in-c-sharp-goes-to-heap-or-stack
but  -
Quote
i know that i know nothing
it's here ))  - http://en.wikipedia.org/wiki/I_know_that_I_know_nothing
so  -
Quote
It is just not logical, regardless what programming language you use
you 're right) I don't know many basic things....so I wouldn't say what's logical and what's not )
Title: Re: view source code during debugging
Post by: Bitbeisser on May 28, 2012, 06:38:56 am
http://stackoverflow.com/questions/6794371/array-of-valuetype-in-c-sharp-goes-to-heap-or-stack
Well, referencing that article shows that you in fact do not understand the issue at hand. And to make it clear, I have no intentions of being "mean", I just try to make you aware that there are certain aspects of programming that you do not understand or have a misunderstanding about...


That article refers to the allocation without  a local/global context, which is however relevant for the example you posted here (in C)...
While the actual array (the memory area containing the values of the array elements) might be created on the heap, the variable that is pointing to that area is in your example defined as a local variable, so this variable/pointer is no longer valid when you exit out of the function (scope) you have defined that variable. That includes returning the value of that variable (pointer), it will point to a memory area that is undefined to anything outside of that function.

Ralf
Title: Re: view source code during debugging
Post by: vedro-compota on May 28, 2012, 07:32:15 am
>>>>>>That article refers to the allocation without  a local/global context,
but it's about heap/stack - you should  agree - if my data in heap and I have pointer to that araa in heap - this variable in this case is "global" (in general purpose)
so it's really "relevant for the example I posted here (in C)"

yes -  i can't use local pointer to the heap by its name in another function - but I can return its value -  and get the same result.

but I don't want to show that i'm know C -  I even hadn't written 5000 lines of code in C - so how can I said that I know this lang?! - nothing!

I cite C# as an example only to show that there is variant where I can return local pointer value, because data all referenced data is stored in  heap (in C#) - which is common pool for process.

But you said -
Quote
so this variable/pointer is no longer valid when you exit out of the function (scope) you have defined that variable.
do you mean C or C# ?
if data is in common heap - it's really not logical if I had a pointer (not by local name - but returned adress) and can't access this data!
 
Title: Re: view source code during debugging
Post by: Bitbeisser on May 28, 2012, 07:47:36 am
>>>>>>That article refers to the allocation without  a local/global context,
but it's about heap/stack - you should  agree - if my data in heap and I have pointer to that araa in heap - this variable in this case is "global" (in general purpose)
so it's really "relevant for the example I posted here (in C)"

yes -  i can't use local pointer to the heap by its name in another function - but I can return its value -  and get the same result.
Well, no, that is simply "by chance", in case of such a simple program. The returned value in your case simply can not be trusted as soon as you leave the scope of the local function in which this variable/pointer is defined...
Quote
but I don't want to show that i'm know C -  I even hadn't written 5000 lines of code in C - so how can I said that I know this lang?! - nothing!
My comments were not in regards to C, it is clear that you are trying to learn this particular programming languages (and it's implementation in Pelle's C). What I was referring to was that you have skipped over what I was referring to in a previous post as the first step of learning to program, to understand the very basics, which apply to all program languages (as long as those support certain functionality, like local variables)...
Quote
I cite C# as an example only to show that there is variant where I can return local pointer value, because data all referenced data is stored in  heap (in C#) - which is common pool for process.
You have not understand what I tried to tell you. It doesn't matter if C or C#, the value of the local variable (in your case the pointer to the locally allocated array) is not valid outside of the scope of the function where you allocate that array. Depending on the compiler implementation (in regards of garbage collection for example), it is by pure chance that you can access the same array variables with the returned pointer. In best case scenario, this kind of programming (when the compiler/OS doesn't do a good job by itself in terms of "house keeping") leads to memory leaks.
Quote
But you said -
Quote
so this variable/pointer is no longer valid when you exit out of the function (scope) you have defined that variable.
do you mean C or C# ?
if data is in common heap - it's really not logical if I had a pointer (not by local name - but returned adress) and can't access this data!
On the contrary, it is very logical, because that pointer to that data become valid (can not be trusted anymore) once you leave the scope of the function!

I am not sure if I have enough time before I go to bed, but I will try to give you an example that demonstrates what your misunderstanding is...

Ralf
Title: Re: view source code during debugging
Post by: Bitbeisser on May 28, 2012, 08:01:54 am
Oops, my bad...  ???

I got this thread mixed up with your other one, "Is this right in C?"...

The general remarks still hold true and the result that you see in your sample program in that thread is exactly what I am trying to tell you.
The local variable "array", which is declared in "MyFunction", will point to the same memory region on the stack on each of the subsequent  call of "MyFunction", which in turn will allocate the very same region of memory that hold the actual values of the array. That's why the subsequent calls to that function overwrite this area of the int array. If that is on the stack or on the heap, that is just of secondary relevance here...

Ralf
Title: Re: view source code during debugging
Post by: CommonTater on May 28, 2012, 04:23:18 pm
@ Vedro...
 
What I don't think you're undestanding is the rules of scope in C...
Many beginners do not appreciate that the { and } characters are an active part of their code, viewing them only as punctuation marks... which they are not.
 
Here's a bit of code to demontrate the concept... (I've also attached the project so you can run and test it...)
 
Code: [Select]

 // demonstration of rules of scope
 
int main (void)
  {                // beginning of outer scope
    int x;
    int y;
    x = 10;
    y = 20;
    printf("x = %d, y = %d\n",x,y);
 
    {               // beginning of inner scope
      int x;
      x = 10233;
      printf("x = %d, y = %d\n",x,y);
    }               // end of inner scope
 
    printf("x = %d, y = %d\n",x,y);
       
    return 0;
  }                  // end of outer scope
 
The output from this is...
x = 10, y = 20
x = 10233, y = 20
x = 10, y = 20
 
So how can we change x in the inner scope but it is not changed in the outer scope? 
How can y be visible in the inner scope?
 
Because, for all intents and purposes each scope is it's own little program... demarked by the braces... braces begin and end scopes, they're not merely punctuation marks. Values from larger scopes penetrate into smaller scopes... values from smaller scopes do not carry over to larger scopes... 
 
Thus when you try to return your array from the "inner scope" of a function call, you are breaking the rules of scope and it will fail.
 
These rules have to be understood if you are to write useable programs...
 
 
 
 
Title: Re: view source code during debugging
Post by: vedro-compota on May 28, 2012, 06:46:31 pm
thank you! I'll study this example.