Thread: right way to use pointer notation?

  1. #1
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101

    Question right way to use pointer notation?

    Hi
    I am unsure about using pointers,so I usually stick to array[index]
    but now I want to make a function effecient by passing pointers and loop thru char's and do some math and if too big result pass 255 instead,do I really need if,or (char) does it for me?
    also unsure about change adress,or the data inside the adress
    Code:
    int blend(char *ptr1, char *ptr2,int tsize) {	int i, j,texture3;
    	
    	for (i = 0; i < tsize; i++) {
    		texture3 = (*ptr1 + *ptr2) / 2;//simplest blend together 50/50%
    		if (texture3 > 255) texture3 = 255;//max255
    		*(ptr1) =(char)texture3;//
    		++ptr1; ++ptr2;
    	}
    	return 1;
    }
    you tell me you can C,why dont you C your own bugs?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I am unsure about using pointers,so I usually stick to array[index]
    Well really in C++ you should strive to avoid pointers and arrays when possible, use reference and one of the standard containers instead.

    but now I want to make a function effecient by passing pointers
    What makes you think that "just" passing by pointer will make your function "efficient"? In most cases if you're passing an array you should just stick with array notation. Array notation usually better illustrates what is going on in your function. Also in this case it would remove the necessity for those "additional" increments inside your loop.

    Do you realize that in C++ (and modern C for that matter) it is considered a poor practice to declare all of your variables in one big glob at the beginning of some scope? It considered much better to declare your variables close to first use instead. Also in C++ you should use the "safer" C++ style casts instead of C style casts.

    Do your realize that in most situations today a char can not hold a value over 127?

    Why are you always returning 1? Why not just use a void return value?

    Lastly how are you insuring that your arrays have the same sizes?

  3. #3
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Jim,you mean I should use loadimage() and handles like in windows instead?
    I previous made a bmploader function and I want a function that blends 2 loaded images together,also have a todo list of functions that work on images
    so I should allocate a dynamic memory and load all image I need there and my loop inside blender use one pointer only and that pointer+offset to second image,seem logical to me
    thanks,missed on "unsigned char"

    good point checking imagewidth and height are the same in struct that hold info from fileheaders
    you tell me you can C,why dont you C your own bugs?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Jim,you mean I should use loadimage() and handles like in windows instead?
    Well since I really have no idea what loadimage() actually does or how Windows does anything, I would have to say not what I meant.

    I previous made a bmploader function and I want a function that blends 2 loaded images together,also have a todo list of functions that work on images
    so I should allocate a dynamic memory and load all image I need there and my loop inside blender use one pointer only and that pointer+offset to second image,seem logical to me
    Glad it seems logical to you, but I was only talking about that code you posted, not your overall "plan".

    good point checking imagewidth and height are the same in struct that hold info from fileheaders
    Okay, but again I don't see how this relates to the code you posted and the question you asked. And you didn't even bother to answer most of the questions I posed.

    For example why are you trying to use pointers?

    Do you realize that just because you use pointers doesn't magically make your function more efficient?

    Why aren't you using something like std::vector<unsigned char> instead of the arrays?

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    214
    @I C everything,

    @jimblumberg 's message goes deeper.

    However, there's no context. This is an isolated function.

    In C++ I'd expect to see a pixel, not a char or unsigned char pointing to an array. I'd hope at least to have a std::array instead of a pointer, and I'd hope to find it contains pixels not unsigned chars.

    Types are central to modern C++. The habit from C where the programmer "just remembers" that this pointer is an array, and that the unsigned char represents a pixel (or part of a pixel, lest this is a monochrome image), is now known to be a source of bugs and problems.

    Beyond what @jimblumberg pointed out, how does anyone know one of these aren't some C style string, or that the two images have the same format, let alone that the two arrays are the same size?

    One of the features of C++ is the ability to create types representing key facts which the compiler can enforce. As such, it wouldn't be prone to an error (or crash) if given two images of differing dimensions, or one image of 8 bit channels and another of 16 bit channels.

    Types can be used to select behavior based on such differences, and algorithms can be fashioned to either reject some incompatibilities, or to select adapters when required.

    C is still inside C++, and for a reason. C was purpose built to take the position of an assembler independent of the CPU, and in C++ we will use that level for performance.

    Yet, C++ programmers strive to encapsulate code which doesn't express types clearly for the purpose of performance. That's what @jimblumberg is pointing out.

    This code snippet hints there's no real C++ happening here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help converting to pointer notation
    By Dominiko in forum C Programming
    Replies: 5
    Last Post: 01-28-2018, 03:05 PM
  2. pointer notation
    By alien1 in forum C Programming
    Replies: 4
    Last Post: 12-02-2014, 06:19 PM
  3. Pointer notation help!
    By colmulhall in forum C Programming
    Replies: 4
    Last Post: 03-23-2011, 09:15 AM
  4. Pls Help: Insertion sort using pointer notation
    By mack_ol in forum C Programming
    Replies: 1
    Last Post: 02-17-2002, 01:03 PM
  5. manipulation of strings using pointer notation
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-08-2001, 12:23 PM

Tags for this Thread