UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » memory storage problem

#1 2007-10-28 02:28 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: memory storage problem

Hi

I've a "little" (and very, VERY wierd) problem.
What I wanna do:
- Putting all of my email adresses (contacts) in a structure.
- split it, put the piece before in a seperate field as the piece after the at (@)
- sort the structure on my domain (the piece after the @)
- create an XML with every contact in an xml tag with the domain, like this:

<domain d="bla.com"><contact>person</contact><contact>person2</contact></domain><domain d="hotmail.com"><contact>etc etc etc</contact></domain>

(that is not exactly the XML I wanna generate, but it looks like it.)

Well. First I need to fill a structure with data.
This is how I declare my structure:

struct contactsstruct {
        char domain[65];   // het deel na de @
        char persoon[100]; // het deel voor de @
    };

and:

struct contactsstruct *contacts;

Then, I wanna fill it with data:

for(member = ezxml_child(members, "Member"); member; member = member->next) {
        passport = ezxml_child(member, "PassportName");
        contacts = malloc(sizeof(struct contactsstruct));

        if(length < (strlen(passport->txt)-1)) {        
            length = (strlen(passport->txt)-1);
        }
        
        split("@", passport->txt, tmpContacts);
        
        strcpy(contacts[i].domain, tmpContacts[1]);
        strcpy(contacts[i].persoon, tmpContacts[0]);        
        
        printf("\r\n --> %s @ %s   --   %s @ %s\r\n", tmpContacts[0], tmpContacts[1], contacts[i].persoon, contacts[i].domain);
        i++;
    }

then, I need to sort it, but when I call the qsort command, it blocks on my next for, so I commented out this line:

/* qsort(&contacts, i, length, strcmp); */

Then, to look if it is really in my structure, the second for:

for(j=0; j<i; j++) {
        printf("blurp:  %d\r\n", j);
        printf("%s  @  %s\r\n", contacts[j].persoon, contacts[j].domain);
    }

so.
That's my piece of code .. (where it is about).
But ... I got this as output:

// Removed, sensitive information //

As u see: For some strange reason, the second email adres isn't shown at all. But when I change some values in the malloc(), it changes, and is (i.e.) the 3th value not complete.
So, I think the problem is in the malloc().

And then, the second for(), U see it isn't printing any value at all! except the last adress ..

Anyone knows why this is, yeah, why it is like it is ? :P

Oh, and, my split() function looks like this:

char *split(char *token, char *str, char *outp[9]) {
    char *p;
    int i = 0;

    p = strtok (str, token);
    
    while (p != NULL) {
        outp[ i ] = malloc( strlen( p ) + 1 );
        strcpy( outp [ i ], p );	

        p = strtok (NULL, token);

        i++;
    }

    return 0;
}

Best Regards,
Niek van der Steen

Offline

#2 2007-10-28 04:43 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: memory storage problem

Run it through valgrind.

(No time or energy to point out all the mistakes, sorry.)

Offline

#3 2007-10-28 04:56 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: memory storage problem

I think your main problem is this:

contacts = malloc(sizeof(struct contactsstruct));
...
        strcpy(contacts[i].domain, tmpContacts[1]);
        strcpy(contacts[i].persoon, tmpContacts[0]);

So, you only allocate enough space in "contacts" for a single structure (yet, you
reallocate and overwrite that pointer every time through the loop!), yet you index it
as if it were an array of multiple structures...  So, you're just trashing memory...

What you need to do is determine up front how many structs you'll need then
calloc() that number of them for "contacts"...  Or, alternatively, you can use realloc()
inside the loop to grow by one more every time...  But, your current approach is
just nonsensical...

Offline

#4 2007-10-28 05:42 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: memory storage problem

ah now I understand
I need to create an array wich holds pointers to the structures, not ?

because contacts is a pointer to one structure ..

but how .. :confused:

Offline

#5 2007-10-28 06:10 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: memory storage problem

In cases like this it's better to use a linked list.

Offline

#6 2007-10-28 06:14 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: memory storage problem

Hmm..

I would proberbly sound foulish now, but, what is it ? A linked list ?

Offline

#7 2007-10-28 11:23 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: memory storage problem

Offline

#8 2007-10-29 05:36 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: memory storage problem

Isn't there a good lib wich can do this for me ?
because now I need 999999999 lines of code, just to generate one XML string.

Offline

#9 2007-10-29 06:10 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: memory storage problem

Offline

#10 2007-10-29 06:19 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: memory storage problem

Offline

  • Index
  • » C
  • » memory storage problem

Board footer

Powered by FluxBB