function to read arguments

I am trying to make working a source codes from articls "Playing with OpenCL: Gaussian Blurring" and I found that the author did not include main file, so I try to make my own. I found that the variables which are assigned from arguments are not declared, but the main problem is that the function looks strange to me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "args.h"
#include "gaussian.h"
#include "refu/Time/rfc_timer.h"
#include "stdio.h"
#include "stdlib.h"

#include <CL/cl.h>

bool readArguments(int argc, char *argv[],char** imgname,uint32_t* gaussianSize,float* gaussianSigma)
{
    uint32_t i;
    char gSizeYes,gSigmaYes;
    gSizeYes = gSigmaYes = false;
    *imgname=0;
    /** read the arguments of the program **/
    for(i=1;i<argc;i++)
    {
        if(strcmp(argv[i],"-h")==0)
        {
            printf("\n%s\n",helpStr);
            return false;
        }
        /** read the image name **/
        else if(strcmp(argv[i],"-i")==0)
        {
            *imgname = argv[i+1];
            i++;
        }
        /** read the gaussian size **/
        else if(strcmp(argv[i],"-g")==0)
        {
            *gaussianSize = atoi(argv[i+1]);
            i++;
            //only accept odd sized gaussian
            if((*gaussianSize)%2 == 0 && (*gaussianSize) > 1)
            {
                printf("Argument Error: The size of the squared gaussian kernel has to be an odd number greater than one.\n");
                return false;
            }
            gSizeYes = true;
        }
        /** read the gaussian sigma parameter **/
        else if(strcmp(argv[i],"-s")==0)
        {
            *gaussianSigma = atof(argv[i+1]);
            i++;
            gSigmaYes = true;
        }
        else
        {
            printf("Unrecognized program argument given. The correct program usage can be seen below:\n%s",helpStr);
            return false;
        }
    }
    /** if arguments are not given take default value **/
    if(*imgname == 0)
        *imgname = DEFAULT_IMG_NAME;
    if(gSizeYes == false)
        *gaussianSize = DEFAULT_GAUSSIAN_SIZE;
    if(gSigmaYes == false)
        *gaussianSigma = DEFAULT_GAUSSIAN_SIGMA;
    //success
    return true;
}


As far as I understand this function, the variables are local. The function does return boolean but it does not have any reference. Do you understand how this function can work? It does not have references in the arguments:

 
bool readArguments(int argc, char *argv[],char** imgname,uint32_t*  gaussianSize,float* gaussianSigma)


Edit:
I have found the main.c file, just forget to copy. But it has errors and does not work to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main(int argc, char* argv[])
{

double units;
uint32_t gaussianSize;
float gaussianSigma;
char* imgname;//the name of the image file, taken by the arguments
//init the Refu library
rfInit("errorLog","infoLog");
//read in the program's arguments
if(readArguments(argc,argv,&imgname,&gaussianSize,&gaussianSigma)==false)
return -1;
//start a timer to time the CPU performance
RF_Timer timer;
rfTimer_Init(&timer,RF_TIMER_MICROSECONDS);
#ifndef MEASURE_COMM_OVERHEAD
//perform CPU blurring
if(pna_blur_cpu(imgname,gaussianSize,gaussianSigma)==false)//time it
return -2;
//query the timer for the CPU
units = rfTimer_Query(&timer,RF_TIMER_MICROSECONDS);
printf("CPU Gaussian blurring took %f microseconds\n",units);
//reset the timer for the GPU
rfTimer_Query(&timer,RF_TIMER_MICROSECONDS);
#endif
//perform GPU blurring and then read the timer
if(pna_blur_gpu(imgname,gaussianSize,gaussianSigma)==false)
return -3;
units = rfTimer_Query(&timer,RF_TIMER_MICROSECONDS);//time it
printf("GPU Gaussian blurring took %f microseconds\n",units);
return 0;
}


Error on the line:
rfInit("errorLog","infoLog");

1
2
3
main.cpp||In function 'int main(int, char**)':|
main.cpp|20|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
main.cpp|20|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|


How to fix it?
Last edited on
warning != error You should look at the rfInit() for that one.


Your function has pointer parameters. What does a pointer parameter do?

How does this fail?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

void foo( int * gaz ) {
  *gaz = 42;
}

int main() {
  int bar = 0;
  foo( &bar );
  std::cout << bar << '\n';
  return 0;
}



Have you noticed that C does not have references at all?
Ah I see. So I forgot about pointers.

Edit: here is declaration

i_DECLIMEX_ void rfInit(char* errstr,char* logstr,uint64_t size);

should I pass variable instead?
Last edited on
I did this:
1
2
3
char* errorLog;
char* infoLog;
rfInit(errorLog,infoLog);


warning: 'errorLog' is used uninitialized in this function [-Wuninitialized]|
The function takes a pointer variable. The function thus needs a memory address. Not just any address, but one that contains a character and has characters in consecutive locations as well, until there is a character that has value null.

Pointer is a variable. Variable stores a value. The value in pointer is a number that is interpreted as a memory address.

