Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Seeting malloc pointer to NULL [2] -Totally confused!!!!!

0 views
Skip to first unread message

Robby

unread,
Oct 11, 2008, 3:28:00 PM10/11/08
to
Hello,

Hope you guys are still there! ...... Need help.

It worked, then it didn't work, then it worked and now it doesn't work.

I don't know exactly how to integrate pointers to pointers when it comes to
using them with functions. I am getting weird results again! I will let you
guys take a look at the last code that I ahve been using! Please get back!

============================================
// #includes here!

enum enum_OC{e_CREATE=1, e_MODIFY, e_FREE};

typedef struct passCode {
int TOUCHES;
} PASSCODE;

void config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES );

void main()
{
PASSCODE *obj_PASSCODE; // Create a PASSCODE type pointer

config(e_CREATE, obj_PASSCODE, 4);

//... other code..... functions using obj_PASSCODE!

config(e_FREE, obj_PASSCODE, ...); //Free the Pointer !

}

config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES )
{
switch (OC)
{
//Now that I am passing obj_PASSCODE with multi indirection, how do
//I dereference it in this function? Is the code below right? (I don't no
anymore!)

case e_CREATE:
// **obj_PASSCODE = NULL ???
obj_PASSCODE = NULL;
obj_PASSCODE = malloc (sizeof (PASSCODE));

case e_MODIFY:
// **obj_PASSCODE->TOUCHES = TOUCHES; ???
obj_PASSCODE->TOUCHES = TOUCHES;
//....

break;

case e_FREE:
free(obj_PASSCODE);
obj_PASSCODE = NULL;
break;
}
}
=================================================

What's happening is that when I return to main and pass obj_PASSCODE to
another function, the data I set in config seems to be lost?


Passing a variable by value:

f1( int x);
main{
int x;
f1(x);
}
f1( int x);
{...}

Passing a variable by reference:
f1( int *i)
main{
int x;
f1(&x);
}

f1( int *i)
{...}

Am I not passing a pointer by reference using multi indirection? like this:
=========================================
void config( PASSCODE **obj_PASSCODE);

void main()
{
PASSCODE *obj_PASSCODE;
config( obj_PASSCODE);
}

config( PASSCODE **obj_PASSCODE)
{...}
===========================================

The changes that occur in the function should be remembered after I get out
of the function.

Help sincerely appreciated!

--
Best regards
Roberto

Giovanni Dicanio

unread,
Oct 11, 2008, 4:16:43 PM10/11/08
to

"Robby" <Ro...@discussions.microsoft.com> ha scritto nel messaggio
news:BC697F74-89E1-40C5...@microsoft.com...

