Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3599816
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:24:54+00:00 2026-05-18T20:24:54+00:00

I have a dynamic array of structures, so I thought I could store the

  • 0

I have a dynamic array of structures, so I thought I could store the information about the array in the first structure.

So one attribute will represent the amount of memory allocated for the array and another one representing number of the structures actually stored in the array.

The trouble is, that when I put it inside a function that fills it with these structures and tries to allocate more memory if needed, the original array gets somehow distorted.

Can someone explain why is this and how to get past it?
Here is my code

#define INIT 3

typedef struct point{
    int x;
    int y;
    int c;
    int d;
}Point;
Point empty(){
    Point p;
    p.x=1;
    p.y=10;
    p.c=100;
    p.d=1000; //if you put different values it will act differently - weird
    return p;
}
void printArray(Point * r){
    int i;
    int total = r[0].y+1;
    for(i=0;i<total;i++){
          printf("%2d | P [%2d,%2d][%4d,%4d]\n",i,r[i].x,r[i].y,r[i].c,r[i].d);
    }
}

void reallocFunction(Point * r){
    r=(Point *) realloc(r,r[0].x*2*sizeof(Point));
    r[0].x*=2;
}
void enter(Point* r,int c){
    int i;
    for(i=1;i<c;i++){
        r[r[0].y+1]=empty();
        r[0].y++;
        if( (r[0].y+2) >= r[0].x ){ /*when the amount of Points is near
                                     *the end of allocated memory.
                                      reallocate the array*/
            reallocFunction(r);
        }
    }
}

int main(int argc, char** argv) {
    Point * r=(Point *) malloc ( sizeof ( Point ) * INIT );
    r[0]=empty();
    r[0].x=INIT;    /*so here I store for how many "Points" is there memory
                    //in r[0].y theres how many Points there are.*/
    enter(r,5);
    printArray(r);
    return (0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:24:55+00:00Added an answer on May 18, 2026 at 8:24 pm

    Your code does not look clean to me for other reasons, but…

    void reallocFunction(Point * r){
        r=(Point *) realloc(r,r[0].x*2*sizeof(Point));
        r[0].x*=2;
        r[0].y++;
    }
    

    The problem here is that r in this function is the parameter, hence any modifications to it are lost when the function returns. You need some way to change the caller’s version of r. I suggest:

    Point *   // Note new return type...
    reallocFunction(Point * r){
        r=(Point *) realloc(r,r[0].x*2*sizeof(Point));
        r[0].x*=2;
        r[0].y++;
    
        return r; // Note: now we return r back to the caller..
    }
    

    Then later:

    r = reallocFunction(r);
    

    Now… Another thing to consider is that realloc can fail. A common pattern for realloc that accounts for this is:

    Point *reallocFunction(Point * r){
        void *new_buffer = realloc(r, r[0].x*2*sizeof(Point));
    
        if (!new_buffer)
        {
           // realloc failed, pass the error up to the caller..
           return NULL;
        }
    
        r = new_buffer;
        r[0].x*=2;
        r[0].y++;
    
        return r;
    }
    

    This ensures that you don’t leak r when the memory allocation fails, and the caller then has to decide what happens when your function returns NULL…

    But, some other things I’d point out about this code (I don’t mean to sound like I’m nitpicking about things and trying to tear them apart; this is meant as constructive design feedback):

    • The names of variables and members don’t make it very clear what you’re doing.
    • You’ve got a lot of magic constants. There’s no explanation for what they mean or why they exist.
    • reallocFunction doesn’t seem to really make sense. Perhaps the name and interface can be clearer. When do you need to realloc? Why do you double the X member? Why do you increment Y? Can the caller make these decisions instead? I would make that clearer.
    • Similarly it’s not clear what enter() is supposed to be doing. Maybe the names could be clearer.
    • It’s a good thing to do your allocations and manipulation of member variables in a consistent place, so it’s easy to spot (and later, potentially change) how you’re supposed to create, destroy and manipulate one of these objects. Here it seems in particular like main() has a lot of knowledge of your structure’s internals. That seems bad.
    • Use of the multiplication operator in parameters to realloc in the way that you do is sometimes a red flag… It’s a corner case, but the multiplication can overflow and you can end up shrinking the buffer instead of growing it. This would make you crash and in writing production code it would be important to avoid this for security reasons.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're looking at doing a MOSS 2007 site which will have a fairly dynamic
I have a variable of type Dynamic and I know for sure one of
I have a dynamic query that returns around 590,000 records. It runs successfully the
I have a Dynamic Data Entities Web Application that uses a database with GUIDs
I have a dynamic class that serves as a storage container for configuration settings.
How can I have a dynamic variable setting the amount of rows to return
When C# 4.0 comes out and we have the dynamic keyword as described in
(Eclipse 3.4, Ganymede) I have an existing Dynamic Web Application project in Eclipse. When
I have recently written a dynamic querying tool using expression trees and as I
I have a custom built ajax [div] based dynamic dropdown. I have an [input]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.