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

  • SEARCH
  • Home
  • 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 6584997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:36:25+00:00 2026-05-25T16:36:25+00:00

I have the following code: #include <stdio.h> #include <stdlib.h> #define OUT void getDataFromServer(OUT int**

  • 0

I have the following code:

#include <stdio.h>
#include <stdlib.h>
#define OUT

void getDataFromServer(OUT int** array, OUT int* size)
{
    static int tmpArr[] = {0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                        0x10, 0x11, 0x12, 0x13,  0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
    *size = sizeof tmpArr / sizeof(int);
    printf("Before realloc: %p\n", *array);
    *array = realloc(*array, sizeof(*array) * *size);
    printf("After realloc : %p\n", *array);
    int i=0;
    for (; i < *size; i++)
    {
        (*array)[i] = tmpArr[i];
    }
}

int main(void)
{
    int size = 0;
    int* dataFromServer = malloc(sizeof *dataFromServer);
    printf("in main: %p\n", dataFromServer);
    getDataFromServer(&dataFromServer, &size);

    int x;
    for (x=0; x < size; x++)
        printf("%d ", dataFromServer[x]);
    printf("\n\n");
    free(dataFromServer);
    return 0;
}

Output:

in main: 003F1708
Before realloc: 003F1708
After realloc : 003F3430
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

From the output, the realloc returns a pointer to a new memory address..

So the question is, should I free this location explicitly- besides freeing the location created by original malloc?

Or it is doing what was desired by the code above which is Just expand the memory location reserved previously?

Thanks.

EDIT: Actually, each answer below provided me with a valuable piece of info. And because I’ve to choose just one Answer to accept. I’ve chosen the one that corrected my above code!

  • 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-25T16:36:26+00:00Added an answer on May 25, 2026 at 4:36 pm

    After you called realloc and it returned some pointer you should forget about the previous one and just keep the “new” pointer.

    If realloc resized it, then realloc returns it, if it allocated a new space in memory and copied the previous content of it it will free the old pointer and return a new one.

    But, never overwrite the old pointer with the result of a call to realloc (as you’re doing in your code): in facts, when realloc fails it returns NULL and does not free the old pointer, so, if you are overwriting the only variable where you stored it, you are losing the only way you have to free that memory, and thus you have a memory leak. So, the “canonical” way to call realloc is:

    /* assuming myPtr contains the malloced memory */
    void * tempPtr=realloc(myPtr, newSize);
    if(tempPtr==NULL)
    {
        /* realloc failed, handle the error and, if aborting, free myPtr */
    }
    else
        myPtr = tempPtr;
    
    /* ... */
    
    /* when you no longer need it free the memory */
    free(myPtr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <stdlib.h> #include <stdio.h> #define SIZE 100 int* arr;
i have following code #include <stdlib.h> #include <stdio.h> int sorter( const void *first_arg,const void*
I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
I have the following code: #include <stdlib.h> #include <stdio.h> typedef void (*func_t)(void * data);
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
I have the following code: #include <boost/shared_ptr.hpp> struct Foo { int a; }; static
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
I have the following code #include <stdio.h> #include <iostream> #include <stdlib.h> #include <stdint.h> using

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.