2 Replies - 828 Views - Last Post: 06 June 2014 - 11:22 PM Rate Topic: -----

#1 expir   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-August 12

Problem with allocating memory for a global char array

Posted 05 June 2014 - 03:22 PM

Hi,
i'm making a little program in c++ and i've ran on a small problem.
I want to make a global char array called password which would store the password entered by the user for the whole time the program is using.
What i've tried is doing it like that:
main.h:
extern char* szUserPassword;

main.cpp:
char* szUserPassword;


The other file where I am trying to allocate the memory:
szUserPassword = (char*) malloc(sizeof(char) * strlen(inputtext));



And the error is:

Quote

error LNK2001: unresolved external symbol "char * szUserPassword" ([email protected]@3PADA)


However, it works locally:
char* testallocation;
testallocation = (char *) malloc(sizeof(char) * strlen(inputtext));


But the problem is that I need it globally not locally.
I have no ideas how to fix that, searched the internet but I dont even know how i would call this problem.
Thanks in advance :P

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with allocating memory for a global char array

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Problem with allocating memory for a global char array

Posted 05 June 2014 - 04:17 PM

First why the global variable?

Next show the smallest possible complete program that illustrates the problem.

Lastly, you did include main.h in that "other" c file, correct? And in a C program you shouldn't be casting the return value from malloc.



Jim
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Problem with allocating memory for a global char array

Posted 06 June 2014 - 11:22 PM

> The other file where I am trying to allocate the memory:
Both otherfile.cpp and main.cpp need to include main.h

Also, as this is a link error, you need to make sure all the source files are listed in your project.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1