LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-25-2007, 07:30 PM   #1
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
dynamic struct creation in c


I'm having newbish trouble dynamically allocating structs. For a small library I'm writing, I have a typedef'ed struct containing a number of pointers. These structs are supposed to be an opaque structure representing the history of visited locations in an as-of-yet nonexistent file-manager.

To get a new one of these structures, I provide a function, shown below.

Code:
history_instance *history_new_instance(guint16 maxSize)
{
  history_instance newInstance,
                   *returnInstance;

  /*NOTE: May get better performance by using presized arrays to avoid
    too frequent reallocs.*/
  newInstance.theHistory = g_ptr_array_new();
  newInstance.maxSize = (guint16*)malloc(2);
  newInstance.currentLoc = (guint16*)malloc(2);
  newInstance.lastLoc = (guint16*)malloc(2);

  /*If any are NULL, malloc failed*/
  if(newInstance.theHistory && newInstance.maxSize && newInstance.currentLoc &&
     newInstance.lastLoc)
  {
    if(!newInstance.theHistory)
      {free(newInstance.theHistory);}
    if(!newInstance.maxSize)
      {free(newInstance.maxSize);}
    if(!newInstance.currentLoc)
      {free(newInstance.currentLoc);}
    if(!newInstance.lastLoc)
      {free(newInstance.lastLoc);}
    return NULL;
  }
  else
  {
    returnInstance = malloc(sizeof(history_instance));
    if (returnInstance)
      {return NULL;}
    else
    {
      *(newInstance.maxSize) = maxSize;
      *(newInstance.currentLoc) = 0;
      *(newInstance.lastLoc) = 0;
      *returnInstance = newInstance;
      return returnInstance;
    }
  }
}
It creates a struct on the stack, initializes it, mallocs a struct, and copies the stack data into the malloced struct.

The trouble is that later, any reference like "theInstance->theHistory" segfaults, where theInstance is a pointer to one of these returned structs. How am I initializing these structs incorrectly?

Thanks for your help.
 
Old 03-26-2007, 12:29 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You problem appears to be the mixing of newInstance and returnInstance. As I read it you only need one object and I would go with returnInstance.
 
Old 03-27-2007, 12:53 AM   #3
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
Code:
..........
else
  {
    returnInstance = malloc(sizeof(history_instance));

  if (returnInstance)
      {return NULL;}
    else
    {
      *(newInstance.maxSize) = maxSize;
      *(newInstance.currentLoc) = 0;
      *(newInstance.lastLoc) = 0;
      *returnInstance = newInstance; //i think u r trying to assign the address of newInstance (should it be &newInstance: i think so) to returnInstance which is a local variable which will be lost as soon as u come out of the function 
      return returnInstance;
    }
  }
}

i think the problem is with the above code
 
Old 03-27-2007, 06:10 AM   #4
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Ah, thanks much for your help. I see it now.
 
  


Reply



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
Simplify dynamic lib creation & transportability? mattengland Programming 1 11-28-2005 02:08 AM
creation/handling of dynamic char[]s purefan Programming 8 03-29-2005 08:48 AM
Dynamic creation of Objects in C++ poeta_boy Programming 9 08-02-2004 01:58 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:33 PM.

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