NO

Author Topic: view source code during debugging  (Read 22786 times)

CommonTater

  • Guest
Re: view source code during debugging
« Reply #15 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.
 
« Last Edit: May 25, 2012, 09:36:57 AM by CommonTater »

vedro-compota

  • Guest
Re: view source code during debugging
« Reply #16 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 ?

CommonTater

  • Guest
Re: view source code during debugging
« Reply #17 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.
 
« Last Edit: May 28, 2012, 01:06:14 AM by CommonTater »

vedro-compota

  • Guest
Re: view source code during debugging
« Reply #18 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)

CommonTater

  • Guest
Re: view source code during debugging
« Reply #19 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.
« Last Edit: May 28, 2012, 01:07:03 AM by CommonTater »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: view source code during debugging
« Reply #20 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

CommonTater

  • Guest
Re: view source code during debugging
« Reply #21 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...




Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: view source code during debugging
« Reply #22 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

vedro-compota

  • Guest
Re: view source code during debugging
« Reply #23 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...
« Last Edit: May 28, 2012, 06:07:46 AM by vedro-compota »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: view source code during debugging
« Reply #24 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

vedro-compota

  • Guest
Re: view source code during debugging
« Reply #25 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 )
« Last Edit: May 28, 2012, 06:30:57 AM by vedro-compota »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: view source code during debugging
« Reply #26 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

vedro-compota

  • Guest
Re: view source code during debugging
« Reply #27 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!
 

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: view source code during debugging
« Reply #28 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

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: view source code during debugging
« Reply #29 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