> void config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES );
>
> void main()
> {
> PASSCODE *obj_PASSCODE; // Create a PASSCODE type pointer
>
> config(e_CREATE, obj_PASSCODE, 4);

Robby: the correct code should be:

config( e_CREATE, &obj_PASSCODE, 4 );

Note the & ("address of").

In fact, obj_PASSCODE is defined as a PASSCODE*, but the 'config' function
prototype has a double indirection pointer (**) as second parameter.


> config(e_FREE, obj_PASSCODE, ...); //Free the Pointer !

Again, you should use &obj_PASSCODE in the function call.


> config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES )
> {
> switch (OC)
> {
> //Now that I am passing obj_PASSCODE with multi indirection, how do
> //I dereference it in this function? Is the code below right? (I don't no
> anymore!)

You dereference it using *:
'*obj_PASSCODE' is of type 'PASSCODE*' (while 'obj_PASSCODE' is of type
'PASSCODE **').


> case e_CREATE:
> // **obj_PASSCODE = NULL ???

*obj_PASSCODE = NULL;
(But it is useless, because on malloc() failure, the return value of
malloc() will be NULL.)

> obj_PASSCODE = malloc (sizeof (PASSCODE));

*obj_PASSCODE = malloc( sizeof(PASSCODE) );


> case e_FREE:
> free(obj_PASSCODE);
> obj_PASSCODE = NULL;

free( *obj_PASSCODE );
*obj_PASSCODE = NULL;


> Passing a variable by reference:
> f1( int *i)
> main{
> int x;
> f1(&x);

> Am I not passing a pointer by reference using multi indirection? like

> this:
> =========================================
> void config( PASSCODE **obj_PASSCODE);
>
> void main()
> {
> PASSCODE *obj_PASSCODE;
> config( obj_PASSCODE);

You need the &:

config( &obj_PASSCODE );


Hope it helped,

Giovanni

Igor Tandetnik

unread,
Oct 11, 2008, 4:18:47 PM10/11/08
to
"Robby" <Ro...@discussions.microsoft.com> wrote in message
news:BC697F74-89E1-40C5...@microsoft.com

> enum enum_OC{e_CREATE=1, e_MODIFY, e_FREE};

I still recommend you define three separate functions for these three
operations.

> typedef struct passCode {
> int TOUCHES;
> } PASSCODE;
>
> void config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES );
>
> void main()
> {
> PASSCODE *obj_PASSCODE; // Create a PASSCODE type pointer
>
> config(e_CREATE, obj_PASSCODE, 4);

Make it

config(e_CREATE, &obj_PASSCODE, 4);

Note the ampersand.

> //... other code..... functions using obj_PASSCODE!
>
> config(e_FREE, obj_PASSCODE, ...); //Free the Pointer !

And similarly here.

> }
>
> config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES )
> {
> switch (OC)
> {
> //Now that I am passing obj_PASSCODE with multi indirection, how do
> //I dereference it in this function? Is the code below right? (I
> don't no anymore!)
>
> case e_CREATE:
> // **obj_PASSCODE = NULL ???
> obj_PASSCODE = NULL;
> obj_PASSCODE = malloc (sizeof (PASSCODE));

*obj_PASSCODE = malloc (sizeof (PASSCODE));

>
> case e_MODIFY:
> // **obj_PASSCODE->TOUCHES = TOUCHES; ???
> obj_PASSCODE->TOUCHES = TOUCHES;

(*obj_PASSCODE)->TOUCHES = TOUCHES;

> //....
>
> break;
>
> case e_FREE:
> free(obj_PASSCODE);
> obj_PASSCODE = NULL;

free(*obj_PASSCODE);
*obj_PASSCODE = NULL;

> break;
> }
> }
> =================================================
>
> What's happening is that when I return to main and pass obj_PASSCODE
> to another function, the data I set in config seems to be lost?

Your code shouldn't even compile. In main(), you pass PASSCODE* where
PASSCODE** is expected.

> Passing a variable by reference:
> f1( int *i)
> main{
> int x;
> f1(&x);

Note the ampersand.

> }
>
> Am I not passing a pointer by reference using multi indirection? like
> this: =========================================
> void config( PASSCODE **obj_PASSCODE);
>
> void main()
> {
> PASSCODE *obj_PASSCODE;
> config( obj_PASSCODE);

Note lack of ampersand. Compare and contrast.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Giovanni Dicanio

unread,
Oct 11, 2008, 4:29:55 PM10/11/08
to
One more note:

This is a notation suggestion, that may help you with pointers in C/C++ (I
found that useful myself).

When you have a pointer to something, you can prefix the variable name with
a 'p':

MYDATA MyData;

MYDATA * pMyData; /* pointer to MYDATA */

Now, there is a "cancelation rule": the * and the 'p' cancel each other,
e.g.:

*pMyData

you can see the * and 'p', so they cancel, and *pMyData is kind of MyData
(i.e. *pMyData is of type MYDATA, it is not a pointer anymore).

This comes in handy especially when you have double indirection pointers.

e.g.

void DoSomething( MYDATA ** ppMyData ); /* double pointer --> 2 'p's
prefix */

Now:

- ppMyData is a double indirection pointer to MYDATA (i.e. MYDATA **)
- *ppMyData -> * and the first 'p' cancel, so this is like pMyData, i.e. a
pointer (simple pointer) to MYDATA (i.e. MYDATA *)
- **ppMyData -> both * and 'p's cancel, so this is like MyData, i.e. type
MYDATA (no more pointer).

If you use this notation consistently, I think that this may help in your
code.

e.g.

> void config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES );

Change to:

void config(
...
PASSCODE ** ppPassCode,
... );

Your wrong code was:

> obj_PASSCODE = malloc (sizeof (PASSCODE));

Now, you see, malloc() is expected to return a pointer to PASSCODE
structure, so malloc() is expected to return a PASSCODE*, right?
You have ppPassCode as function parameter; how can you get a PASSCODE* from
ppPassCode?
Simple: cancel one 'p', using one '*':

*ppPassCode

is of type PASSCODE*, because * and the first 'p' cancel, and you have
pPassCode (i.e. PASSCODE *).

I hope I did not make much confusion with my English... But, trust me, this
rule is very simple, and after some practice you may find it helpful.

Giovanni


Robby

unread,
Oct 11, 2008, 4:52:04 PM10/11/08
to
Man! am I glad to hear from you guys!!!!!

I am in a state of panic here!

I can't look at your help right now, I am out the door (am me na go get me
another C book!) , but when I get back, later I will look at all of your
feedback and surely get back to you guys.

