Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > struct/malloc failure

Reply
Thread Tools

struct/malloc failure

 
 
Sandy Beech
Guest
Posts: n/a
 
      10-16-2010
Hey

I'm trying to dynamically allocate space for a struct, and I get this
error:

"appCheck.c", line 159: warning(1515): a value of type "struct Minipath
*"
cannot be assigned to an entity of type "struct MiniPath *"

which makes no sense to me. I have to use cc to compile because it is
the only thing available on some machines where this needs to run. On my
machine I get the above warning, but if I run it, it works correctly.
Problem is, I need it to run on a different machine where that is an
error, not a warning. What am I missing? Any help?
It's declared:

struct MiniPath
{
char third [15];
char* fourth [15];
int places;

};

Then in another struct:

struct Path
{
char second [15];
struct MiniPath* path [15];
int splits;

} pathList[200];

And later:

pathList[currPath].path[pathList[currPath].splits] =
(struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
+sizeof(int));
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      10-16-2010
On 2010-10-16, Sandy Beech <(E-Mail Removed)> wrote:
> Hey
>
> I'm trying to dynamically allocate space for a struct, and I get this
> error:
>
> "appCheck.c", line 159: warning(1515): a value of type "struct Minipath
> *"
> cannot be assigned to an entity of type "struct MiniPath *"
> which makes no sense to me.


C is case sensitive.

> pathList[currPath].path[pathList[currPath].splits] =
> (struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
> +sizeof(int));


This looks very, very, confused. First, the cast is probably useless. So
let's break this down. We have apparently something which has a .path[]
member, so let's assume it's a struct Path. x.path[SOMETHING] is an
object of type "struct MiniPath *". You are trying to assign to it
the result of a malloc operation, but instead of "sizeof(struct MiniPath)",
you're requesting the size of an array of 15 characters plus the size of an
array of fifteen pointers plus the size of an int. Why?

Be aware that on many systems:

struct foo { char x[15]; struct foo *bar[15]; int i; }
will NOT have that size -- it can have padding.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
 
J. J. Farrell
Guest
Posts: n/a
 
      10-17-2010
Sandy Beech wrote:
> Hey
>
> I'm trying to dynamically allocate space for a struct, and I get this
> error:
>
> "appCheck.c", line 159: warning(1515): a value of type "struct Minipath
> *"
> cannot be assigned to an entity of type "struct MiniPath *"
>
> which makes no sense to me. I have to use cc to compile because it is
> the only thing available on some machines where this needs to run. On my
> machine I get the above warning, but if I run it, it works correctly.
> Problem is, I need it to run on a different machine where that is an
> error, not a warning. What am I missing? Any help?
> It's declared:
>
> struct MiniPath
> {
> char third [15];
> char* fourth [15];
> int places;
>
> };
>
> Then in another struct:
>
> struct Path
> {
> char second [15];
> struct MiniPath* path [15];
> int splits;
>
> } pathList[200];
>
> And later:
>
> pathList[currPath].path[pathList[currPath].splits] =
> (struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
> +sizeof(int));


The cast, which you don't need, has got a typo in it. Either correct the
typo or, much better, delete the cast.

As an aside, the size you're allocating will often happen to be the same
as the size of a struct MiniPath (though it's not guaranteed to be), but
it would be much cleaner to just malloc(sizeof struct MiniPath)
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      10-17-2010
On 16 Oct, 21:40, Sandy Beech <(E-Mail Removed)> wrote:
> Hey
>
> I'm trying to dynamically allocate space for a struct, and I get this
> error:
>
> "appCheck.c", line 159: warning(1515): a value of type "struct Minipath
> *"
> * * * * * cannot be assigned to an entity of type "struct MiniPath *"


you specify two different types. Check the spelling.

> which makes no sense to me. I have to use cc to compile because it is
> the only thing available on some machines where this needs to run. On my
> machine I get the above warning, but if I run it, it works correctly.
> Problem is, I need it to run on a different machine where that is an
> error, not a warning. What am I missing? Any help?
> It's declared:
>
> struct MiniPath
> {
> * char third [15];
> * char* fourth [15];
> * int places;
>
> };
>
> Then in another struct:
>
> struct Path
> {
> * char second [15];
> * struct MiniPath* path [15];
> * int splits;
>
> } pathList[200];
>
> And later:
>
> pathList[currPath].path[pathList[currPath].splits] =
> (struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
> +sizeof(int));


as someone else pointed out, this is a mess. Remove the cast and use
the sizeof value as the malloc parameter and things look much simpler

pathList[currPath].path[pathList[currPath].splits] =
malloc(sizeof(struct MiniPath*));

some prefer a variable instead of a type in the sizeof, though in this
case I'm not convinced it's any clearer.

pathList[currPath].path[pathList[currPath].splits] =
malloc(sizeof
*pathList[currPath].path[pathList[currPath].splits]);










 
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
Success Or Failure: There Is No Such Thing As Failure bs866806@163.com C Programming 0 01-06-2008 11:41 AM
[JAVA] [EVALUATION] - The Java Failure (Sorry: The Java(tm) Failure) Ilias Lazaridis Java 0 02-01-2005 10:32 AM
windows socket failure - still having trouble =?Utf-8?B?Q01Dcm9uYW4=?= Wireless Networking 2 10-03-2004 01:10 AM
let's try that again - windows socket failure =?Utf-8?B?Q01Dcm9uYW4=?= Wireless Networking 2 10-02-2004 09:06 AM
Re: 802.1x dhcp failure dpablo Wireless Networking 0 09-01-2004 12:03 AM



Advertisments