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 8528837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:53:31+00:00 2026-06-11T08:53:31+00:00

My main intention is to pass a pointer to a structure, to a function,

  • 0

My main intention is to pass a pointer to a structure, to a function, which will allocate memory and fill all its values. After returning back i will print it on screen.

Structure looks like this.

struct isolock{
    char *name;
    unsigned int state;};
typedef struct isolock lock;

Please note i can not change declaration of this structure. My main function is very simple like this.

int main(){
    int i = 0;
    isolock *lock = NULL;
    fillArray(&lock);

    //print struct contents
    for(i=0;(lock+i)->name;i++){
            printf("name = %s status = %u \n", (lock+i)->name,(lock+i)->state);
    }

}

fillArray function is suppose to allocate n + 1 memory blocks for my structure where n is the actual number of locks present. whereas last (n+1)th block will be filled with zero. So that while printing from main, i can just check for this condition, without need of worrying about length of structures which i have allocated.

My problem is in fillArray function.

static const char *array[] = {"SWMGMT_LOCK","OTHER_LOCK"};
void fillArray(isolock **dlock){
    int i = 0;
    if (*dlock == NULL){
            *dlock = (isolock *)malloc((sizeof (*dlock)) * 3);      //allocate space for holding 3 struct values

            for(i=0;i<2;i++){
                    (*(dlock) + i)->name = strdup(array[i]);       
                    (*(dlock) + i)->state = (unsigned int)(i+1);                                           
            }
            (*(dlock) + i)->name = 0;   //LINE100
            (*(dlock) + i)->state = 0;
    }
}
o/p:

name =  status = 1 
name = OTHER_FP_LOCK status = 2 

Actually this LINE100 causes problem. It actually replaces already filled structure entry i.e., this line makes dlock[0]->name to be filled with 0. whereas dlock[1] remains unchanged.

My gdb log shows something like this.

All these are logs are taken after allocation and filling values.

    (gdb) p (*(dlock)+0)
    $10 = (isolock *) 0x804b008
    (gdb) p (*(dlock)+1)
    $11 = (isolock *) 0x804b010 
    (gdb) p (*(dlock)+2)                      <== Note here 1
    $12 = (isolock *) 0x804b018
    (gdb) p *(*(dlock)+0)
    $13 = {name = 0x804b018 "SWMGMT_LOCK", state = 1}     <== Note here 2
    (gdb) p *(*(dlock)+1)
    $14 = {name = 0x804b028 "OTHER_FP_LOCK", state = 2}
    (gdb) n
    33    (*(dlock) + i)->state = 0;
    (gdb) 
    35   }
    (gdb) p *(*(dlock)+2)
    $15 = {name = 0x0, state = 0}
    (gdb) p (*(dlock)+2)
    $16 = (isolock *) 0x804b018

From note 1 and note 2 its very clear that, strdup has returned already allocated memory location by malloc i.e., first call to strdup has returned the address of dlock[2]. how could this happen. because of this, (*dlock+2)->name = 0 has caused dlock[0]->name to be filled with 0.

To easily explain this problem,
Malloc has returned me three addresses. for easy understanding lets say, {1000,1008,1010}
Two times i ve called strdup which has returned {1010,1018}

this 1010 and 1018 are stored in char *name of lock[0] and lock[1] respectively.

Can someone tell me, am i doing something wrong in this code or is this problem of strdup ( allocating a already allocated block of memory)

NOTE: When i have changed char *name to char name[20] and instead of strdup, i used strcpy and it worked perfectly.

  • 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-06-11T08:53:33+00:00Added an answer on June 11, 2026 at 8:53 am

    A possible cause of the error is your allocation:

    *dlock = (isolock *)malloc((sizeof (*dlock)) * 3);
    

    Here dlock is a pointer to a pointer, so sizeof(*dlock) is the size of a pointer which is not the same as sizeof(struct isolock) (or sizeof(**dlock)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My intention was to use pyGTK's main loop to create a function that blocks
My intention is to have 4 main Nav-bars at a site. If the user
Main thread of application is doing some work. There also exists a timer, which
main.c (with all the headers like stdio, stdlib, etc): int main() { int input;
As I wrote an app with the function main(int argc, char * argv[]) When
Goal : My intention to design a utility that could access any numeric values(
Trying to learn pointer math better I wrote this code. The intention was to
My main intention is just use some functions in the ann library in Pylab
My intention is to create a menu at the top of the page which
I have a php file as my main page, and I want to pass

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.