Click here to Skip to main content
15,899,754 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionMfc100d.dll Not Found Pin
AmbiguousName19-Jun-12 18:13
AmbiguousName19-Jun-12 18:13 
AnswerRe: Mfc100d.dll Not Found Pin
Richard MacCutchan19-Jun-12 20:59
mveRichard MacCutchan19-Jun-12 20:59 
AnswerRe: Mfc100d.dll Not Found Pin
Sivaraman Dhamodharan19-Jun-12 22:39
Sivaraman Dhamodharan19-Jun-12 22:39 
GeneralRe: Mfc100d.dll Not Found Pin
ThatsAlok20-Jun-12 20:20
ThatsAlok20-Jun-12 20:20 
GeneralRe: Mfc100d.dll Not Found Pin
Sivaraman Dhamodharan21-Jun-12 1:23
Sivaraman Dhamodharan21-Jun-12 1:23 
AnswerRe: Mfc100d.dll Not Found Pin
Albert Holguin20-Jun-12 3:23
professionalAlbert Holguin20-Jun-12 3:23 
AnswerRe: Mfc100d.dll Not Found Pin
Brandon-X1200021-Jun-12 21:41
Brandon-X1200021-Jun-12 21:41 
QuestionC structs dynamic vs local stack Pin
Bacchus Beale19-Jun-12 16:16
Bacchus Beale19-Jun-12 16:16 
I did C structs a few years ago at uni.
I am trying to avoid malloc for all structures in the tree- since I would need to be sure it has all been freed.
In the first two ways without malloc but both end up using local stack variables which ‘disappear’ and are not kept in memory.
But Only the malloc response has the correct answers after the function call.

C++
#include <stdlib.h>
#include <stdio.h>

struct thing1
{
	int n;
	char* word;
	
};

struct thing2
{
	int n;
	char* word;
	struct thing1 *node1;
};

struct response
{
	int n;
	struct thing2 *node2;
};

///declarations
//with malloc
int getDynamicResponse(struct response* res);
//without malloc
int getResponse(struct response* res);
//by ref
int getRefResponse(struct response* res);

//get structs
struct thing1 getThing1();
struct thing2 getThing2();
//by ref
void refThing1(struct thing1* out);
void refThing2(struct thing2* out);
//print
void showThing1(struct thing1 *t1);
void showThing2(struct thing2 *t2);
void showResponse(struct response *res);

///implementation
//get structs
struct thing1 getThing1()
{
	struct thing1 t1;
	t1.n = 1;
	t1.word = "thing1";
	return t1;
}

struct thing2 getThing2()
{
	struct thing2 t2;
	t2.n = 2;
	t2.word = "thing2";
	return t2;
}

//by ref
void refThing1(struct thing1* out)
{
	struct thing1 t1;
	t1.n = 3;
	t1.word = "refthing3";
	*out = t1;
}

void refThing2(struct thing2* out)
{
	struct thing2 t2;
	t2.n = 4;
	t2.word = "refthing4";
	*out = t2;
}

//print
void showThing1(struct thing1 *t1)
{
	printf("thing1\n");
	if(t1)
	{
		int i = t1->n;
		if(i) printf("n=%d\n", i);
		if(t1->word) printf("word=%s\n", t1->word);
	}
	else printf("Thing1 NULL");
}

void showThing2(struct thing2 *t2)
{
	printf("thing2\n");
	if(t2)
	{
		printf("n=%d\n", t2->n);
		if(t2->word) printf("word=%s\n", t2->word);
		if(t2->node1) showThing1(t2->node1);
	}
	else printf("Thing2 NULL");
}

void showResponse(struct response *res)
{
	if(res)
	{
		printf("response\n");
		printf("n=%d\n", res->n);
		if(res->node2) showThing2(res->node2);
	}
	else printf("Response NULL");
	
}

