4

I am working on an application (C++ language on visual studio) where all the strings are referred with integer pointer.

For example, the class I am using has this integer pointer to the data and a variable for size.

{
    ..
    ..
    unsigned short int *pData;
    int iLen
}

I would like to know

  1. Are there any advantages of using int pointer instead of char pointer?

    After thinking a lot, I suspect that the reason may be to avoid the application crash which may happen if the char pointer is used without a null termination. But i am not 100% sure.

  2. During debugging, how can we check the content of the pointer, where the content is a char array or string (on Visual studio).

    I can only see the address when I check the content during debugging. because of this i am facing difficulty in debugging.

    Using printf would work to display the content but I can't do it in all places.

    i am suspecting that the reason for using integer pointer may be to avoid the application crash which may happen if the char pointer is used without a null termination. But i am not 100% sure.

may be to avoid such programmatic errors it is taken as integer pointer.

the class i am using has this integer pointer to the data and a variable for size.

{

..

..

unsigned int *pData;

int iLen

}

Please correct me if you think this can not be the reason.

13
  • 3
    Why are you using an int pointer? Commented Sep 27, 2016 at 6:30
  • 4
    1. No. 2. Fix the data type in the code. Commented Sep 27, 2016 at 6:30
  • 1
    I don't see how this would achieve anything useful in either C or C++ (also please decide which language you are using, they have sufficient differences to require different treatment) Commented Sep 27, 2016 at 6:33
  • 2
    If they are wide strings then this is correct behaviour (in the MS comipler,, wchar_t is a typedef for unsigned short int) Commented Sep 27, 2016 at 6:57
  • 2
    You would get better answers by posting a code sample Commented Sep 27, 2016 at 6:57

3 Answers 3

1

Please let us know which language you're using so that we can help you a bit better. As for your questions:

  1. There is no benefit of using an int array vs. char array. In fact having an integer array takes up more space (if each int represents its own character). This is because a char takes up one byte where an integer takes up four.

  2. As for printing things when debugging I'm not a master of visual studio since I haven't used it in some time but most modern IDEs allow you to cast things before you print them. For example in lldb you can do po (char)myIntArray[0] (po stands for print). In visual studio writing a custom visualizer should do the trick.

Sign up to request clarification or add additional context in comments.

4 Comments

The standard does not actually specify the size of either char or int, it is compiler dependent (although the sizes you mentioned are correct for most modern compilers)
Sorry to miss out the language used. it is C++ & C. i have edited my question to include language. Thanks for the answer. i ll look through the custom visualizer.
@Sagar I already commented on your question (and this is what Majster wanted to have clarification on) that C and C++ are different programming languages that require different treatment (and different code). Please decide which one you are using.
@UnholySheep yes, I know that it's compiler dependent and it's good that you pointed this out. I'm using the "standard" values only for the sake of simplicity and ease of understanding.
1

I am not sure why you would want to do this, but if you wanted to store an EOF character in your string for some reason, you would need a pointer to int.

Comments

1

MS Visual Studio often uses UTF-16 to store strings. UTF-16 requires a 16-bit data type, your application uses unsigned short for that, while a more correct name would be uint16_t (or maybe wchar_t, but I am not sure about that).

Another way to store strings uses UTF-8; if you use this way, use a pointer to char (not convenient) or std::string (more convenient). However, you don't have any choice here; you are not going to change how your application stores strings (it's probably too tedious).

To view your UTF-16-encoded string, use a dedicated format specifier. For example, in a quick-watch window, enter:

object->pData,su

instead of just

object->pData

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.