ts-7000
[Top] [All Lists]

Re: [ts-7000] malloc()

To:
Subject: Re: [ts-7000] malloc()
From: "Don W. Carr" <>
Date: Wed, 25 Oct 2006 09:54:19 -0500
No, all pointers are treated differently. With a void pointer, you can only copy bytes to or from that location using memcpy, or other functions. If you know what type you are going to store after calling malloc, you should cast it to that type. If you need to manipulate the memory and walk through, it is typical to cast it to a character pointer. With a void pointer, you can not add to it or increment it, since that the compiler does not know the size of what you are pointing to. An increment, will always increment by the size of what you are pointing to, as shown in the code below, ptr++ will increment the pointer by 4 bytes to point at the next floating point number that was allocated.

float *ptr;

ptr = (float *) malloc(10 * sizeof(float));

ptr++;

If you are going to use pointers, it is important that you take care with what you are doing, and you understand very well how they work. Also, try to isolate the code that does pointer manipulation so that you do not have pointer manipulation mixed throughout your code.

Don.

On 10/25/06, Jason Stahls <> wrote:

Ok, I had this working for a while then started mucking and broke it again.  Question is, why are you casting the returns to char *, I've always understood that a pointer is a pointer, it has no type, am I wrong?

Jason

Joe Bouchard wrote:

Now, a co-worker has raised a question about if this is a result of me 
misusing strtok_r(), I think I'm using it correctly but not sure.
Yeah, I can see a couple of issues. I'm surprised you aren't getting segfaults. When using both strtok and strtok_r it's a 2 step process. Typically the first call is before the loop, and subsequant calls are inside a loop. char * p = strtok(char * s, char * delimiters); p = strtok(NULL, delimiters); In the first call you give it the real "s", and after that, you give it NULL. No it doesn't make sense. I didn't write it, but that's how it works. And remember that it destroys your original string, so if you need to keep it for another use, you need to make a copy (which you were doing). The thread safe version is strtok_r, and it's similar, except you need to allocate another buffer, and pass that as the last parameter. Here is a working example: int main(void) { char msg[] = "SEE:SPOT:RUN"; char msg2[]= "RUN:SPOT:RUN"; char * place_keeper = (char*)malloc(1024); // strtok uses only the original string and a spare pointer. // it looks like this the first time with strtok char * p = (char *) strtok(msg, ":"); if (p) printf("Command=%s\n",p); while(p) { // it passes NULL on subsequent calls p = (char*)strtok(NULL, ":"); if (p) printf("Command=%s\n",p); } printf("------------------------------------------\n"); // strtok_r uses the original string, // and a spare pointer, and another buffer // it looks like this the first time with strtok_r p = (char*) strtok_r(msg2, ":", place_keeper); if (p) printf("Command=%s\n",p); while(p) { // it passes nulls on subsequant calls. p = (char*)strtok_r(NULL, ":", place_keeper); if (p) printf("Command=%s\n",p); } free(place_keeper); return 0; } Here is the result. Command=SEE Command=SPOT Command=RUN ---------------------------------------
---
Command=RUN
Command=SPOT
Command=RUN



Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/ts-7000/join (Yahoo! ID required) <*> To change settings via email:
m("yahoogroups.com","ts-7000-fullfeatured");" target="_blank" > <*> To unsubscribe from this group, send an email to: <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/




--
Dr. Don W. Carr
J. G. Montenegro 2258
Guadalajara, Mexico
+52-333-630-0704
+52-333-836-4500 ext 2930 __._,_.___


SPONSORED LINKS
Single board computer Hardware Computer running slow
Linux os Single board

Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: =Email Delivery: Digest | m("yahoogroups.com?subject","ts-7000-fullfeatured");=Change Delivery Format: Fully Featured">Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | =Unsubscribe

__,_._,___
<Prev in Thread] Current Thread [Next in Thread>
Admin

Disclaimer: Neither Andrew Taylor nor the University of NSW School of Computer and Engineering take any responsibility for the contents of this archive. It is purely a compilation of material sent by many people to the birding-aus mailing list. It has not been checked for accuracy nor its content verified in any way. If you wish to get material removed from the archive or have other queries about the archive e-mail Andrew Taylor at this address: andrewt@cse.unsw.EDU.AU