LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2023, 05:23 AM   #256
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870

Please compare these two lines:

> the start address would be the same, unless the first location has been put in a different location by the sort(int *, int) function.

> sort(v+mid, mid);

Doesn't that ring a bell?

Let me help: 'v+mid' means '&v[mid]' while 'v' means '&v[0]'. Are they equals?

Off: do you ever acknowledge when you get some information, like in post #253 ?

Last edited by NevemTeve; 09-19-2023 at 05:26 AM.
 
Old 09-19-2023, 05:57 AM   #257
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Please compare these two lines:

> the start address would be the same, unless the first location has been put in a different location by the sort(int *, int) function.

> sort(v+mid, mid);

Doesn't that ring a bell?

Let me help: 'v+mid' means '&v[mid]' while 'v' means '&v[0]'. Are they equals?

Off: do you ever acknowledge when you get some information, like in post #253 ?
I saw your response as soon as it came, and was and still am working on inability to understand the program, using gdb. In fact, was out of focus on this, and hence could not gain the logic, or understand its workings even; though am conversant with mergesort.
Because of that the gdb could not help me in understanding the working of the program.
But, isn't it sure that gdb helps only if one has clear idea of what is happening, as for such a simple program I faltered in using gdb to understand the program; the reason being that I could not form an idea about the program's workings.
If so, how can your big program be understood by me, is a big confusing issue.

Thanks, that you have fixated on this program. Will soon be back.

Last edited by ajiten; 09-19-2023 at 06:05 AM.
 
Old 09-19-2023, 06:04 AM   #258
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
https://www.linuxquestions.org/quest...ml#post6454280
 
Old 09-19-2023, 06:08 AM   #259
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Will do the rest of the problems, after problem #4; but this implies that you mean that gdb is an advanced tool, that helps those who can figure out its (someone else's program's) working.
Isn't that a chicken-and-egg issue, as if am totally clear about the logic of someone else's program, then gdb is just an asset, not a neccessity.

Last edited by ajiten; 09-19-2023 at 06:17 AM.
 
Old 09-19-2023, 06:12 AM   #260
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
I think I know what is the problem: you forgot to take same basic programming courses before starting an advanced course about compilers.
This cannot be solved via gdb, only via hard work.

Last edited by NevemTeve; 09-19-2023 at 06:16 AM.
 
1 members found this post helpful.
Old 09-19-2023, 07:11 AM   #261
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Re #250
Code:
before: printf("value=%d", n);
after:  printf("value=%d\n", n);
For such a big answer under #1, posted by you in post #245; cannot understand why you have given such a crispiest reply.

You stated that:
Code:
#1 FILEs like stdout can be buffered, e.g. printf/putc/... etc won't be visible until the end of line (\n). There are different solutions for this, e.g. writing debug-messages to stderr instead.
Or using fflush(stdout) after every debug-print.
Or adding \n to the debug messages.
Or using `setvbuf(stdout, NULL, _IONBF, 0);` at the beginnging
But, that seems to address only the very first measure stated by you, i.e. using the printf() statement.
But, as have already shown earlier, if set breakpoints at lines containing the printf() statement(s), then nothing is printed, till the program exits, and the very last hit at the given breakpoint would print the desired variable value(s).
Such a run of gdb is shown in the attachment, for the given mergesort program.
Attached Thumbnails
Click image for larger version

Name:	Screenshot (1261).png
Views:	3
Size:	149.5 KB
ID:	41700  

Last edited by ajiten; 09-19-2023 at 07:20 AM.
 
Old 09-19-2023, 07:27 AM   #262
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
> But, as have already shown earlier, if set breakpoints at lines containing the printf() statement(s), then nothing is printed, till the program exits, and the very last hit at the given breakpoint would print the desired variable value(s).

No, it has nothing to with breakpoints, it is buffering. That's again something you would learned if you took some beginner programming courses.
 
1 members found this post helpful.
Old 09-19-2023, 08:25 PM   #263
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
> But, as have already shown earlier, if set breakpoints at lines containing the printf() statement(s), then nothing is printed, till the program exits, and the very last hit at the given breakpoint would print the desired variable value(s).

No, it has nothing to with breakpoints, it is buffering. That's again something you would learned if you took some beginner programming courses.
How can not know sthg about printf() statements, but have heard buffering in the context of printf() the first time.
Have done projects of long duration of 9 months, 6 months, & 2 years respectively; in IIT-B, IIT-K, IIT-Kgp as well; and don't know what is this.

