Pointer Question
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]
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:
[edited by - nologic on April 21, 2004 10:32:50 PM]
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]
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement