Memory Allocate new

Started by
22 comments, last by monkeyboi 11 years, 9 months ago

char* ta = new char[4];
int size = strlen(ta);
char* tb = new char[4];

Set a break point and monitor the memory address and context.

+ ta 0x02059318 "????????" char *


+ tb 0x02059358 "????????" char *


size 16 int

That is not what I am expecting. I only allocate 4 char space(4 bytes) for ta pointing to. But it has 8 or more bytes. The interval between ta and tb is 40 in hexadecimal which is 64 decimal. How could I calculate this to byte? and Why I got 16 using strlen? I am using a 64 bit system

If you don't mind please explain it step by step.

Thanks in advance

Jerry
Advertisement
When you allocate memory it is (usually) filled with random garbage. Accordingly, calling strlen on any just allocated pointer is undefined. It might return any value. It might crash.

When allocating memory there is also the need to store memory management information somewhere. Where and how much is implementation defined, but placing it in front on the allocated block is one way to do it. That aside, there is no guarantee two successive news will allocate memory that is anywhere close to each other. Assuming new will usually allocate memory in a linear fashion (by no means guaranteed), even right after program startup the runtime library might have already been doing some allocations and deallocations and the first new could be allocated by reusing a hole.

Edit: In summary, strlen is not a viable method to check the length of a memory block. It does something completely different, that is return the length of a C string. Details of memory management are impossible to answer without talking about a specific compiler and build settings.
Your array variable is a char*, not strictly 4 chars. Pointers on a 64-bit system are 8-byte aligned. (according to wikipedia)

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)


When you allocate memory it is (usually) filled with random garbage

Ok should the garbage be filled inside of the space that is just allocated. Like the example I put earlier ta 0x02059318 "????????" char * there are 8 random charactors in ta. But I only allocate 4 chars using new char[4].

Your array variable is a char*, not strictly 4 chars.

I know in 64 bit system char* pointer itself is 8 byte. I want to use it point to a 4 chars space.

there is no guarantee two successive news will allocate memory that is anywhere close to each other.

em this is quite right. but I have tested it several times all the results show the interval is 40 in hexadecimal. I know this can not promise anything but still can explain something.
It was pure coincidence, that strlen returned 16. In this case, it could have returned zero, a million or even crashed your program. strlen reads the all the memory from the address you pass as its argument until the first null byte and returns the number of bytes it read. You allocated ta and put nothing in. strlen does not know how many characters you allocated. It's up to you to make sure, you never use more memory than you allocated. You get 8 random characters (which more likely are 16 random chars, just what your strlen returned, only most of them unprintable), because the first null byte in your memory is found after 8 (or 16) bytes.

If you write a null byte to the address pointed to by ta, strlen will return 0, and you the debugger will as well display an empty string. Try it:


char* ta = new char[4];
ta[0] = 0;
int size = strlen(ta);
If you need space for strings, consider using std::string instead.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

If you write a null byte to the address pointed to by ta, strlen will return 0, and you the debugger will as well display an empty string.

ok now I know strlen is very unreliable. And I try the code like this

char* ta = new char[4];
ta[5] = 0;
int size = strlen(ta);

ta[5] definitely beyond the original bound, but it still works and gets the result like

+ ta 0x021b9318 "???" char *
Just because it's outside of the array range doesn't mean it's outside of a memory block allocated to your program as a whole. You've probably overwritten memory that was allocated to your program by the OS.

Yo dawg, don't even trip.

strlen is not unreliable, it's just not meant to be used for what you are trying to use it.

But just as boogyman says, try to use standard library classes as much as possible.

Avoid "new" like the devil and if you know you need it, read up on smart pointers before.

This topic is closed to new replies.

Advertisement