Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Pfhorseti

Help - delaying declaration of char array to compile time

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a class, where in it''s header declares a char pointer: char *myPointer; In it''s constructor I want to actually allocate the char array using user''s provided info (array size). So in it''s constructor (actually an -init method, written in Obj-C), I do: char allocatedTempBuffer[[[[NSUserDefaults standardUserDefaults] objectForKey:@"maximumPacketSize"] intValue]]; tempBuffer = allocatedTempBuffer; Above code is logically the same as: char allocatedTempBuffer[256]; tempBuffer = allocatedTempBuffer; I thought I did this right, but later on when I call memcpy(), my compiler gives me an error (STServer.m:238: warning: passing arg 1 of `memcpy'' makes pointer from integer without a cast). Here''s the line: memcpy([[connectedClients objectForKey:[NSNumber numberWithInt:newSocketID]] workBuffer][totalBytesReceived], tempBuffer, bytesReceived); Which is again the same as: memcpy(workBuffer[startingPoint], tempBuffer, bytesToCopy); Why is this an incorrect use of my pointer variable? This may be a basic question. I am kind of slow with pointers. Thank you for the help! -Yuki

Share this post


Link to post
Share on other sites
Advertisement
Dynamic memory allocation

In C++, the appropriate mechanism would be:
myPointer = new char[ specified_size ]; 

In C it would be:
myPointer = (char *)malloc( specified_size * sizeof( char ) ); 

Go and do likewise in Obj-C.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!