Sponsored Content
Top Forums Programming Swap call by reference confusion Post 302889447 by yifangt on Thursday 20th of February 2014 04:03:47 PM
Old 02-20-2014
Thanks, I think this is the part I am not clear about:
You changed the values of X and Y, not their contents.
Does that imply if I want to change the contents of pointers X and Y, only expression *X = *Y must be used? Please confirm this, thank you!
Code:
#include <stdio.h>

void swap1 (int *x, int *y);

int main ()
{
  int a, b;
  a = 10; 
  b = 20;
  printf ("Original a = %d    b = %d\n", a, b);
  swap1 (&a, &b);
  printf ("After calling swap1, a = %d b = %d\n", a, b);
  return 0;
}

void swap1 (int *x, int *y)         //Compiled without error but did not work!
{
printf("Before swap, the values of x= %p, y = %p\n", x, y);
printf("Before swap, the contents of x= %d, y = %d\n\n", *x, *y);

  int t;
  t = *x;
  x = y;           // Line 23 Correct way is *x = *y; 
  y = &t;           //Line 24 Correct way is *y = t; But it did change the value of y, cf. the ./a.out part:  y = 0x7fffc542206c.

printf("After swap, the values of x= %p, y = %p\n", x, y);
printf("After swap, the contents of x= %d, y = %d\n\n", *x, *y);

}

When I checked the swap function, it seems x and y got swapped, but not a and b.
Code:
yifangt@mint $ ./a.out
Original a = 10    b = 20
Before swap, the values of x= 0x7fffc5422088, y = 0x7fffc542208c
Before swap, the contents of x= 10, y = 20

After swap, the values of x= 0x7fffc542208c, y = 0x7fffc542206c             //because y = &t Line 24
After swap, the contents of x= 20, y = 10

After calling swap1, a = 10 b = 20
yifangt@mint $

I have a strong feeling but very vague idea of the stack/heap for this swap-function calling in the memory, especially when the code did not work without an error nor any warning. Thanks a lot!
 

8 More Discussions You Might Find Interesting

1. Solaris

RAM and SWAP confusion

Hi Folks, This is my first post here - so nice to meet u all :-) Recently i was trying to dig a little bit into the memory structure of my machine and due to the lack of concept, cannot figure out a calculation. This is how it goes: 1) My swap slice is at the usual /dev/dsk/c0t1d0s1... (0 Replies)
Discussion started by: s4g3
0 Replies

2. Solaris

Swap config - Mirror swap or not?

Hello and thanks in advance. I have a Sun box with raid 1 on the O/S disks using solaris svm. I want to unmirror my swap partition, and add the slice on the second disk as an additional swap device. This would give me twice as much swap space. I have been warned not to do this by some... (3 Replies)
Discussion started by: BG_JrAdmin
3 Replies

3. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

4. HP-UX

Swap device file and swap sapce

Hi I have an integrity machine rx7620 and rx8640 running hp-ux 11.31. I'm planning to fine tune the system: - I would like to know when does the memory swap space spill over to the device swap space? - And how much % of memory swap utilization should be specified (swap space device... (6 Replies)
Discussion started by: lamoul
6 Replies

5. Programming

call by reference

Ive just started programming in C and am confused with the output of the code void main() { int a=10; int b=20; swapr(a++,b++); printf("%d %d",a,b); } swapr (int *x,int *y) { x+=2; y+=3; } the output of the code is 11,21 which is confusing. first its... (3 Replies)
Discussion started by: ra2000
3 Replies

6. UNIX for Dummies Questions & Answers

[Query] Confusion of the Swap when using 'free -m' command

Hi All, I have just installed my first Linux server ( Ubuntu 11.10 ). I am sure I didn't allocate /swap , and double check by 'df -h', yes really no /swap but when I use 'free -m' , returned a "Swap" line as below. total used free shared buffers cached Mem: ... (3 Replies)
Discussion started by: joaming
3 Replies

7. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

8. Solaris

Explain the output of swap -s and swap -l

Hi Solaris Folks :), I need to calculate the swap usage on solaris server, please let me understand the output of below swap -s and swap -l commands. $swap -s total: 1774912k bytes allocated + 240616k reserved = 2015528k used, 14542512k available $swap -l swapfile dev swaplo... (6 Replies)
Discussion started by: seenuvasan1985
6 Replies
poll_mouse(3alleg4)						  Allegro manual					       poll_mouse(3alleg4)

NAME
poll_mouse - Polls the mouse. Allegro game programming library. SYNOPSIS
#include <allegro.h> int poll_mouse(); DESCRIPTION
Wherever possible, Allegro will read the mouse input asynchronously (ie. from inside an interrupt handler), but on some platforms that may not be possible, in which case you must call this routine at regular intervals to update the mouse state variables. To help you test your mouse polling code even if you are programming on a platform that doesn't require it, after the first time that you call this function Allegro will switch into polling mode, so from that point onwards you will have to call this routine in order to get any mouse input at all, regardless of whether the current driver actually needs to be polled or not. RETURN VALUE
Returns zero on success, or a negative number on failure (ie. no mouse driver installed). SEE ALSO
mouse_needs_poll(3alleg4), install_mouse(3alleg4), mouse_x(3alleg4), exlights(3alleg4), exmouse(3alleg4), exshade(3alleg4), exs- pline(3alleg4), extrans(3alleg4) Allegro version 4.4.2 poll_mouse(3alleg4)
All times are GMT -4. The time now is 01:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy