   Dynamic memory allocation problems...
  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++;}
  Is this C, or C++? (There might be a giveaway in there somewhere, but if so I didn't see it.)   ;  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.   ;Quote:Original post by blackeyedwhen 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];Quote:Original post by _fastcallQuote:Original post by blackeyedwhen 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.hvoid love();void getstuff();void Add(); typedef struct info{  char *Name;    char *Address; char *Loc; int zip;}info;;  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.   ;Quote:Original post by ApochPiQThe 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++   ;Quote:Original post by ApochPiQThe 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).   