//with malloc
int getDynamicResponse(struct response* res)
{
	printf("\nwith malloc\n");
	printf("n=%d\n", res->n);
	struct response *tmp;
	tmp = (struct response*)malloc(sizeof(struct response));
	tmp->n = 12345;
	tmp->node2 = (struct thing2*)malloc(sizeof(struct thing2));
	tmp->node2->n = 2468;
	tmp->node2->word = "malloc1";
	tmp->node2->node1 = (struct thing1*)malloc(sizeof(struct thing1));
	tmp->node2->node1->n = 987;
	tmp->node2->node1->word = "malloc2";
	
	*res = *tmp;
	showResponse(res);
	printf("***************************\n");
	return 0;
}
//by ref
int getRefResponse(struct response* res)
{
	printf("\nby pointer ref\n");
	printf("n=%d\n", res->n);
	struct response out, *pout;
	res->n = 888;
	struct thing2 t2;
	struct thing1 t1;
	refThing2(&t2);	
	refThing1(&t1);	
	struct thing1 *p1 = &t1;
	struct thing2 *p2 = &t2;
	
	//link nodes
	pout = &out;
	pout->node2 = p2;
	pout->node2->node1 = p1;
	
	*res = out;
	showResponse(res);
	printf("***************************\n");
	return 0;
}

//without malloc
int getResponse(struct response* res)
{
	printf("\nlocal stack\n");
	printf("n=%d\n", res->n);
	struct response out, *pout;
	res->n = 99;
	struct thing2 tmp2 = getThing2();
	struct thing1 tmp1 = getThing1();
	struct thing1 *p1 = &tmp1;
	struct thing2 *p2 = &tmp2;
	//link nodes
	pout = &out;
	pout->node2 = p2;
	pout->node2->node1 = p1;
	
	*res = out;
	showResponse(res);
	printf("***************************\n");
	return 0;
}

int main(int argc, char **argv)
{
	printf("structs\n");
	struct response r1, r2, r3;
	r1.n = 51;
	r2.n = 52;
	r3.n = 53;
	getResponse(&r1);
	//error: local stack garbage	showResponse(&r1);
	printf("=====================\n");
	getRefResponse(&r2);
	showResponse(&r2);
	printf("=====================\n");
	getDynamicResponse(&r3);
	showResponse(&r3);
	printf("=====================\n");
	getchar();
	return 0;
}

AnswerRe: C structs dynamic vs local stack Pin
Chris Losinger19-Jun-12 16:47
professionalChris Losinger19-Jun-12 16:47 
AnswerRe: C structs dynamic vs local stack Pin
fat_boy20-Jun-12 1:32
fat_boy20-Jun-12 1:32 
AnswerRe: C structs dynamic vs local stack Pin
Albert Holguin20-Jun-12 4:13
professionalAlbert Holguin20-Jun-12 4:13 
AnswerRe: C structs dynamic vs local stack Pin
jschell20-Jun-12 8:38
jschell20-Jun-12 8:38 
QuestionVideo Player Win32->Animate Pin
MarcoXIII19-Jun-12 11:35
MarcoXIII19-Jun-12 11:35 
AnswerRe: Video Player Win32->Animate Pin
Vaclav_19-Jun-12 11:46
Vaclav_19-Jun-12 11:46 
QuestionProperty Sheets & Pages questions Pin
DeepT19-Jun-12 10:06
DeepT19-Jun-12 10:06 
AnswerRe: Property Sheets & Pages questions Pin
Chris Losinger19-Jun-12 12:39
professionalChris Losinger19-Jun-12 12:39 
GeneralRe: Property Sheets & Pages questions Pin
DeepT20-Jun-12 2:21
DeepT20-Jun-12 2:21 
QuestionRe: Property Sheets & Pages questions Pin
David Crow20-Jun-12 2:34
David Crow20-Jun-12 2:34 
AnswerRe: Property Sheets & Pages questions Pin
DeepT20-Jun-12 2:36
DeepT20-Jun-12 2:36 
QuestionUpdating CPropertyPage Pin
Vaclav_19-Jun-12 5:59
Vaclav_19-Jun-12 5:59 
QuestionRe: Updating CPropertyPage Pin
David Crow19-Jun-12 7:02
David Crow19-Jun-12 7:02 
AnswerRe: Updating CPropertyPage Pin
Vaclav_19-Jun-12 7:48
Vaclav_19-Jun-12 7:48 
GeneralRe: Updating CPropertyPage Pin
David Crow19-Jun-12 8:03
David Crow19-Jun-12 8:03 
GeneralSOLVED : Updating CPropertyPage Pin
Vaclav_19-Jun-12 11:40
Vaclav_19-Jun-12 11:40 
SuggestionRe: SOLVED : Updating CPropertyPage Pin
David Crow20-Jun-12 2:38
David Crow20-Jun-12 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.