Jump to content

  • Log In with Google      Sign In   
  • Create Account


returning a void pointer to a chunk of a character array


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 darcmagik   Members   -  Reputation: 103

Like
0Likes
Like

Posted 27 August 2012 - 10:34 PM

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?????
Darcmagik

Of all the things I''ve lost I miss my mind the most.

Sponsor:

#2 rnlf   Members   -  Reputation: 1047

Like
0Likes
Like

Posted 28 August 2012 - 12:23 AM

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?

my blog (German)


#3 krippy2k8   Members   -  Reputation: 638

Like
0Likes
Like

Posted 28 August 2012 - 12:40 AM

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.

#4 rip-off   Moderators   -  Reputation: 6855

Like
0Likes
Like

Posted 28 August 2012 - 02:57 AM

What part is causing you difficulty? What have you tried?

#5 L. Spiro   Crossbones+   -  Reputation: 9659

Like
4Likes
Like

Posted 28 August 2012 - 06:17 AM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#6 darcmagik   Members   -  Reputation: 103

Like
-1Likes
Like

Posted 28 August 2012 - 07:30 AM

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.
Darcmagik

Of all the things I''ve lost I miss my mind the most.

#7 Telastyn   Crossbones+   -  Reputation: 3692

Like
2Likes
Like

Posted 28 August 2012 - 07:52 AM

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)

#8 BitMaster   Crossbones+   -  Reputation: 2963

Like
0Likes
Like

Posted 28 August 2012 - 07:52 AM

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...

#9 darcmagik   Members   -  Reputation: 103

Like
-1Likes
Like

Posted 28 August 2012 - 08:10 AM

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.
Darcmagik

Of all the things I''ve lost I miss my mind the most.

#10 Álvaro   Crossbones+   -  Reputation: 10570

Like
0Likes
Like

Posted 28 August 2012 - 08:24 AM

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.

#11 Telastyn   Crossbones+   -  Reputation: 3692

Like
0Likes
Like

Posted 28 August 2012 - 08:35 AM

You know, part of taking a programming test is to evaluate your skills. That sort of thing is pretty elementary C.

And yeah... that comes off as completely unprofessional.

#12 Cornstalks   Crossbones+   -  Reputation: 6878

Like
0Likes
Like

Posted 28 August 2012 - 08:50 AM

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.

I downvoted this because a) I don't think it's cool that you're using other people to write your code for your test, b) you mislead people on this (by not stating it upfront), and c) you have shown no effort (see rip-off's post and the "What have you tried?" link) (I get it, you're not to share code from the project, but that's when you write throw-away code that you can share; even without the code though, you could've explained what you've tried/what you thought a valid solution might be).
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#13 darcmagik   Members   -  Reputation: 103

Like
-1Likes
Like

Posted 28 August 2012 - 09:48 AM

Ok I have to admit this morning when I came on hoping for some nugget of knowledge to lead me on the right path and I saw a thread full of basically insults from everybody on here I was very angry. I get it my post was not perfect I'm sorry for that. I was not trying to mislead anybody or attempting to cheat on the test I was simply a stressed out programmer trying to figure out this damn problem. I get the fact that if I can't figure out this problem on my own than I'm probably not ready for the job that I applied for, but that doesn't mean that I dont want to figure it out. Yes I probably should have been more descriptive and explained what I have tried. What I have tried is to wrap my head around what it was that I'm trying to do in this project. Its been a while since I've dealt with pointers and was stumped on how to go forward with this problem.

So I'm going to end this thread by saying what I guess I've had to say all along I'm sorry. I have been coming to this website for years, and have learned a lot but the way some of you act on this thread I'm some low life because I don't know every answer.... Thanks for making my day so much better.

Edited by darcmagik, 28 August 2012 - 09:48 AM.

Darcmagik

Of all the things I''ve lost I miss my mind the most.

#14 Oberon_Command   Crossbones+   -  Reputation: 1707

Like
1Likes
Like

Posted 28 August 2012 - 10:15 AM

So I'm going to end this thread by saying what I guess I've had to say all along I'm sorry. I have been coming to this website for years, and have learned a lot but the way some of you act on this thread I'm some low life because I don't know every answer.... Thanks for making my day so much better.


I don't think it's because you don't know every answer. It's more that it appears that you're trying to use GDNet as a source for "cheating" on a programming test. It may not be literal cheating, but as has been said, this kind of thing (if we understand what you're trying to do correctly) is simple enough that if you can't do it without our help, you probably don't deserve to pass the test. Personally, I would consider anything that helps one pass a test - when one does not deserve to - to be cheating, and judging by posts here, I'm not alone. In essence, we are not attacking you - personal attacks are definitely unprofessional - but it does appear that some are attacking your intention and your presentation, which are perfectly fair game and the latter is even expected in a professional environment. I often take solutions I invent to my colleagues at work specifically to have them "attack" my solution and point out all of the flaws in it.

If you had come to us and had said something along the lines of, "I got this question in a programming test, and though I wasn't able to solve it myself, I'm interested in what the answer is. I'd also like to know how to do this so that next time I see a question like this, I can solve it," then you would have received a much better reception. Writing in more formal English than you used would also have helped, as we tend to assume that people who have bad spelling/grammar or excessive punctuation (as in your case) are either non-native speakers of English or native-speaker teenagers unaware that forums are not texting. The former is permissible, if not ideal, but the latter looks lazy and unprofessional. There are many who would argue that writing in the correct register is part of being "professional."

#15 Cornstalks   Crossbones+   -  Reputation: 6878

Like
0Likes
Like

Posted 28 August 2012 - 10:21 AM

For the record, I'm not making any claims about you as a person or your work. I'm not saying you haven't put forward any effort (I'm saying you didn't show you have), I'm not saying you tried to mislead people (though it was misleading), and I'm not insulting you (though I am being critical). I can't make those claims. I'm just (and others have done this too) pointing out how you can be more effective and professional in the future.

Take this in stride and just use it as an opportunity to learn how to more effectively and professionally communicate in the future. Don't get offended (L. Spiro's analysis is indeed accurate; I don't think L. Spiro intended to offend you, and only intended to point out how/why your post is ineffective so that you can improve).
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#16 darcmagik   Members   -  Reputation: 103

Like
2Likes
Like

Posted 28 August 2012 - 12:13 PM

Ok I can respect everybody's opinion on here and yes I will agree my post was very unclear and from the outside looking in it looks very deceiving and like I was attempting to cheat on the test. Please allow me to again apologize for my actions on here and my all around bad thread that I started. That being said I would like to try to meet in the middle on this issue and try to get everybody to see it from my point of view, when you post a thread on here and the first four responses given on here are about how unclear and bad your post is it can make you a bit irritated. I'm not saying that my post deserved tons of help and information but one maybe two posts asking for clarification is fine, more than that and it feels like some are repeating what everybody else was saying just so they could have put their opinion in on the matter.

That being said I see that I need to read up on my pointer skills as in all of the studying I have done I have actually never run into an example or a programming project / exercise that covered the idea of returning a void pointer to a section of an array. I guess the idea never even crossed my mind as something that was possible.
Darcmagik

Of all the things I''ve lost I miss my mind the most.

#17 Trienco   Crossbones+   -  Reputation: 1944

Like
0Likes
Like

Posted 28 August 2012 - 10:27 PM

I think you are completely misunderstanding the "mission statement" of returning a pointer to a segment.

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.


You can't. A pointer doesn't point to a range/segment, it points to a "point". It's like having only one number and wanting it to reference a range of numbers: impossible.
f@dzhttp://festini.device-zero.de

#18 darcmagik   Members   -  Reputation: 103

Like
0Likes
Like

Posted 28 August 2012 - 10:34 PM

Yes true as I've been working on this I realize what I need to do is return a pointer to the beginning of the chunk being worked with.
Darcmagik

Of all the things I''ve lost I miss my mind the most.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS