Sign in to follow this  
3dmodelerguy

using char array to stores ints?

Recommended Posts

I am following the tutorials here: http://www.gamedev.net/reference/articles/article1686.asp and a little confused on something. Is it valid to store ints inside a char array? I mean it work and i can access the first element but when i try to access the second and any after the program just closes?

Share this post


Link to post
Share on other sites
Quote:
Original post by 3dmodelerguy
I am following the tutorials here: http://www.gamedev.net/reference/articles/article1686.asp


Don't. First of all, it's out of date. Second, the code is badly designed (as indicated by responses in the previous threads you've made about it). Third, there's no actual reason you'd create a scripting system within C++ these days. Get proper tools, and embed Python. Better yet, use C++ to extend a Python program.

Quote:
and a little confused on something. Is it valid to store ints inside a char array? I mean it work and i can access the first element but when i try to access the second and any after the program just closes?


What exactly are you trying to do? Show the code.

You can't store ints in a char array. A char array holds chars. However, you can interpret a char as an int, in several different ways. They're both integral, numeric types, after all. That we use 'char' to represent elements of a string is actually kind of coincidental.

Share this post


Link to post
Share on other sites
Think of it like this; trying to hold 2 tons of rock in a container only capable of holding half a ton.

Char = 1 byte (8-bits).
Integer = 4 or 8 bytes (32 or 64-bits) depending on the CPU/OS you are compiling for.

Share this post


Link to post
Share on other sites
Quote:
Original post by 3dmodelerguy
I am following the tutorials here: http://www.gamedev.net/reference/articles/article1686.asp and a little confused on something. Is it valid to store ints inside a char array? I mean it work and i can access the first element but when i try to access the second and any after the program just closes?


First, you need to learn how to use a debugger.

The problem you are probably having here is an alignment issue. A char is one byte. An int can be two or four bytes (depending on your platform).

Your char array will start on an even boundry memory address. The second element will be at address+1 (an odd number). You can only write ints (words) to an even address. If you therefore try to write an int to your second (byte) element it will cause an exception (and crash your code).

So for example

int _tmain(int argc, _TCHAR* argv[])
{
char *c = "test";
int *i;

int x = 100;
c = c + 1;
i=(int *)c;
*i = x;

return 0;
}


will crash due to an unaligned write.

Share this post


Link to post
Share on other sites
Quote:
Original post by OldProgie2
You can only write ints (words) to an even address. If you therefore try to write an int to your second (byte) element it will cause an exception (and crash your code).

So for example
*** Source Snippet Removed ***
will crash due to an unaligned write.
While it's correct that unaligned reads/writes are not precisely the best practice, your fears are not generally true.
It will cause an exception on some processors, but not on others. For example, on x86 compatible processors (which are quite common) it will be no issue at all. (Note: it may be slightly slower, but that's it.)

Share this post


Link to post
Share on other sites
Quote:
While it's correct that unaligned reads/writes are not precisely the best practice, your fears are not generally true.

The fact that the original OP's code was crashing demonstrates that there is a problem.
Quote:

It will cause an exception on some processors, but not on others. For example, on x86 compatible processors (which are quite common) it will be no issue at all. (Note: it may be slightly slower, but that's it.)

It will be an issue even if one of performance (which you noted).

There is also the further issue that if the processor was using 4 byte int's and no exception was generated (or was handled) in the given example, the write would overwrite the null terminator of the string, causing other potential problems.

My point was, bad practice or not, writing an int value to a char array is probably unsafe, especially in the hands of an inexperienced programmer. There are valid cases where you could/would write ints to char arrays, but explaining all the nuances would probably only confuse a new programmer.

Share this post


Link to post
Share on other sites
Quote:
The fact that the original OP's code was crashing demonstrates that there is a problem
That's right, but it may of course have a different reason too. We've seen no code and don't know what's going on really.

I think some of the problem also comes from either all us not addressing the question correctly or the question not being asked "right".

Is it valid to store ints inside a char array? First, of all, one must ask "what is 'valid', and what are the other conditions".

Storing an int in a char array will cast int to char, throwing away some information, and write out a char.
char_array[i] = integer_value; is perfectly valid in the sense that it will compile (although possibly with a "loss of accuracy" warning), and it will never crash (other array-related stuff like out-of-bounds may still crash you of course).
However, it is *not* valid in the sense that it will preserve your values. Your values will only be the same if you have an additional condition like "I know that my integers are always numbers between 0 and 50" (anything that fits into 8 bits, so 0-255u will be accurately preserved).

i=(int *)c; as in your snippet uses a punned pointer, which is not the same as "store ints inside a char array" but more "writing an int to an int pointer that accidentially aliases a char pointer". It is nevertheless a perfectly valid thing in some situations, even though it has its gotchas and probably isn't what most people would want to do most of the time. Also, as pointed out before, if it's unaligned, it may crash on some architectures.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this