Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc and functions

Reply
Thread Tools

malloc and functions

 
 
raphfrk
Guest
Posts: n/a
 
      07-15-2007
I am having an issue with malloc and gcc. Is there something wrong
with my code or is this a compiler bug ?

I am running this program:

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

typedef struct pxl {
double lon, lat;
double x,y,z;
double px,py;
} pixel;


struct chn {
int index;
struct nde *node;
};

struct nde {
pixel position;
struct chn *forward;
struct chn *reverse;
int *chain_num;
int size;
};

typedef struct chn chain;
typedef struct nde node;

node *createnode( );

main()
{

node *startn;

startn = createnode( );

startn->size = 2;
startn->forward = malloc( 2 * sizeof( chain * ) );
startn->reverse = malloc( 2 * sizeof( chain * ) );

printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);

printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);

}

node *createnode( )
{
node *node;
node = malloc( sizeof(node) );
return node;
}



I get the following output.

sf 0x660168
sr 0x660178

sf 0xa383731
sr 0x660178

The startn->forward point changes between the two printfs despite
there being no code between them.

 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      07-15-2007
raphfrk <(E-Mail Removed)> writes:

>
> node *createnode( )
> {
> node *node;
> node = malloc( sizeof(node) );


Just a quick glance, the above "confused me".

node? Try "node * pnode" ...

No idea if it will fix your problem.

 
Reply With Quote
 
 
 
 
raphfrk
Guest
Posts: n/a
 
      07-15-2007
On Jul 15, 4:09 pm, Richard <(E-Mail Removed)> wrote:
> raphfrk <(E-Mail Removed)> writes:
>
> > node *createnode( )
> > {
> > node *node;
> > node = malloc( sizeof(node) );

>
> Just a quick glance, the above "confused me".
>
> node? Try "node * pnode" ...
>
> No idea if it will fix your problem.


Yeah, that fixed it. I have been trying to figure it out for ages
(well about 3 hours). gcc must be misinterpreting what I wanted. It
didn't give a warning though.

Thanks alot.

 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      07-15-2007
On Jul 15, 10:57 am, raphfrk <(E-Mail Removed)> wrote:
> I am having an issue with malloc and gcc. Is there something wrong
> with my code or is this a compiler bug ?
>
> I am running this program:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> typedef struct pxl {
> double lon, lat;
> double x,y,z;
> double px,py;
> } pixel;
>
> struct chn {
> int index;
> struct nde *node;
> };
>
> struct nde {
> pixel position;
> struct chn *forward;
> struct chn *reverse;
> int *chain_num;
> int size;
> };
>
> typedef struct chn chain;
> typedef struct nde node;
>
> node *createnode( );
>
> main()


'int main (void)' is better.

> {
>
> node *startn;
>
> startn = createnode( );
>
> startn->size = 2;
> startn->forward = malloc( 2 * sizeof( chain * ) );
> startn->reverse = malloc( 2 * sizeof( chain * ) );
>
> printf("sf %p\nsr %p\n\n",
> startn->forward,
> startn->reverse
> );
>
> printf("sf %p\nsr %p\n\n",
> startn->forward,
> startn->reverse
> );


The %p conversion specifier expects a void * argument, you need to
cast your arguments appropriately.

> }
>
> node *createnode( )
> {
> node *node;


Yuck. Using the same name for a typedef and a variable (esp. one that
is a pointer to that type) is ugly and has the potential to cause
great confusion, see below.

> node = malloc( sizeof(node) );


Hint: Does sizeof apply to the typedef name or the variable name?
It's probably not what you expect.
You should also check the return value of malloc for failure.

> return node;
> }
>
> I get the following output.
>
> sf 0x660168
> sr 0x660178
>
> sf 0xa383731
> sr 0x660178
>
> The startn->forward point changes between the two printfs despite
> there being no code between them.


Fix the issues mentioned above and let us know if you still have a
problem.

Robert Gamble

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-15-2007
raphfrk <(E-Mail Removed)> writes:

> On Jul 15, 4:09 pm, Richard <(E-Mail Removed)> wrote:
>> raphfrk <(E-Mail Removed)> writes:
>>
>> > node *createnode( )
>> > {
>> > node *node;
>> > node = malloc( sizeof(node) );

>>
>> Just a quick glance, the above "confused me".
>>
>> node? Try "node * pnode" ...
>>
>> No idea if it will fix your problem.

>
> Yeah, that fixed it. I have been trying to figure it out for ages
> (well about 3 hours). gcc must be misinterpreting what I wanted. It
> didn't give a warning though.


Rubbish in rubbish out :-; Personally, even if it ever was "ok" I would
never have a variable with the same name as a type. A variable should
reflect the object to which it refers. In this case a pointer to a node
- hence a "pnode".

>
> Thanks alot.
>


No probs. Glad to help. Good luck.

Ask in a gcc newsgroup here about the warnings - seems strange:

gnu.gcc.help
 
Reply With Quote
 
rzed
Guest
Posts: n/a
 
      07-15-2007
raphfrk <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> On Jul 15, 4:09 pm, Richard <(E-Mail Removed)> wrote:
>> raphfrk <(E-Mail Removed)> writes:
>>
>> > node *createnode( )
>> > {
>> > node *node;
>> > node = malloc( sizeof(node) );

>>
>> Just a quick glance, the above "confused me".
>>
>> node? Try "node * pnode" ...
>>
>> No idea if it will fix your problem.

>
> Yeah, that fixed it. I have been trying to figure it out for
> ages (well about 3 hours). gcc must be misinterpreting what I
> wanted. It didn't give a warning though.
>
> Thanks alot.
>


It may have fixed it, but it doesn't explain it. Your call to
createnode came before the first printf. So why does the confusion
matter to the printf?

By the way, I repeated the printf more times, and each time after
the first, it prints the same values as the second printf. That
is, it doesn't change after that first time. What could possibly
be happening under the hood to get that bizarre result?

--
rzed
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      07-15-2007
In article <(E-Mail Removed)>, Richard <(E-Mail Removed)> wrote:
....
>Rubbish in rubbish out :-; Personally, even if it ever was "ok" I would
>never have a variable with the same name as a type.


This is common with structs. You frequently see code like: struct stat stat;
Of course: int int
doesn't work as well.

>A variable should reflect the object to which it refers. In this case a
>pointer to a node - hence a "pnode".


 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      07-15-2007
On Jul 15, 11:48 am, (E-Mail Removed) (Kenny McCormack)
wrote:
> In article <(E-Mail Removed)>, Richard <(E-Mail Removed)> wrote:
>
> ...
>
> >Rubbish in rubbish out :-; Personally, even if it ever was "ok" I would
> >never have a variable with the same name as a type.

>
> This is common with structs. You frequently see code like: struct stat stat;


This is true but the issue is much worse with typedefs since they
share the same namespace as ordinary identifiers and thus have greater
potential for confusion.

> Of course: int int
> doesn't work as well.


Well that's because int is a keyword.

typedef int INT;
{
INT INT;
....
}

This "works" but like the example presented by the OP is horrible
form.

Robert Gamble

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-15-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) (Kenny McCormack) writes:

> In article <(E-Mail Removed)>, Richard <(E-Mail Removed)> wrote:
> ...
>>Rubbish in rubbish out :-; Personally, even if it ever was "ok" I would
>>never have a variable with the same name as a type.

>
> This is common with structs. You frequently see code like: struct stat stat;
> Of course: int int
> doesn't work as well.


I simply don't like it. More than likely it will confuse a debugger too
never mind someone maintaining your code.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-15-2007
raphfrk wrote:
> I am having an issue with malloc and gcc. Is there something wrong
> with my code or is this a compiler bug ?

[...]

> node *createnode( )
> {
> node *node;
> node = malloc( sizeof(node) );
> return node;
> }


Giving variables with the same identifier as one you use for a type is
just asking for trouble. Compare the above with

node *createnode( )
{
node *nodeptr;
nodeptr = malloc(sizeof(node));
return nodeptr;
}

or with

node *createnode( )
{
node *nodeptr;
nodeptr = malloc(sizeof *nodeptr);
return nodeptr;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments