Domanda

I don't know much about C++ (almost nothing), meaning I'm a noob at C++.

1.

Let's say you have this code:

typedef unsigned char u8;

With this code, does it mean that when you create a variable you can write u8 instead of unsigned char? Is an unsigned char a one byte value ranging from 0 to 255 or is it something else?

2.

Now I add something:

typdef unsigned char u8;

u8 *someVariable;
someVariable = new u8[12345];

What's the variable someVariable now? Is it a list/array with 12345 items where every entry is of type u8?

3. Adding some more:

typedef unsigned char u8;

u8 *someVariable;
someVariable = new u8[12345];

someVariable+=4;

What happens to someVariable now? Does it add 4 to every index in someVariable or only to a certain one? Or am I totally wrong with the list or array thing?

È stato utile?

Soluzione 3

  1. Yes you can write u8 stuff; instead of unsigned char stuff; given the typedef. Yes, it might range from 0 to 255. It might be bigger. See here

  2. In this example you have allocated an array (std::list or C++11's std::array is different) or unsigned chars (and don't seem to delete[] them)

  3. Adding a number to a pointer will affect the pointer, not what it points to, so the third example will move the pointer along to the fourth item, but not change any values.

Altri suggerimenti

  1. First part: yes. Second part: Not exactly. The Standard goes out of its way to specify exactly what it needs. Nowhere does it say that char and its signed/unsigned variants must be 8 bits, but they are on every platform you are likely to care about. (If I remember correctly, char must support at least the range [0,127], and can share the same representation as either its signed or unsigned version.)

  2. someVariable is a pointer to an array of 12345 u8s. The actual array exists somewhere else (on the free store, more commonly called the heap). If you want the array "right here", you just write u8 myArray [12345];.

  3. In this case, it moves the pointer to the array forward. A pointer to an array is basically the same thing as a pointer to its first element. So now you have a pointer to the 5th element (they are 0-indexed). But you can't delete the array through this new pointer.

1.

typedef unsigned char u8;

This means "u8" and "unsigned char" are the same. You've defined a new type (typedef) called u8 that is defined equal to unsigned char. An unsigned char is 1 byte (C++ defines this as at 8 bits or more) with no sign bit. In practical terms this means no negatives but also that some operations are defined which aren't defined for signed char.

2.

typdef unsigned char u8;

u8 *someVariable;
someVariable = new u8[12345];

someVariable is a pointer to memory holding a u8. In this case the memory it points to has space for 12345 contiguous u8s. Please don't start thinking pointers are the same as arrays or lists, it will serve you much better to get ideas like that out of your head now.

3.

typedef unsigned char u8;

u8 *someVariable;
someVariable = new u8[12345];

someVariable+=4;

The pointer someVariable now points to a different location in memory. It's been moved forward by 4 u8s. A u8 being 1 byte this is the same as moving forward in memory by 4 bytes, if your pointer was pointing at an unsigned int (normally 4 bytes) it would have moved forward 16 bytes.

1.

typedef unsigned char u8;

typedef is like alias, So you are right. You can replace unsigned char to u8 and most of the production code will have such typedefs

unsigned char is 1 byte (8 bits) can hold values from 0 to 255.

2. typedef unsigned char u8;

u8 *someVariable;
someVariable = new u8[12345];

someVariable is a pointer with size say 4 (depends on operating system). This variable will hold data of type u8, either unsigned char.

In your case its an array of size 12345. Hence the size of somevariable will be 12345 and can hold 12345 objects of 1 byte each.

  1. typedef unsigned char u8;

    u8 *someVariable; someVariable = new u8[12345];

    someVariable+=4;

here you are trying to do pointer arithmetic somervariable is just a pointer. For example, lets say some variable's address is 1000 then + 4 will move the variable to 1004, hence *somevariable now will result in value at 1004 .

practically its not advised to move pointers without storing the starting address in some temp variable. reason - after several pointer arithmetic we might loose track of the starting location and hence we might be pointing to some irrelevant address causing crash.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top