Quiz:
1.
1
2
char* errorLog;
char* infoLog;

What are the values of errorLog and infoLog now?

2.
1
2
char* errorLog = "errorLog";
char* infoLog = "infoLog";

What are the values of errorLog and infoLog now?

3.
1
2
char errorLog[] = "errorLog";
char infoLog[] = "infoLog";

What are the values of errorLog and infoLog now?


ad1 ) no value
ad2 )
main.cpp|19|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
W:\___NEW_PROJECTS\GaussianBlur\Gaussian_with_OpenCL\main.cpp|20|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
convertion from limited variable to unlimited. Value should be "errorLog" and "infoLog" but I think it's pointer to constant. Not sure.
3) This works. No error. I thought that when it is pointer required for the function arguments so I should declare it there. But obviously, there is a pointer to char array, because arrays cannot be passed by value.

3 more errors
\main.cpp|24|undefined reference to `readArguments(int, char**, char**, unsigned int*, float*)'|
\main.cpp|31|undefined reference to `pna_blur_cpu(char*, unsigned int, float)'|\main.cpp|40|undefined reference to `pna_blur_gpu(char*, unsigned int, float)'|

what this mean? I would guess that the header file with function was not included, but this one was.
#include "args.h"
contains the function readArguments
Included by main.c?


Q1. Yes, they have no value set. The value is undefined. You did try to pass such to function and got the "used uninitialized".

Q2. Same error as you had initially. You have const string literals and attempt to store their address in non-const pointers. A non-const pointer could be used to modify the memory that it points to, and an attempt to modify literal is undefined.

Q3. The array variables essentially refer to memory blocks that have been allocated in local scope. In other words, 'errorLog' can be used where a char pointer is expected. The initialization copies characters from a const string literal into the non-const array.
I have found some problems while I try to remake the function. While debugging in code::blocks strange things happen. I have added two new arguments -install and -load

arguments given:-install -image image1.BMP

what happens is that when I break/jump on the line 59:
 
if(strcmp(argv[i],"-install")==0)

the debugger skips the next lines:
1
2
3
4
5
6
7
8
9
{
    #define INSTALL 1
}
else
{
     int pom;
     pom = strcmp(argv[i],"-install");
     printf(" var pom = %s",pom);
}


even when I add break points into the branches, it does not enter.
This is very similar with the -image argument. I checked the Watch panel for actual values and argv[i] is set correctly to -install and then to -image. So I am confused why debugger skips the branches like the string were not found!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "include/args.h"
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

char helpStr[] = {
    "Usage:\n\t-h\t Displays this help message\n\t"
    "-i\tThe name of the input image for blurring\n\t"
    "-g\tThe size of the gaussian kernel. Default is 3\n\t"
    "-s\tThe sigma parameter of the gaussian. Default is 0.8\n"
};

char readArguments(int argc,
                   char *argv[],
                   char** imgname,
                   uint32_t* gaussianSize,
                   float* gaussianSigma)
{
    uint32_t i;
    char gSizeYes, gSigmaYes;
    gSizeYes = gSigmaYes = false;
    *imgname=0;
    // read the arguments of the program
    for (i=1 ;i<argc ;i++) {
        if (strcmp(argv[i],"-h") == 0) {
            printf("\n%s\n",helpStr);
            return false;
        }
        /** input image name **/
        else if(strcmp(argv[i],"-image ")==0)
        {
            i++; *imgname = argv[i]; continue;
        }
        /** gaussian size **/
        else if(strcmp(argv[i],"-gaussian")==0)
        {
            i++; *gaussianSize = atoi(argv[i]);

            //only accept odd sized gaussian
            if((*gaussianSize)%2 == 0 && (*gaussianSize) > 1)
            {
                printf("Argument Error: The size of the squared gaussian kernel has to be an odd number greater than one.\n");
                return false;
            }
            gSizeYes = true;
            continue;
        }
        /** sigma parameter for gaussian blur **/
        else if(strcmp(argv[i],"-sigma")==0)
        {
            i++; *gaussianSigma = atof(argv[i]);
            gSigmaYes = true;
            continue;
        }
        else
        {
            /** install to build kernels **/
            if(strcmp(argv[i],"-install")==0)
            {
                #define INSTALL 1
            }
            else
            {
                int pom;
                pom = strcmp(argv[i],"-install");
                printf(" var pom = %s",pom);
            }
            /** LOAD to load kernels **/
            // else
            if(strcmp(argv[i],"-load")==0)
            {
                #define LOAD 1
            }
            else
            {
            printf("Unrecognized program argument given. The correct program usage can be seen below:\n%s",helpStr);
            return false;
            }
        }
    }
    // if arguments are not given take default value
    if(*imgname == 0)
        *imgname = DEFAULT_IMG_NAME;
    if(gSizeYes == false)
        *gaussianSize = DEFAULT_GAUSSIAN_SIZE;
    if(gSigmaYes == false)
        *gaussianSigma = DEFAULT_GAUSSIAN_SIGMA;
    //success
    return true;
}
Last edited on
Topic archived. No new replies allowed.