All I could get is here.

Edit: Got much better answer here, but seems need refer to the Richard Stevens' book titled: Advanced Programmng in the UNIX environment; as given an oblivious reference in the answer.
The second & third attachments show the portions found in the book's 3rd edition. But, have not been able to link still to your answer; or even make any meaning out of the same.

======================================================
Doubt #2:

Please tell what is the meaning of the address: 0x0000000000400815, in the given webpage, stated in the line obtained after running the command: bt (backtrace):
Code:
#1  0x0000000000400815 in main () at merge.cc:20
also shown in the first attachment.

The reason for asking this is that there is no such address displayed for the sort(), as earlier took it as the start address of the nain(), but then the same should be for the sort() too.
Attached Thumbnails
Click image for larger version

Name:	Screenshot (1267).png
Views:	7
Size:	151.2 KB
ID:	41705   Click image for larger version

Name:	Screenshot (1269).png
Views:	4
Size:	172.1 KB
ID:	41708   Click image for larger version

Name:	Screenshot (1268).jpg
Views:	3
Size:	162.5 KB
ID:	41709  

Last edited by ajiten; 09-20-2023 at 09:47 PM.
 
Old 09-21-2023, 01:28 PM   #264
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by ajiten View Post
Will do the rest of the problems, after problem #4; but this implies that you mean that gdb is an advanced tool, that helps those who can figure out its (someone else's program's) working.
Isn't that a chicken-and-egg issue, as if am totally clear about the logic of someone else's program, then gdb is just an asset, not a neccessity.
Gdb is not a substitute for your own understanding.

If you cannot understand implementations of simple programming concepts without gdb, then you will not be able to understand them with gdb. Gdb is exactly this in all cases: an asset, not a necessity.

Quote:
Originally Posted by ajiten View Post
Sir, I am a faculty, though with only basic C programming skills.
Faculty or otherwise, more knowledge of gdb is not what you need at this time. You need to take a course, not a refresher, in basic programming in C. Continuing posts about gdb cannot cover for that requirement.

Quote:
Originally Posted by ajiten View Post
How can not know sthg about printf() statements, but have heard buffering in the context of printf() the first time.
Have done projects of long duration of 9 months, 6 months, & 2 years respectively; in IIT-B, IIT-K, IIT-Kgp as well; and don't know what is this.
I intend this in a most helpful way, please do not take it otherwise, but how you can have participated in programming projects over such a period without learning any of the basics, including output buffering, is apparent from your questions here.

You actively avoid learning those basics, looking for quick answers which require no effort on your part and substituting questions about other topics such as endless minutiae about gdb, and as a result you make no useful progress.

Quote:
Originally Posted by ajiten View Post
All I could get is here.

Edit: Got much better answer here, but seems need refer to the Richard Stevens' book titled: Advanced Programmng in the UNIX environment; as given an oblivious reference in the answer.
The second & third attachments show the portions found in the book's 3rd edition. But, have not been able to link still to your answer; or even make any meaning out of the same.

======================================================
Doubt #2:

Please tell what is the meaning of the address: 0x0000000000400815, in the given webpage, stated in the line obtained after running the command: bt (backtrace):
Code:
#1  0x0000000000400815 in main () at merge.cc:20
also shown in the first attachment.

The reason for asking this is that there is no such address displayed for the sort(), as earlier took it as the start address of the nain(), but then the same should be for the sort() too.
Again, pointless minutia from which you will derive no benefit, deflecting the advice already given.

It is time you make that actual effort. If you do then you will find others, and not only here at LQ, encouraging and eager to help! But if you continue this course you will find that others lose interest and avoid participation in your threads. The reward for those providing help is to see someone benefit from their help. Please reward us!

One additional note on posting: It is preferable to paste only the relevant text of code, error messages and the like, rather than posting screenshots. Relevant text formatted in the common fonts and colors provided by the page is much easier for others to follow. Additionally, if you reach the limit of attachments here at LQ and begin to remove older images to make room for new, you will remove context from existing threads making them of no value to future visitors. Thanks for considering this.

Last edited by astrogeek; 09-21-2023 at 06:53 PM. Reason: tpoys
 
Old 09-23-2023, 10:45 AM   #265
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by astrogeek View Post
Gdb is not a substitute for your own understanding.

