LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-06-2014, 12:17 PM   #16
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930

Had a colleague who used to always comment that all of programming really boils down to read/modify/test/branch. And that's pretty much correct. The higher languages have methods which are very well tested and the compilers or interpreters further qualify things, check scope, and provide exception processing. In the end, that's just more assembly code and more complexities leading to the same intended result. Not saying that's bad, but I do feel it depends on the use you're applying it for and the OS environment (if any) under which you're running.

That's another thing which has been noted. The assembly instructions rely a great deal on addressing, because you have a finite amount of registers, and further, you desire efficiency in your operating code; therefore the compilers are designed to make use of existing data rather than alter a register every instruction.
 
1 members found this post helpful.
Old 11-06-2014, 12:19 PM   #17
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Original Poster
Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by rtmistler View Post
all of programming really boils down to read/modify/test/branch
Interesting thought. Can you provide a basic code snippet related to this outline?
 
Old 11-06-2014, 12:26 PM   #18
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by szboardstretcher View Post
Interesting thought. Can you provide a basic code snippet related to this outline?
Original code:

Code:
#include <string.h>

void main(void)
{
    char a[8], b[8];

    memcpy(a, "18675309", 8);

    memcpy(b, a, 8);
}
Bold/Red highlights of a few examples, but there are plenty throughout:

Code:
GAS LISTING /tmp/ccpS2bjq.s 			page 1


   1              		.file	"main.c"
   2              		.text
   3              	.Ltext0:
   4              		.globl	main
   6              	main:
   7              	.LFB0:
   8              		.file 1 "main.c"
   1:main.c        **** #include <string.h>
   2:main.c        **** 
   3:main.c        **** void main(void)
   4:main.c        **** {
   9              		.loc 1 4 0
  10              		.cfi_startproc
  11 0000 55       		pushl	%ebp
  12              	.LCFI0:
  13              		.cfi_def_cfa_offset 8
  14              		.cfi_offset 5, -8
  15 0001 89E5     		movl	%esp, %ebp
  16              	.LCFI1:
  17              		.cfi_def_cfa_register 5
  18 0003 83E4F0   		andl	$-16, %esp
  19 0006 83EC20   		subl	$32, %esp
  20              		.loc 1 4 0
  21 0009 65A11400 		movl	%gs:20, %eax
  21      0000
  22 000f 8944241C 		movl	%eax, 28(%esp)
  23 0013 31C0     		xorl	%eax, %eax
   5:main.c        ****     char a[8], b[8];
   6:main.c        **** 
   7:main.c        ****     memcpy(a, "18675309", 8);
  24              		.loc 1 7 0
  25 0015 C744240C 		movl	$926300209, 12(%esp)
  25      31383637 
  26 001d C7442410 		movl	$959460149, 16(%esp)
  26      35333039 
   8:main.c        **** 
   9:main.c        ****     memcpy(b, a, 8);
  27              		.loc 1 9 0
  28 0025 8D44240C 		leal	12(%esp), %eax
  29 0029 8B5004   		movl	4(%eax), %edx
  30 002c 8B00     		movl	(%eax), %eax
  31 002e 89442414 		movl	%eax, 20(%esp)
  32 0032 89542418 		movl	%edx, 24(%esp)
  10:main.c        **** }
  33              		.loc 1 10 0
  34 0036 8B44241C 		movl	28(%esp), %eax
  35 003a 65330514 		xorl	%gs:20, %eax
  35      000000
  36 0041 7405     		je	.L2
  37 0043 E8FCFFFF 		call	__stack_chk_fail
  37      FF
  38              	.L2:
  39 0048 C9       		leave
  40              		.cfi_restore 5
  41              	.LCFI2:
  42              		.cfi_def_cfa 4, 4
  43 0049 C3       		ret
GAS LISTING /tmp/ccpS2bjq.s 			page 2


  44              		.cfi_endproc
  45              	.LFE0:
  47              	.Letext0:
GAS LISTING /tmp/ccpS2bjq.s 			page 3


DEFINED SYMBOLS
                            *ABS*:0000000000000000 main.c
     /tmp/ccpS2bjq.s:6      .text:0000000000000000 main

UNDEFINED SYMBOLS
__stack_chk_fail

Last edited by rtmistler; 11-06-2014 at 12:27 PM.
 
1 members found this post helpful.
Old 11-06-2014, 12:28 PM   #19
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by NevemTeve View Post
High-level languages hide the pointers from you, but the pointers are still there.
Yeah, they hide them either completely or most of the functionality. With C you have all the power, and all the responsibility.
 
Old 11-06-2014, 04:58 PM   #20
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
If languages didn't try to hide pointers so much, no one would be confused about why int is passed by value but java.lang.Integer by reference. If one simply realises that “int foo;” declares an integer and “java.lang.Integer foo;” declares a pointer to java.lang.Integer instance, all confusion is gone. To Java defence they call it a reference, but still the “there are no pointers” mindset seems to create more confusion than it's worth. This is why I find tying concept of pointers to “low level” counter-productive.

