Advertisement

pointer question

Started by December 23, 2002 11:17 AM
12 comments, last by zackriggle 21 years, 10 months ago
How do I tell if a pointer has memory allcated as an array, or a pointer to a single item. Also, will this work to delete any memory allocated for a pointer [char *buffer].
  
if(buffer != NULL)
{
	if(sizeof(buffer) == 1) // we use 1 because a char is 1 byte

		delete buffer;
	else 
		delete []buffer;
};
  
Also, are pointers created as NULL? ================== My (soon-to-be) Cherished Cookie of Appreciation: -- MattB - for WinSock advice --
Hi,

buffer is not automatically set to NULL, you will have to do this yourself.

sizeof(buffer) will not return the size of the buffer you allocated with new, it will always return 4 because this is the size of a pointer variable (regardless of what it points to).

If buffer is allocated as an array then use delete [], otherwise use delete. If you dont know, delete will work for both, but really you should know if it''s an array or not.
Advertisement
First, pointers should be initialized to NULL, like this:

void * pointer = NULL;

They should also be set to null when deleted.

delete pointer;
pointer = NULL;

sizeof(pointer) == 4, no matter what data the pointer is pointing to. The pointer variable itself is of type long, which is 32-bit (or 4 bytes), so its size will always be 4.

sizeof(*pointer) is the size of the object pointed to by the pointer. However, in the case of an array, the object pointed to by the pointer is not the array itself, but the FIRST object in the array (in your example, it''s a char, so this size would be 1 even for a 1k-char array).

As a rule of thumb, you have no way to know how many objects there are inside an array, unless you STORE that information yourself along with the array( in a struc, for instance).

Hovever, there are some objects (such as std::vector) that store objects as an array, but keep track of length, and can resize themselves when needed.

I might be wrong, but I believe []delete and delete have the same effect on a single-element array, So you can use []delete safely in all cases.

The method I would advise is to create your array using malloc and free, like this:

char * buffer = (char *)malloc(size_needed);
//Use buffer here
free((void *)buffer); //(is that cast necessary?)

void * malloc(size) returns a pointer to a chunk of data of the requested size. You may then safely cast it to any other kind of data (here, char). free() is used to "delete" memory allocated through malloc(). Using "delete" on these pointers might have weird results.

In the above example, buffer would behave as a normal array of char; You would be able to read buffer, or pass buffer as a normal LPCSTR pointer argument to any function (such as strcmp).

Hope this helped.
No, delete will NOT work for both. The behavior of a delete operator acting on an array is undefined, and therefore dangerous and probably memory-leaky.

You should never lose track of whether something is an array or a single object.
You cannot tell if a pointer has memory allocated. You must keep track of that yourself. The easiest way to do that is to set the pointer to NULL on creation and to check against NULL.

I would HIGHLY suggest that if you do not need the power pointers give you, you do not use them. Instead use std::vector, referances, or std::auto_ptr.
If you''re not afraid of wasting a little memory, you could always make pointers to single items one-element arrays. Then you could always use delete []. Honestly though, you should always know such basic information about your pointers.

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Advertisement
Thanks, all of ya. I did this mainly for one function, but figured it would come in useful sometime later. What the function would do is read from an open WinSock connection (or file, doesn''t matter) specified by the first parameter. The second paramter would be a char*& for the output of the data read. I just thought that at some point in time, a pointer that was not initialized would be sent to the function. I also figured that if you call delete[] on a wild pointer, some scary shit can happen. The array-or-just-one-element part was just something else I threw in there as a just-in-case-some-other-kid-does-something-stupid-while-using-my-program type scenario.

Thanks though. I''ll just have to take it for granted that the pointers are set to NULL before being passed into the function (or we will have a huge memory leak. Like 5K/second big. When you''re playing online for 3 hours, that leads up to [gets calculator] a 50MB memory loss).

==================
My (soon-to-be) Cherished Cookie of Appreciation:

-- MattB - for WinSock advice --
If you can find a way to make the output pointer the return value of the function, that would make it much safer.
Very good idea, but i have no clue how to do it. Any suggestions? Also, I thought of running strlen() on the pointer, since I would be dealing just with char*''s. Would this solve any problems (does a wild char* return a length of 0?)

==================
My (soon-to-be) Cherished Cookie of Appreciation:

-- MattB - for WinSock advice --
char* ReadFromWinsock(...); 


What's the problem with something like that? You can get around not knowing the length of the buffer by terminating it with a NULL character like a C-style string.

EDIT: I believe that strlen() returns the number of characters in a buffer until the first NULL character. So the length of a string in a wild pointer is undefined.

[edited by - micepick on December 23, 2002 2:21:41 PM]

This topic is closed to new replies.

Advertisement