Thanks a million, and later!

--
Best regards
Roberto

Barry Schwarz

unread,
Oct 12, 2008, 2:57:19 AM10/12/08
to
On Sat, 11 Oct 2008 12:28:00 -0700, Robby
<Ro...@discussions.microsoft.com> wrote:

>Hello,
>
>Hope you guys are still there! ...... Need help.
>
>It worked, then it didn't work, then it worked and now it doesn't work.
>
>I don't know exactly how to integrate pointers to pointers when it comes to
>using them with functions. I am getting weird results again! I will let you
>guys take a look at the last code that I ahve been using! Please get back!

The only weird result you should get is a failure to compile. At that
point, any attempt at execution is a waste of time.

>
>============================================
>// #includes here!

The last set of includes I saw was half C and half C++. Maybe you
should show the ones you are using now.

>
>enum enum_OC{e_CREATE=1, e_MODIFY, e_FREE};
>
>typedef struct passCode {
>int TOUCHES;
>} PASSCODE;
>
>void config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES );
>
>void main()
>{
>PASSCODE *obj_PASSCODE; // Create a PASSCODE type pointer
>
>config(e_CREATE, obj_PASSCODE, 4);

The second argument is incompatible with the function prototype. This
was a bad idea that has been implemented incorrectly.

>
>//... other code..... functions using obj_PASSCODE!
>
>config(e_FREE, obj_PASSCODE, ...); //Free the Pointer !
>
>}
>
>config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES )
>{
>switch (OC)
>{
>//Now that I am passing obj_PASSCODE with multi indirection, how do

No you are not. That is what this function wants to receive but it is
not what you sent.

>//I dereference it in this function? Is the code below right? (I don't no
>anymore!)
>
>case e_CREATE:
>// **obj_PASSCODE = NULL ???

Think about it. Within this function, obj_PASSCODE is a struct**.
Therefore, *obj_PASSCODE is a struct* and **obj_PASSCODE is a struct.
Do you think it makes sense to say set struct to NULL?

>obj_PASSCODE = NULL;
>obj_PASSCODE = malloc (sizeof (PASSCODE));

Again, obj_PASSCODE is a struct**. It must point to a struct*. You
do not allocate space for a struct*. You allocate space for a struct.

>
>case e_MODIFY:
>// **obj_PASSCODE->TOUCHES = TOUCHES; ???

What is the relative precedence of * and ->? You statement will be
parsed as
**(obj_PASSCODE->TOUCHES) = TOUCHES;

How many was can this be wrong. The only type that can appear on the
left of the -> operator is struct*. obj_PASSCODE is not an expression
of this type. Even if it were, ->TOUCHES is an int, not a pointer.
You cannot apply any dereference operators to non-pointer types.

>obj_PASSCODE->TOUCHES = TOUCHES;

Again, obj_PASSCODE is not a struct* and cannot appear on the left of
the -> operator. I told you this was a bad idea.

>//....
>
>break;
>
>case e_FREE:
>free(obj_PASSCODE);
>obj_PASSCODE = NULL;
>break;
>}
>}
>=================================================
>
>What's happening is that when I return to main and pass obj_PASSCODE to
>another function, the data I set in config seems to be lost?

At no point in this code does your function ever set the value of the
object obj_PASSCODE in main. If you would stop using the same name
for different objects, some of your confusion might be eliminated.

>
>
>Passing a variable by value:
>
>f1( int x);
>main{
>int x;
>f1(x);
>}
>f1( int x);
>{...}
>
>Passing a variable by reference:

This is not a pass by reference.

>f1( int *i)
>main{
>int x;
>f1(&x);

You new to use the & operator here. Why didn't you use it in "your
real code".

>}
>
>f1( int *i)
>{...}
>
>Am I not passing a pointer by reference using multi indirection? like this:

No you are not passing by reference in C, ever.

>=========================================
>void config( PASSCODE **obj_PASSCODE);
>
>void main()
>{
>PASSCODE *obj_PASSCODE;
>config( obj_PASSCODE);

This is the same constraint violation mentioned above. The type of
your argument is incompatible with the type expected by the function.

>}
>
>config( PASSCODE **obj_PASSCODE)
>{...}
>===========================================
>
>The changes that occur in the function should be remembered after I get out
>of the function.

Changes made to objects local to a function are NEVER remembered when
you leave the function. If you code the function properly and call it
properly, config could make changes to objects defined in main.

1 - For any object in main you want to change in config, pass the
address of that object as a pointer.

2 - In order for config to make the change to that object, it must
dereference the pointer it received.

>
>Help sincerely appreciated!