If you cannot understand implementations of simple programming concepts without gdb, then you will not be able to understand them with gdb. Gdb is exactly this in all cases: an asset, not a necessity.



Faculty or otherwise, more knowledge of gdb is not what you need at this time. You need to take a course, not a refresher, in basic programming in C. Continuing posts about gdb cannot cover for that requirement.



I intend this in a most helpful way, please do not take it otherwise, but how you can have participated in programming projects over such a period without learning any of the basics, including output buffering, is apparent from your questions here.

You actively avoid learning those basics, looking for quick answers which require no effort on your part and substituting questions about other topics such as endless minutiae about gdb, and as a result you make no useful progress.



Again, pointless minutia from which you will derive no benefit, deflecting the advice already given.

It is time you make that actual effort. If you do then you will find others, and not only here at LQ, encouraging and eager to help! But if you continue this course you will find that others lose interest and avoid participation in your threads. The reward for those providing help is to see someone benefit from their help. Please reward us!

One additional note on posting: It is preferable to paste only the relevant text of code, error messages and the like, rather than posting screenshots. Relevant text formatted in the common fonts and colors provided by the page is much easier for others to follow. Additionally, if you reach the limit of attachments here at LQ and begin to remove older images to make room for new, you will remove context from existing threads making them of no value to future visitors. Thanks for considering this.
Your last line suits you and others of caliber like you, not me; as screenshots help me to remember easily what issues I faced.
 
Old 09-24-2023, 04:07 AM   #266
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by ajiten View Post
Your last line suits you and others of caliber like you, not me; as screenshots help me to remember easily what issues I faced.
It is more about helping others help you and maintaining the integrity of information found here over time for all future visitors. But it is your choice.
 
1 members found this post helpful.
Old 10-02-2023, 04:50 AM   #267
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
> But, as have already shown earlier, if set breakpoints at lines containing the printf() statement(s), then nothing is printed, till the program exits, and the very last hit at the given breakpoint would print the desired variable value(s).

No, it has nothing to with breakpoints, it is buffering. That's again something you would learned if you took some beginner programming courses.
#1> request to give more inputs/hints. Please take into consideration the time that has passed since you posted the given post, and hence I am in need of more inputs.

#2> have issues with the two versions of code, one gives segmentation fault; while the other doesn't. Fail to see the reason why the segmentation fault appears in one only?

bug_c.c : not gives segmentation fault.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
   char *noptr;
   char i = '5';
   *noptr = i;
   printf("i: %c, *noptr:%c", i, *noptr);
}
Gives the output as:
Code:
$ ./bug_c
i: 5, *noptr:5
buggy_.c: gives segmentation fault
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
   int *ptr = (int *)malloc(sizeof(int));
   int *noptr = NULL;

   for (int i=0; i<10; i++) {
      if (i == 5) {
        *noptr = i;
      }
      else {
        *ptr = i;
      }
      printf("i is %d\n", i);
   }
}
Gives the segmentation fault:
Code:
$ ./buggy_
i is 0
i is 1
i is 2
i is 3
i is 4
Segmentation fault (core dumped)
Attached Thumbnails
Click image for larger version

Name:	Screenshot (1378).png
Views:	1
Size:	118.5 KB
ID:	41773  
 
Old 10-02-2023, 06:03 AM   #268
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Your second question is off-topic here, please open another topic titled "What is memory-garbage and how to avoid it?"
 
1 members found this post helpful.
Old 10-02-2023, 06:39 AM   #269
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Your second question is off-topic here, please open another topic titled "What is memory-garbage and how to avoid it?"
Have posted here; please also give some needed response to my last post's first query.
 
Old 10-02-2023, 06:55 AM   #270
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
As a start, learn these by heart
https://en.m.wikipedia.org/wiki/Data_buffer
https://en.cppreference.com/w/c/io/setvbuf
 
  


Reply

Tags
compiler



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to understand why the given Lex program fails to recognize the given input string. ajiten Programming 11 07-11-2023 12:28 AM
LXer: Trees, B-Trees, B+Trees and H-Trees LXer Syndicated Linux News 0 07-15-2013 02:11 PM
How to Boot Linux/ Linux like OS from Pen derive kumars.nitin123 Linux - General 2 11-05-2009 11:00 PM
Unable to derive IRQ merchtemeagle Linux - Hardware 2 06-21-2007 08:14 PM
mount derive in lfs aneel Linux From Scratch 2 11-24-2005 12:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration