Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-04-2012, 07:47 AM   #16
Registered: May 2009
Posts: 295

Rep: Reputation: 6

struct areas *top = NULL;
top = malloc(sizeof (top));

someone told me to make sure it's
top = malloc(sizeof(struct areas));

sorry I posted here.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-04-2012, 07:58 AM   #17
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Originally Posted by errigour View Post
I'm still curious about what you meant in posting the above link.

There is one link to source code on that page and that source code uses the name "top" in a conceptually similar way to what is probably in the program you are debugging. But neither the bug you just described
top = malloc(sizeof (top));
nor the strange Valgrind symptom you described earlier
if ( top== NULL)
correspond to anything in that source code.

Now that you posted the source code of the bug, it was easy to identify your other thread, containing a better discussion of that bug:

Looking at just your post directly above this (before finding your other thread) I was tempted to make the same suggestion as appears at the bottom of the reply you already got here:

That can be a cleaner approach in complicated projects, because it avoids putting information, (the struct name) that might be changed in subsequent program revisions, in a place where the compiler could not see the bug if you made such a change in one place but not the other.

You should understand sizeof(*top) is not computed at run time and is not the size of the actual object top points to (which might be NULL). It is computed at compile time and is the size of the kind of object that top should point to.

sizeof(top) is the size of the pointer, not the size of what it does and/or should point to.

Last edited by johnsfine; 10-04-2012 at 08:20 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting "Short read in buffer_copy" error with "dpkg" stalefries Linux - Software 6 03-24-2010 03:19 PM
ns:"error when calling class OldSim"&tclsh:"invalid command+child process exits abn." shojaru Linux - Newbie 0 03-05-2009 04:23 AM
error: Error for wireless request "Set Mode" (8B06) : invalid argument "roaming" penguin chick Linux - Wireless Networking 5 08-22-2008 01:16 PM
"capset: Operation not permitted" error when I run valgrind. rsravi74 Linux - Newbie 1 09-02-2007 05:30 PM
valgrind crashes at start up with "Killed" error message lasindi Slackware 0 04-22-2006 08:54 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration