Jump to content
  • Advertisement
Sign in to follow this  
blackeyed

Dynamic memory allocation problems...

This topic is 3409 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm going crazy...I'm new to dynamic memory allocation and all that and now it appears I can't pass by reference... :shock: when I call Add() it gives me an address...or garbage for alloc, and ele it also segfaults like no other and I am clueless :x.
void getstuff(info *strux){
     info Anon;
     int count=0;
     int element=0;
     int allocated=0;
     Anon.Name=malloc(40*(sizeof(char)));
     Anon.Address=malloc(60*(sizeof(char)));
     Anon.Loc=malloc(40*(sizeof(char)));
     
     for(count=0; count<50; count++){
                 
                  fgets(Anon.Name,40,stdin);
                  if(Anon.Name==NULL)
                  break;
                  fgets(Anon.Address,60,stdin);
                  fgets(Anon.Loc,40,stdin);
                  scanf("%d",&Anon.zip);
                  allocated++;
                  element++;
                  printf("%d \n \n",allocated);
                  Add(Anon, *strux, &allocated, &element);
              system("PAUSE");
     
               
                 
     }
     
     }
void pushstuff(int n, info *list[]){
     
     
     }
void Add(info object,info *thearray,int *alloc,int *ele){
printf("alloc %d \n",*alloc);
             
     if (*alloc<49){
             
     alloc++;
     }
     else{
           printf("50 max!");
     return;
     }
     thearray=(info*)realloc(thearray,*alloc * sizeof(info));
     thearray[*ele]=object;
     *ele++;
     }

Share this post


Link to post
Share on other sites
Advertisement
What is the intent of this code, i.e. what needs to happen when it executes? I'm having a little trouble following the code with no context.

Share this post


Link to post
Share on other sites
Quote:
Original post by blackeyed
when I call Add() it gives me an address...or garbage for alloc



/* In the old code, you're passing a copy of the pointer to the array.
Changing this pointer DOES NOT change the pointer that
was passed into it, only the copy. Therefore if you
reassign this copy of the pointer to new address (and
invalidating the old address), the other pointer(s)
pointing to the old array are now dangling pointers.
Instead, pass a pointer-to-a-pointer so that you can
control the actual pointer being passed in. */

void Add( info object, /* info* */ info** thearray, int *alloc, int *ele )
{
printf( "alloc %d \n", *alloc );

/* Indexes go from [0 max) */
if ( *alloc < 50 /* 49 */ )
{
/* alloc++; Whoops, now alloc points to memory that contains something that is not *alloc */
(*alloc)++;
}
else
{
printf( "50 max!" );
return;
}

/* Re-allocate the array, and store the new memory address into
the pointer passed in; the old memory is free'd */

*thearray = (info*)realloc(thearray,*alloc * sizeof(info));
(*thearray)[*ele] = object;
*ele++;
}






[Edited by - _fastcall on May 20, 2009 11:25:44 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
Quote:
Original post by blackeyed
when I call Add() it gives me an address...or garbage for alloc


*** Source Snippet Removed ***


thanks for the replies guys,
basically the program is supposed to create an array of pointers to structs which I will sort later on.

okay I've done what you said and sort of understand where I went wrong however I'm still segfaulting somewhere and I feel pretty retarded that I haven't found out where though...


//main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Funcs.h"




int main()
{
info **stru;
info **pointp;

getstuff(stru);
love(stru);
system("PAUSE");
return 0;
}




//func.h

void love();
void getstuff();
void Add();
typedef struct info{
char *Name;
char *Address;
char *Loc;
int zip;
}info;

Share this post


Link to post
Share on other sites
The code you posted won't even compile, since your declarations of the love and getstuff functions don't match the parameters you pass in main.

It would be helpful if you could post the exact code that you're working on [smile]


Also, I'm moving this over to For Beginners since you'll likely get better responses from there.

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
The code you posted won't even compile, since your declarations of the love and getstuff functions don't match the parameters you pass in main.

It would be helpful if you could post the exact code that you're working on [smile]


Also, I'm moving this over to For Beginners since you'll likely get better responses from there.


but it does compile? That is the exact code I am working on...I'm using bloodshed dev c/c++

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
The code you posted won't even compile, since your declarations of the love and getstuff functions don't match the parameters you pass in main.


Unfortunately, in C it will. Functions declared with () in C are not the same as functions declared with (void).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!