Go back to the original approach and have config return the new value.

--
Remove del for email

Robby

unread,
Oct 12, 2008, 2:07:01 PM10/12/08
to
Hello,

Unfortunately at the following line:

*obj_PASSCODE = malloc (sizeof (PASSCODE));

I get an "Invalid type conversion" error!!!!!

I have done all the changes listed. Could it be a compiler issue!

Get back!

--
Best regards
Robert

Robby

unread,
Oct 12, 2008, 2:20:01 PM10/12/08
to
Hello fellows,

Okay, I have entered the code in VC++ and an error points to the same line
of code:

*obj_PASSCODE = malloc (sizeof (PASSCODE));

The error is:

c:\_DTS_PROGRAMMING\C_PROGRAMMING\c\TEST_4\Test_1.cpp(70): error C2440:
'type cast' : cannot convert from 'void *' to 'PASSCODE'

Beats me!

As much as Igor says that I should do it in 3 different functions for
simplicity, he is right. But I am also trying to learn pointers to pointers
with functions and structs and besides the point that this is new to me, I am
also currious as to why it doesn't work.

Please get back!

--
Best regards
Robert

Igor Tandetnik

unread,
Oct 12, 2008, 2:45:19 PM10/12/08
to
"Robby" <Ro...@discussions.microsoft.com> wrote in message
news:213AA21B-76AF-4503...@microsoft.com

> Okay, I have entered the code in VC++ and an error points to the same
> line of code:
>
> *obj_PASSCODE = malloc (sizeof (PASSCODE));
>
> The error is:
>
> c:\_DTS_PROGRAMMING\C_PROGRAMMING\c\TEST_4\Test_1.cpp(70): error
> C2440: 'type cast' : cannot convert from 'void *' to 'PASSCODE'

malloc() returns void*. Unlike C, there's no implicit conversion in C++
from void* to other pointer types. If you want this code to be valid C++
as well as C, make it

*obj_PASSCODE = (PASSCODE*)malloc(sizeof (PASSCODE));

Scott McPhillips [MVP]

unread,
Oct 12, 2008, 2:56:58 PM10/12/08
to
"Robby" <Ro...@discussions.microsoft.com> wrote in message
news:213AA21B-76AF-4503...@microsoft.com...

> Hello fellows,
>
> Okay, I have entered the code in VC++ and an error points to the same line
> of code:
>
> *obj_PASSCODE = malloc (sizeof (PASSCODE));
>
> The error is:
>
> c:\_DTS_PROGRAMMING\C_PROGRAMMING\c\TEST_4\Test_1.cpp(70): error C2440:
> 'type cast' : cannot convert from 'void *' to 'PASSCODE'
>
> Beats me!

Robby, You have not shown us the type declaration of obj_PASSCODE in this
context. It is obvious from the error message that it is not a pointer or
pointer to pointer here.

--
Scott McPhillips [VC++ MVP]

Robby

unread,
Oct 12, 2008, 3:36:05 PM10/12/08
to
Hello Igor and Scott! Thanks for your much appreciated replies!

Yes ! I tried the same thing:

*obj_PASSCODE = (PASSCODE*)malloc(sizeof (PASSCODE));

and made all the assigments like so:

(*obj_PASSCODE)->TOUCHES = TOUCHES;

and it works in VC++, but it doesn't work in my MCU compiler, I think it
could be a limitation... I'm not sure, I could be wrong, but in VC++ it works
perfectly, I am able to retrieve the structure's data even in main after its
been set in config(). And the obj_PASSCODE value is reset to 0 after the call
to:

config(e_FREE, &obj_PASSCODE);

Igor, Ilike the idea of doing it with three seperate functions, however if I
have 30 of these configs in my project, this means I will have 90 functions
as opposed to 20. This is my only pet peave. I greatly respect your input, so
what do you think?

So now that I have gotten it to work in the VC++ compiler, I guess the
problem is on my side as to weather its a specific limitaion in my MCU
compiler... mayby my compiler doesn't support pointers to pointers!

Thanks guys, again this post is getting long, so I will let you know what
happens in another post... I sure hope I will eventually get to the bottom of
this!


--
Best regards
Roberto

Robby

unread,
Oct 12, 2008, 3:40:00 PM10/12/08
to
Igor:

Sorry, my quote should of been:

Igor, Ilike the idea of doing it with three seperate functions, however if I
have 30 of these configs in my project, this means I will have 90 functions

as opposed to 30. This is my only pet peave. I greatly respect your input, so
what do you think?

--
Best regards
Robert

David Wilkinson

unread,
Oct 12, 2008, 3:58:06 PM10/12/08
to
Robby wrote:
> Hello Igor and Scott! Thanks for your much appreciated replies!
>
> Yes ! I tried the same thing:
>
> *obj_PASSCODE = (PASSCODE*)malloc(sizeof (PASSCODE));
>
> and made all the assigments like so:
>
> (*obj_PASSCODE)->TOUCHES = TOUCHES;
>
> and it works in VC++, but it doesn't work in my MCU compiler, I think it
> could be a limitation... I'm not sure, I could be wrong, but in VC++ it works
> perfectly, I am able to retrieve the structure's data even in main after its
> been set in config(). And the obj_PASSCODE value is reset to 0 after the call
> to:
>
> config(e_FREE, &obj_PASSCODE);
>
> Igor, Ilike the idea of doing it with three seperate functions, however if I
> have 30 of these configs in my project, this means I will have 90 functions
> as opposed to 20. This is my only pet peave. I greatly respect your input, so
> what do you think?
>
> So now that I have gotten it to work in the VC++ compiler, I guess the
> problem is on my side as to weather its a specific limitaion in my MCU
> compiler... mayby my compiler doesn't support pointers to pointers!
>
> Thanks guys, again this post is getting long, so I will let you know what
> happens in another post... I sure hope I will eventually get to the bottom of
> this!

Robby:

If you give your file a .c extension instead of .cpp, then VC will compile your
code as C and you will make a better comparison with your other compiler.

--
David Wilkinson
Visual C++ MVP

Giovanni Dicanio

unread,
Oct 12, 2008, 4:24:01 PM10/12/08
to
Hi Robby,

"Robby" <Ro...@discussions.microsoft.com> ha scritto nel messaggio

news:1A3B7598-D915-4C11...@microsoft.com...

> Hello,
>
> Unfortunately at the following line:
>
> *obj_PASSCODE = malloc (sizeof (PASSCODE));
>
> I get an "Invalid type conversion" error!!!!!

I thought you needed C code (not C++), so I omitted the PASSCODE* cast from
malloc().

(Frankly speaking, if you should use C++, then I would suggest you to use
new instead of malloc... and in several contexts I would use smart pointers
like shared_ptr, etc. instead of raw pointers.)

As David already wrote, you can compile pure C code from VC++, using a .c
extension for source files.

Giovanni

Robby

unread,
Oct 12, 2008, 4:26:00 PM10/12/08
to
Hello David,

I did rename the .cpp file to a .c file, I put a break point at a specific
line and when I ran it a question mark flashed in the breakpoint and did
nothing.

How do I create a .c file in VC++, it only seems to want to create .cpp
files???

I tried, File, New, project, then what, I have .NET, ATL, MFC, WIN32 and
general.
Within these, I have all other types of files I can make, but don't see one
for .c?

Get back!

--
Best regards
Roberto

Robby

unread,
Oct 12, 2008, 4:29:00 PM10/12/08
to
Could this be my problem,

mallocs's prototype is:

*int8 malloc(int8 size)

?????

--
Best regards
Robert

Barry Schwarz

unread,
Oct 12, 2008, 5:12:40 PM10/12/08
to
On Sun, 12 Oct 2008 14:45:19 -0400, "Igor Tandetnik"
<itand...@mvps.org> wrote:

>"Robby" <Ro...@discussions.microsoft.com> wrote in message
>news:213AA21B-76AF-4503...@microsoft.com
>> Okay, I have entered the code in VC++ and an error points to the same
>> line of code:
>>
>> *obj_PASSCODE = malloc (sizeof (PASSCODE));
>>
>> The error is:
>>
>> c:\_DTS_PROGRAMMING\C_PROGRAMMING\c\TEST_4\Test_1.cpp(70): error
>> C2440: 'type cast' : cannot convert from 'void *' to 'PASSCODE'
>
>malloc() returns void*. Unlike C, there's no implicit conversion in C++
>from void* to other pointer types. If you want this code to be valid C++
>as well as C, make it
>
>*obj_PASSCODE = (PASSCODE*)malloc(sizeof (PASSCODE));

If that were the problem, the last word in the diagnostic would have
been PASSCODE*. Apparently in the function with this code,
obj_PASSCODE is a struct*. Dereferencing it produces a struct type
and there is no way to assign a pointer to a struct type.

Barry Schwarz

unread,
Oct 12, 2008, 5:12:40 PM10/12/08
to
On Sun, 12 Oct 2008 11:07:01 -0700, Robby
<Ro...@discussions.microsoft.com> wrote:

>Hello,
>
>Unfortunately at the following line:
>
>*obj_PASSCODE = malloc (sizeof (PASSCODE));

