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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:09:00+00:00 2026-06-12T08:09:00+00:00

I’m writing some code in pure C, RHEL 5.5 x64, gcc version 4.1.2 20080704

  • 0

I’m writing some code in pure C, RHEL 5.5 x64, gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)

I’ve got an executable which is compiled using 2 static libraries. Each of them uses some global (for a given library) array variables (i.e. in some .c file I have a
char var1[VAR_SIZE1];, then in other .c files of the lib I use extern char var1[VAR_SIZE1];, and the same situation with the second lib).
Each library is placed in it’s own subdir of an executable source dir.
I’ve started to notice that one lib can place its data in another lib’s memory. To discover what’s going on, I’ve defined a void pointer to one of the variables in a third lib, (lib 3 is used by both libs in question), assigned this pointer an address in lib1 and looked at the addresses in lib2.
What I see now is:

char var1[1000]; /*please mind the length*/
extern void* ptr_to_var2;
printf("var1 addr is %p, var2 addr is %p\n", (void*)&var1, ptr_to_var2);

this code produces

var1 addr is 0xa11b00, var2 addr is 0xa11e00

As you can see, var1 should end up at address 0xa11ee8, and var2’s head is INSIDE var1 memory.
What am I doing wrong? Are there any limitations on using global vars in static libs? I can’t define those vars as static, as they are used throughout a number of files inside each lib, and making those vars dynamic is a big work (there are dozens of such variables).

P.S. Of course, ptr_to_var2 is initialized. What really is going on

<lib3>
void* ptr_to_var2;

<lib2>
#include "hdr_with_my_struct_name_definition.h"
my_struct_name var2[5];
extern void* ptr_to_var2;

int func2(){
   ptr_to_var2=(void*)&var2;
   var2[0].fld1=12345;
   return 1;
}

<lib 1>
#include "hdr_with_my_struct_name_definition.h"
char var1[1000];
extern void* ptr_to_var2;

int func1(){
   my_struct_name *temp_var_2=(my_struct_name*)ptr_to_var2;
   printf("%d", temp_var_2[0].fld1);
   memset(var1, '\0', sizeof(var1);
   printf("%d", temp_var_2[0].fld1);
   return 1;
}

<binary>

main(){
   func2();
   func1();
   return;
}

All of this returns
12345
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-06-12T08:09:01+00:00Added an answer on June 12, 2026 at 8:09 am
    printf("var1 addr is %p, var2 addr is %p\n", (void*)&var1, ptr_to_var2);
    

    In this, you are trying to print value of ptr_to_var2 not actually the address of it. As this variable is not initialized yet, its value is random as can be anything.

    #include<stdio.h>
    
    char v[1000];
    
    void *p1;
    
    int main() {
    
        printf ("var1 %p, var2 %p  &var2 %p\n", &v[0], p1, &p1);
    
        return 0;
    }
    

    gives:

    var1 0x601040, var2 (nil)  &var2 0x601428
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.