Quote:
Originally Posted by szboardstretcher View Post
Interesting thought. Can you provide a basic code snippet related to this outline?
For theory, read up on Turing machine.

Last edited by mina86; 11-06-2014 at 05:00 PM.
 
1 members found this post helpful.
Old 11-06-2014, 05:08 PM   #21
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by mina86 View Post
If languages didn't try to hide pointers so much, no one would be confused about why int is passed by value but java.lang.Integer by reference. If one simply realises that “int foo;” declares an integer and “java.lang.Integer foo;” declares a pointer to java.lang.Integer instance, all confusion is gone. To Java defence they call it a reference, but still the “there are no pointers” mindset seems to create more confusion than it's worth. This is why I find tying concept of pointers to “low level” counter-productive.
Well, that's a detail I did not know about, I'm not much into Java.
 
Old 11-06-2014, 11:37 PM   #22
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by rtmistler View Post
Obviously for the stack this is important. If you have a structure, array, or string which is large, you don't pass a copy of that structure through your call stack, you instead pass the address of it. This therefore reduces the stack size requirements.
This is not as big an issue as it was in the early days of C programming when stack space was usually limited. In fact, early versions of C did not permit the passing of structures and arrays as parameters; you had to use pointers instead.

Time is more the limiting factor now. It takes time to copy large chunks of data to the stack space and the use of pointers can save that time.
 
Old 11-07-2014, 02:09 AM   #23
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by mina86 View Post
If languages didn't try to hide pointers so much, no one would be confused about why int is passed by value but java.lang.Integer by reference. If one simply realises that “int foo;” declares an integer and “java.lang.Integer foo;” declares a pointer to java.lang.Integer instance, all confusion is gone. To Java defence they call it a reference, but still the “there are no pointers” mindset seems to create more confusion than it's worth. This is why I find tying concept of pointers to “low level” counter-productive.


For theory, read up on Turing machine.
very true,
Sun has spent millions $ to convince every one that Java is the way to go and that students should not be irritated with low level stuff and they made a huge marketing camping using the absurdest arguments why people should not use C and C++.
the outcome of this can be seen today
 
Old 11-07-2014, 03:22 AM   #24
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Pointers can also be used as a hack to return multiple data types or multiple values as a result of a void function.
 
Old 11-07-2014, 06:26 AM   #25
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by psionl0 View Post
This is not as big an issue as it was in the early days of C programming when stack space was usually limited. In fact, early versions of C did not permit the passing of structures and arrays as parameters; you had to use pointers instead.

Time is more the limiting factor now. It takes time to copy large chunks of data to the stack space and the use of pointers can save that time.
Depends what you're programming on. An embedded microcontroller does not have unlimited resources.
 
Old 11-07-2014, 07:04 AM   #26
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,635
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
And you've also got to bear in mind where these languages started from. It all started with binary switches; then it evolved to punched paper tape and cards, upon which binary instructions were punched by hand to fill the few-hundred bytes of memory that machines of that time contained. Time marched on, and the first "compilers" were written, thus proving to their detractors that machine instructions could be generated using ... machine instructions.

"C" was literally an extension of assembly, and it was the first tool used to write a complete operating system (Unix, pun intended), for a cast-off machine in a laboratory, at a time when MULTICS was getting all the dough, credibility and $attention$. (At the time, no one believed that an operating system could be written at all in a high-level language.)

Languages since that time have worked to make source-code easier to write: to push more of the responsibility, both for code generation and for error-detection, onto the compiler/interpreter. (And, in a parallel path occupied e.g. by LISP and Prolog, to make certain classes of problems easier to address.) "C" remains as "the language that most of those other systems were written in." It's a linchpin of the reason why Linux et al are transportable to so many different platforms.

Although you might not wish to write new code in "C," you could spend the rest of your career maintaining a portion of the vital code that has been written using it. (Such as, say, Linux ...)
 
Old 11-07-2014, 07:51 AM   #27
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by sundialsvcs View Post
"C" was literally an extension of assembly, and it was the first tool used to write a complete operating system (Unix, pun intended), for a cast-off machine in a laboratory, at a time when MULTICS was getting all the dough, credibility and $attention$. (At the time, no one believed that an operating system could be written at all in a high-level language.)
Your last phrase makes little sense in context. Most of ITS was written in B and the designers of MULTICS were quite familiar with ITS. B was lower level than C, but still quite a bit above assembler.

MULTICS was written in a higher level language than C and that was always its plan. It was funded and started long before UNIX (though arguably MULTICS didn't reach a "working" level until after UNIX). If no one believed an OS could be written in a high level language, why was MULTICS funded?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Freeing pointers to pointers devnull10 Programming 24 07-26-2012 04:58 AM
pointers erat123 Programming 8 06-14-2007 11:54 PM
c pointers andystanfordjason Programming 3 04-22-2007 04:23 PM
Help with C pointers linuxlover1 Programming 13 07-05-2006 06:41 PM

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

All times are GMT -5. The time now is 05:36 PM.

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