Which function contains this line? What is the type of obj_PASSCODE
in that function? Are you compiling C or C++?

>
>I get an "Invalid type conversion" error!!!!!
>
>I have done all the changes listed. Could it be a compiler issue!
>
>Get back!
>

--
Remove del for email

Barry Schwarz

unread,
Oct 12, 2008, 5:12:40 PM10/12/08
to
On Sun, 12 Oct 2008 13:29:00 -0700, Robby
<Ro...@discussions.microsoft.com> wrote:

>Could this be my problem,
>
>mallocs's prototype is:
>
>*int8 malloc(int8 size)

Would you care to identify where you saw such garbage? At the very
least, it says you can never allocate more than 255 bytes. It is also
inconsistent with the error messages in you preceding posts which
clearly show the compiler recognizing that malloc returns a void*.

Barry Schwarz

unread,
Oct 12, 2008, 5:12:40 PM10/12/08
to
On Sun, 12 Oct 2008 13:26:00 -0700, Robby
<Ro...@discussions.microsoft.com> wrote:

>Hello David,
>
>I did rename the .cpp file to a .c file, I put a break point at a specific
>line and when I ran it a question mark flashed in the breakpoint and did
>nothing.
>
>How do I create a .c file in VC++, it only seems to want to create .cpp
>files???
>
>I tried, File, New, project, then what, I have .NET, ATL, MFC, WIN32 and
>general.
>Within these, I have all other types of files I can make, but don't see one
>for .c?

First start with a new project.

Then File->New->C++ source file. Specify a file name with a .c
suffix. Cut and paste the source from your existing source to this
new .c file (e.g., Ctrl-A followed by Ctrl-V).

Repeat the previous paragraph for any other source files.

You should be able to figure out the equivalent steps for any header
files.

Robby

unread,
Oct 12, 2008, 6:47:00 PM10/12/08
to
Hello Barry,

I am using VC++ version 7.1, its quite an old version! And I have always had
problems creating .c files with it and that's why I always used .cpp files.

I did create a project called xxx. Now in the solution explorer under
"source files" I have: xxx.cpp, assemblyInfo.cpp, stdafx.cpp... and I created
a new file called ccc.c and pasted my code in there. Compiled, got error,

c:\_dts_programming\C_programming\c\My_new_tests\xxx\ccc.c(92): fatal error
C1010: unexpected end of file while looking for precompiled header directive

Obviously we now have 2 mains. This one in xxx.cpp:

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
int _tmain()
{
// TODO: Please replace the sample code below with your own.
Console::WriteLine(S"Hello World");
return 0;
}

and mine in ccc.c. What am I supposed to do, start creating other headers
with my code and copy some of my code into the xxx.cpp..... this is too
confusing, This is why I never did .c files in this compiler. Sorry I don't
know where to go from here unless I have very specific instructions
pertaining to this version!

>*int8 malloc(int8 size)
>Would you care to identify where you saw such garbage?

I see this when I put my cursor over the word "malloc" in my code!

> At the very least, it says you can never allocate more than 255 bytes.

Yup! I only have 4K ram on the MCU! Therefore, the limit is 255 bytes!
Search me why they did this!

It would be a great help if one of you could copy the following code into
VC++ in a .c file and see what you get. Take it as you see it below and copy
it and compile. If I compile this in a .cpp file(VC++) I get no errors, but
if you want to see if it would compile differently in a .c file, I don't know
how to do it!

With the code below, the MCU compiler (my compiler) gives me an " Invalid
type conversion" error at the malloc line *and*a "Previous identifier must be
a pointer" error for every line of code in the e_MODIFY section below.
?????????

===============================================
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;

enum enum_OBJTASK {e_CREATE=1, e_MODIFY, e_FREE};

typedef struct passCode {
int TOUCHES;

int LEAST_X;
int FAR_X;
int LEAST_Y;
int FAR_Y;
int DISP_INTRVL;
int MSG_NUM;
} PASSCODE;

void ULC_PASSC_config_passcode (enum_OBJTASK OBJ_CNTRL,
PASSCODE **obj_PASSCODE,
int TOUCHES,
int ID_LEAST_X,
int ID_FAR_X,
int ID_LEAST_Y,
int ID_FAR_Y,
int DISP_INTERVAL,
int MSG_NUM)
{
switch (OBJ_CNTRL)
{
case e_CREATE:
*obj_PASSCODE = (PASSCODE*) malloc (sizeof (PASSCODE));

case e_MODIFY:
(*obj_PASSCODE)->TOUCHES = TOUCHES;
(*obj_PASSCODE)->LEAST_X = ID_LEAST_X;
(*obj_PASSCODE)->FAR_X = ID_FAR_X;
(*obj_PASSCODE)->LEAST_Y = ID_LEAST_Y;
(*obj_PASSCODE)->FAR_Y = ID_FAR_Y;
(*obj_PASSCODE)->DISP_INTRVL = DISP_INTERVAL;
(*obj_PASSCODE)->MSG_NUM = MSG_NUM;
break;

case e_FREE:


free(*obj_PASSCODE);
*obj_PASSCODE = NULL;
break;
}
}

