Skip to main content
gamedev.net
🔒 Locked

Pointer Question

Started by NoLoGiC 4 replies 350+ views
Original Post
NoLoGiC
NoLoGiC
Hey everyone! Quick pointer question Say I read in data from a file. I create a pointer to a char array and then pass it to a function that will create a linked list node with it: char *tempLine = new char[25]; //Pretend now I've filled it with data functionA(tempLine); Using a MessageBox in functionA(for ex), I see the data just fine. functionA creates the linked list node, and I try to send tempLine to its constructor: void functionA(char *tempLine) { MessageBox(NULL, tempLine, "blahblah", MB_OK); // shows the data Node *newNode = new Node(tempLine); } Now in the node, the constructor looks like: Node(char *getData) Now using a MessageBox to show getData...I just get a bunch of garbage. Anyone know what I'm doing wrong? [edited by - nologic on April 21, 2004 9:56:59 PM]
Shannon Barber
Shannon Barber
Show the code the Node ctor.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
merlin9x9
merlin9x9
Are you, by any chance, coming from a Java background?
NoLoGiC
NoLoGiC
Yeah I'm directly from a java background Learned it in the public university, trying to expand and learn other languages. Pointers are still a little bit of a mystery...

Magmai Kai Holmlor: Which code would you like to see? The node constructor?

Here is a cut-down version:


cStickyNode::cStickyNode(int idNum, char *getData, RECT oldClientRect)
{
next = NULL;
previous = NULL;
id = idNum;
cwnd = NULL;

MessageBox(NULL, getData, "What arrived in the node", MB_OK);

strcpy(data, getData);
}


[edited by - nologic on April 21, 2004 10:32:50 PM]
merlin9x9
merlin9x9
Ah, then that explains why you''re doing silly things like "char *tempLine = new char[25]". Remember that in non-garbage-collected languages like C++, never allocate dynamically unless that''s actually what you need. So, tempLine should be allocated with "char tempLine[25];" because the size is known at compile time. If it weren''t, then dynamic allocation would be the way to go. And, unlike a garbage-collected language, you mustn''t forget to release the memory you''ve allocated—wipe your own ass.
NoLoGiC
NoLoGiC
Ok -- point taken

Yay! Removed the dynamically allocated stuff, and now it works.
Thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.