# returning a void pointer to a chunk of a character array

This topic is 2119 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

OK I have an interesting project I'm working on that requires me to write a routine that will return a void pointer to a chunk of a character array. I'll be honest I'm kind of stumped right now on how to do this??? I can't give code as I'm sworn not to post anything from the project but the method takes a size and returns a void*.

Any ideas?????

##### Share on other sites
The information you provide is insufficient to answer your question correctly. Do you already have your data as a character array? Or will you have to allocate it on the heap in your function? Why does it have to be a void-pointer? There are only very few cases where a void-pointer is really needed. For example, why not return a char-pointer if you know it is char data?

##### Share on other sites

I can't give code as I'm sworn not to post anything from the project but the method takes a size and returns a void*.

So write some code that is not part of the project that demonstrates the immediate problem you're trying to solve in it's simplest form.

##### Share on other sites
What part is causing you difficulty? What have you tried?

##### Share on other sites
The “TLDR” form of your post is that you want to do something related to a size and a pointer, but it is a secret.
You failed to realize by yourself that you gave inadequate information for any kind of help at all, and the number of question marks in your post—3 of which were not actually following a question (unless you are asking whether or not you are stumped)—suggests your age is 14 or below (you may want to take this factoid into consideration if you want people to take you and your questions seriously in the future).

The “TLDR” of my post is that you don’t know what you are doing and you also don’t know how to ask questions to get help.
This is the worst possible combination. If you can’t ask proper questions you can’t learn.
I don’t know what your goals are in programming, but I sincerely think you should consider alternative career paths for the future.

I have no idea how to answer your question, but my guess is that you need to study “reinterpret_cast” if you are using C++, or “C casts” if you are using C.
I have no idea why you can’t just do, “return reinterpret_cast<void *>(pcCharBuffer);”.

L. Spiro

##### Share on other sites

The “TLDR” form of your post is that you want to do something related to a size and a pointer, but it is a secret.
You failed to realize by yourself that you gave inadequate information for any kind of help at all, and the number of question marks in your post—3 of which were not actually following a question (unless you are asking whether or not you are stumped)—suggests your age is 14 or below (you may want to take this factoid into consideration if you want people to take you and your questions seriously in the future).

The “TLDR” of my post is that you don’t know what you are doing and you also don’t know how to ask questions to get help.
This is the worst possible combination. If you can’t ask proper questions you can’t learn.
I don’t know what your goals are in programming, but I sincerely think you should consider alternative career paths for the future.

I have no idea how to answer your question, but my guess is that you need to study “reinterpret_cast” if you are using C++, or “C casts” if you are using C.
I have no idea why you can’t just do, “return reinterpret_cast<void *>(pcCharBuffer);”.

L. Spiro

Ok we are all professionals on here so it would be appreciated if you did not insult me just because my post did not include enough information. As far as not providing enough information I'm sorry I wrote that post later at night after a VERY long day. Not that it matters but I'm actually 29 years old not 14 as you suggested.

You want more information ok now that I'm awake and thinking clearly lets start with this. In the program I have a character array that has a fixed size set at the initialization of the program. The method resides in a class, the method takes in a size as its only parameter and returns a pointer to a chunk of the char array(if there is enough room left in the array) to the main application. Then within the main application it attempts to change the contents of the pointed to chunk.

I'm simply stumped as far as how to select a section of a character array the size of the size provided and return a pointer to just that section without returning a pointer to the whole thing. And I am not able to change how the method returns so like I said it is something like this void* method(int size).

I'm not asking for somebody to give me the whole answer handed to me on a silver platter but every attempt I've tried to google search, or go through my reference books I can't seem to find even the smallest hint as to how I would go about doing this.

So any ideas?

And sorry for the vagueness of my original post.

##### Share on other sites
 char buffer[65536]; char *lastAllocation = buffer; void* Allocate(int size) { void *result = lastAllocation; lastAllocation += size; return result; } 

Of course this has no error checking, no releasing of memory, etc etc

But it should be a fair representation of what you're trying to do and how to do it.

(Pardon my rusty C; it should be close enough)

##### Share on other sites
First, I'd like to agree with those before me. From a 'fellow professional' I would expect that they made a reasonable attempt to describe their problem in a, well, professional manner. Unfortunately the initial post was extremely far from that.

That aside, while you still do not make a very good job of explaining yourself, I guess that what you want to do is implement a simple memory pool? In the simplest case, keep track of an offset starting at 0. Return memory buffer + offset at each call and increment offset by size. If memory can be reclaimed by another function call this becomes much more interesting though.

On the other hand, that answer is so trivial that I assume I either misunderstood what you tried explaining or we are back to questioning your professionalism...

##### Share on other sites
Ok I'm not trying to start an agument, yes I agree my posts have not been extremely detailed. That being said insults don't need to be thrown around if we are all really professionals. You can tell me that my post is not giving adequate information without stating that I must be some younger person who doesn't know anything.

As far as my post being vague its vague for a reason the truth is that I'm working on a programming test, in the test it states that I can look for my answers in other places, of course I'm required to state my sources of the information that I get. But I am not to post the test anywhere even as a portfolio project. So I cant give full details of the project hence I'm simply looking for some general knowledge about as I stated returning a pointer to a section of a character array. You are correct its a memory pool of sorts. That is all I can give on the project.

I believe Telastyn your answer may be the realm that I'm looking for.

Thank you.

I do appologize again I have not meant to come off as unprofessional.

##### Share on other sites
Why would you use a void *' instead of a char *'? If both the class and the main program are aware that these are bytes we are pointing at, you don't need to play games using unsafe `void *'s.

• 34
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631355
• Total Posts
2999520
×