void main()
{
PASSCODE *obj_PASSCODE; // CREATE A passCode POINTER

ULC_PASSC_config_passcode(e_CREATE, &obj_PASSCODE, 4,0,0,0,0,0,0);
ULC_PASSC_config_passcode(e_FREE, &obj_PASSCODE, 0,0,0,0,0,0,0);
}
========================================

Sorry for keeping the long names, but I am exhausted, its been almost a week
I am dwelling on this.

Now, I don't know what else to try. :(

This must be a specific compiler issue, I will call them tommorow unless
anyone has a resolution as why this could be happening.

In any event, I still would like to thank everyone for your help, its very
nice of you all!

--
Best regards
Roberto

Barry Schwarz

unread,
Oct 12, 2008, 11:28:20 PM10/12/08
to
On Sun, 12 Oct 2008 15:47:00 -0700, Robby
<Ro...@discussions.microsoft.com> wrote:

>Hello Barry,
>
>I am using VC++ version 7.1, its quite an old version! And I have always had
>problems creating .c files with it and that's why I always used .cpp files.
>
>I did create a project called xxx. Now in the solution explorer under
>"source files" I have: xxx.cpp, assemblyInfo.cpp, stdafx.cpp... and I created
>a new file called ccc.c and pasted my code in there. Compiled, got error,
>
>c:\_dts_programming\C_programming\c\My_new_tests\xxx\ccc.c(92): fatal error
>C1010: unexpected end of file while looking for precompiled header directive
>
>Obviously we now have 2 mains. This one in xxx.cpp:
>
>#include "stdafx.h"
>#using <mscorlib.dll>
>using namespace System;
>int _tmain()
>{
> // TODO: Please replace the sample code below with your own.
> Console::WriteLine(S"Hello World");
> return 0;
>}

I'm sorry but I give up. You have mixed up the languages, the
compilers, and the source code for several modules, including the one
above which is brand new but completely irrelevant to this discussion.

David Wilkinson

unread,
Oct 13, 2008, 5:31:03 AM10/13/08
to
Robby wrote:
> Hello Barry,
>
> I am using VC++ version 7.1, its quite an old version! And I have always had
> problems creating .c files with it and that's why I always used .cpp files.
>
> I did create a project called xxx. Now in the solution explorer under
> "source files" I have: xxx.cpp, assemblyInfo.cpp, stdafx.cpp... and I created
> a new file called ccc.c and pasted my code in there. Compiled, got error,

Robby:

You have not created the correct kind of project.

To bring your code into Visual Studio, cerate a new empty Win32 console project
and then just add your existing .c and .h files to it.

Ask here if you cannot figure out how to create an empty project, or add files.

Ulrich Eckhardt

unread,
Oct 13, 2008, 6:21:16 AM10/13/08
to
Robby wrote:
> I am using VC++ version 7.1, its quite an old version! And I have always
> had problems creating .c files with it and that's why I always used .cpp
> files.

Well, whatever problems you might have using .c files, it would be a good
idea to fix those instead of eliding them and thus stepping into further
problems. The point is, you have implicitly told the compiler (and, by
convention any reader of your code) that you want to write code in C++. The
problem with that is that you are actually coding in C (or so it seems, I'm
afraid you yourself aren't too clear on that subject), but C and C++ are
very distinct languages, even though they look very similar!

> I did create a project called xxx. Now in the solution explorer under
> "source files" I have: xxx.cpp, assemblyInfo.cpp, stdafx.cpp... and I
> created a new file called ccc.c and pasted my code in there. Compiled, got
> error,
>
> c:\_dts_programming\C_programming\c\My_new_tests\xxx\ccc.c(92): fatal
> error C1010: unexpected end of file while looking for precompiled header
> directive

Go to the project settings, deactivate support for precompiled headers.
Those won't be available on your MCU compiler anyway, they are not portable
and rather compiler-specific. Further, remove all C++ files from the
project. If you need their content, rename them to .c afterwards and then
add those again. This should also fix the issue that you are having two
main() functions.

> #using <mscorlib.dll>
> using namespace System;

FYI, this construct is part of yet another language, I think it's C++/CLI.
This is definitely not available in standard C++ or C. If you didn't write
that, you selected the wrong project template when creating the VC project.
Chose a C/C++ commandline project without any files in it.

> It would be a great help if one of you could copy the following code into
> VC++ in a .c file and see what you get.

[...]
> #include <stdio.h>
> #include <iostream>
> using namespace std;

<stdio.h> is a C header for various IO things. This header is available for
C++, too. <iostream> is a pure C++, as is using directive. Usually, you
don't want to use both of those in the same code. Again, I think you need
to work out the distinction between C and C++. You mention that your target
MCU has only 4k RAM, that means that you surely will not have a C++ stdlib
there, so I'd suggest you stay with C. Further, not everything that C
contains will also be available there, be ready for missing functionality.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Robby

unread,
Oct 13, 2008, 12:26:08 PM10/13/08
to
Hello David and Ulrich,

>To bring your code into Visual Studio, cerate a new empty Win32 console project
>and then just add your existing .c and .h files to it.

I now have a solution called xxx with a .c file called ccc.c !
Thankyou David!

>The problem with that is that you are actually coding in C (or so it seems, I'm
>afraid you yourself aren't too clear on that subject), but C and C++ are
>very distinct languages, even though they look very similar!

Yes I do want to test a peice of code in C and not in C++.

>Go to the project settings, deactivate support for precompiled headers.

I now set the "Create use precompiled headers" option to:

"Not using precompiled headers"

>Further, remove all C++ files from the project. If you need their content, rename >them to .c afterwards and then add those again. This should also fix the issue that >you are having two main() functions.

I removed all .cpp files and headers that were created by default and
created a ccc.c file and pasted my C code in there.

>FYI, this construct is part of yet another language, I think it's C++/CLI.
>This is definitely not available in standard C++ or C. If you didn't write
>that, you selected the wrong project template when creating the VC project.
>Chose a C/C++ commandline project without any files in it.

I didn't write those header files. As David mentioned, I created a new
project, selected Win 32 console project, and selected "console application"
in the wizard. So now, I removed all the .cpp files and created a .c file and
I have my code in there. There are so many routes you can go by that I was
trying everything and never got to a simple .c file!!!!! So now I got it!

I have added a stdio.h include line at the top of my code. Most books
instruct to put that there. I don't know if I should remove it or if its okay!

However, what used to compile in C++, now doesn't, here is the code:
==========================================
#include <stdio.h>

void main()
{

PASSCODE *obj_PASSCODE;

ULC_PASSC_config_passcode(e_CREATE, &obj_PASSCODE, 4,0,0,0,0,0,0);
ULC_PASSC_config_passcode(e_FREE, &obj_PASSCODE, 0,0,0,0,0,0,0);
}

=================================================

Here are some of the errors:

c:\_DTS_PROGRAMMING\C_PROGRAMMING\c\MY_NEW_TESTS\xxx\ccc.c(15): error C2146:
syntax error : missing ')' before identifier 'OBJ_CNTRL'

c:\_DTS_PROGRAMMING\C_PROGRAMMING\c\MY_NEW_TESTS\xxx\ccc.c(15): error C2144:
syntax error : '<Unknown>' should be preceded by '<Unknown>'

I really never used C in VC++, and its along time I didn't use VC++and I am
not familliar with all of these other languages, please excuse my ignorance!

What should I do to get it to compile! Please get back, I appolagize for my
lack of knowledge, I am simply trying my best!

Thanks for your posts!

--
Best regards
Roberto

Giovanni Dicanio

unread,
Oct 13, 2008, 1:30:38 PM10/13/08
to

"Robby" <Ro...@discussions.microsoft.com> ha scritto nel messaggio
news:6F74F9AC-D632-4201...@microsoft.com...

> However, what used to compile in C++, now doesn't, here is the code:
> ==========================================
> #include <stdio.h>
>
> enum enum_OBJTASK {e_CREATE=1, e_MODIFY, e_FREE};

> void ULC_PASSC_config_passcode (enum_OBJTASK OBJ_CNTRL,

I think that there is a problem with C enum syntax.

You can do:

typedef enum enum_OBJTASK
{
e_CREATE=1,
e_MODIFY,
e_FREE
} ObjTask;

and

void ULC_PASSC_config_passcode ( ObjTask OBJ_CNTRL,
...

Giovanni


Robby

unread,
Oct 14, 2008, 12:41:01 PM10/14/08
to
Hello Giovanni, thanks for your reply,

Yes, this did compile, so therefore there is something wrong with my MCU
compiler!

Thankyou all for your help, much appreciated!

--
Best regards
Robert

0 new messages