Sponsored Content
Full Discussion: restricted pointers
Top Forums Programming restricted pointers Post 302171876 by carl.alv on Friday 29th of February 2008 10:34:22 PM
Old 02-29-2008
restricted pointers

Hi all. I am trying to use restricted pointers to allow the gcc compiler optimize the code, but I have not been able to make it work so far. I am testing with this code:

Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

void vecmult(int n, int * restrict a, int * restrict b, int * restrict c)
{
  int i;
  for (i=0; i<n; ++i) {
    a[i] = b[i] * c[i];
  }
}

int main(){
  int Nsteps = 100000;
  int n = 1000;
  int* a=NULL; 
  int* b=NULL;
  int* c=NULL;

  //allocate memory
  a = malloc(n*sizeof(int));
  b = malloc(n*sizeof(int));
  c = malloc(n*sizeof(int));

  //initialize arrays
  for(int i = 0; i < n; ++i){
    a[i] = i;
    b[i] = 1;
    c[i] = 0;
  }

  //initialize time
  struct timeval tim;
  gettimeofday(&tim, NULL);
  long tcpu = clock();

  for(int i = 0; i < Nsteps; ++i){
    vecmult(n, a, b, c);
  }

  //time difference evaluation
  double t1 = tim.tv_sec + tim.tv_usec / 1000000.0;
  double start = (double)(tcpu);
  gettimeofday(&tim, NULL);
  double t2 = tim.tv_sec + tim.tv_usec / 1000000.0;
  tcpu = clock();
  double stop = (double)(tcpu);
  double t_elap = (t2 - t1);
  double t_cpu = (stop - start) / 1000000.0;
  //print
  printf("%f %f\n",t_elap, t_cpu);

  //deallocate memory
  free(a);
  free(b);
  free(c);

  //show that restrict does not work
  int l = 1;
  int* restrict p1=NULL;
  int* restrict p2=NULL;
  p1 = &l;
  p2 = p1;

  return 0;
}

The gcc-4.2 compiler does not even complaints for the last part with the -O3 and -std=c99 options. Dues someone knows how to make restricted pointers work? Thanks in advance.
 

10 More Discussions You Might Find Interesting

1. Programming

Pointers to Arrays

Below is the program i tried to execute...... main() { static int a = {0,1,2,3,4}; static int *p = {a, a+1, a+2, a+3, a+4}; printf (“\n %u %u %d”, p, *p, *(*p) ); } This works, but i wanted to know why both a and *p are declared as "static". If we dont declare a as static... (2 Replies)
Discussion started by: Jayathirtha
2 Replies

2. Cybersecurity

Restricted user

A few of the other employees here need to learn "vi" in order to use crontab to schedule / unschedule jobs on one of our production servers. I tried to set up a login on one of my Linux boxes for them to use "vimtutor", but scrapped it and decided to try for something more secure, since there are... (1 Reply)
Discussion started by: LivinFree
1 Replies

3. UNIX for Dummies Questions & Answers

Restricted Shells. . .

Hey, Could someone please help me distinguish between a captive account and a restricted shell? Many thanks, L. (0 Replies)
Discussion started by: crispy
0 Replies

4. Programming

pointers

is this a valid c declaration int (*ptr(int *b)); plz explain... (4 Replies)
Discussion started by: areef4u
4 Replies

5. UNIX for Advanced & Expert Users

HP-UX Restricted SAM

I am having trouble getting restricted SAM to work by allocating Privileges to a "group". I can make it work by allocaing to the userid's, but would preferre to use the "group" option. Running B.11.11 Tried the following.... 1) Created a user group in /etc/group first 2) Used the... (1 Reply)
Discussion started by: Andrek
1 Replies

6. Programming

pointers

Hi I mash with pointers in C. I solve this problem about 5 hours and I don't know how I should continue. void InsertFirst (tList *L, int val) { tElemPtr new; if((new = malloc(sizeof(tElemPtr))) == NULL) Error(); new->data = val; new->ptr = L->frst; L->frst = new;... (2 Replies)
Discussion started by: Milla
2 Replies

7. HP-UX

Restricted Login ID

I have a task to create a new id that going to used by vendor. The ID profiles are: 1)The ID to be created is MUST be restricted sharing. 2)create for their own user account 3)unable to read/navigate/view other directories 4)unable to write to other directories able to create their own... (3 Replies)
Discussion started by: yeazas
3 Replies

8. Programming

Need help with the Pointers in C

I have a special character called ô. When it is declared as a character variable its showing it can be printed. But when it is declared as a character pointer variable its showing it cannot be printed. I am just wondering why its happening like this.. c1 = '@'; c2 = 'ô'; char *fp; fp="XXô"; if... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

9. AIX

Restricted shell

Hello I have a user with a Restricted Shell on a Aix 5.3. My question is about if I can add one more path on the home directory. I mean the user have in his home directory. for example /test/my_application/logs but I need that this user can view another log that is on another path, if this... (1 Reply)
Discussion started by: lo-lp-kl
1 Replies

10. AIX

Restricted user

Hello everyone I need to create a restricted user or a special user. Here is what I need. I have this path next to profiles I have server1, server2, server3....... The user that I have to create I need that he can move between server1. server2, server3 because in every one have logs... (1 Reply)
Discussion started by: lo-lp-kl
1 Replies
XPRINTF(3)						   BSD Library Functions Manual 						XPRINTF(3)

NAME
asxprintf, dxprintf, fxprintf, sxprintf, xprintf, vasxprintf, vdxprintf, vfxprintf, vsxprintf, vxprintf -- extensible printf SYNOPSIS
#include <printf.h> int asxprintf(char ** restrict ret, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int dxprintf(int fd, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int fxprintf(FILE * restrict stream, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int sxprintf(char * restrict str, size_t size, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); int xprintf(printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, ...); #include <stdarg.h> int vasxprintf(char ** restrict ret, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vdxprintf(int fd, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vfxprintf(FILE * restrict stream, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vsxprintf(char * restrict str, size_t size, printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); int vxprintf(printf_domain_t restrict domain, locale_t restrict loc, const char * restrict format, va_list ap); DESCRIPTION
These extensible printf (see xprintf(5)) variants behave like their normal printf counterparts (see printf(3)) without 'x' in the name (except sxprintf() and vsxprintf() behave like snprintf() and vsnprintf(), respectively). The domain argument must be a pointer to a printf domain structure, as returned by one of the functions described in xprintf_domain(3). The loc argument should be an extended locale (see xlocale(3)) or NULL, which means to use the current locale in effect (either the per-thread locale if set, or the global locale by default). SEE ALSO
printf(3), xlocale(3), xprintf_domain(3), xprintf(5) Darwin Aug 19, 2012 Darwin
All times are GMT -4. The time now is 01:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy