Saving values in array wont work

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 705 Views - Last Post: 29 December 2011 - 08:06 AM Rate Topic: -----

#1 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Saving values in array wont work

Posted 18 December 2011 - 10:12 AM

hello,
so i have written that code

///////////////////////////////////////////////////
template <typename T>
inline void Push(T value)
{
	// If the stack if full then dont add more
	if(m_uiArgs >= MAX_ARGS_VALUES)
		return;

	// Save the argument into the virtual stack
	*(T *)(m_ucValue + m_uiArgs * 4) = value;
	// Increase the arguments count
	m_uiArgs++;
		
}
///////////////////////////////////////////////////
template <typename T>
inline T Pop()
{
        // Decrease the arguments count
	m_uiArgs--;
	// Return the value
        return *(T *)(m_ucValue + m_uiArgs * 4);
			
}

private:
	unsigned int		m_uiArgs;
	unsigned char		m_ucValue[MAX_ARGS_VALUES * 4];




now this code normally saves values into the "m_ucValues" array when using the push function, then getting them when using the Pop, but what heppens is after pushing values, it only pops the last pushed value

Example:

Push(1500); // Integer
Push("test string"); // String
Push("test 2");

printf("%s - %s - %d", Pop<char *>(), Pop<char *>(), Pop<int>());



Output:
test 2, test 2, 5465465456(cannot convert the string to integer)



im not sure if the last pushed value overwrites the previous ones, or the Pop function is bugged

any help will be greatly appreciated :)
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Saving values in array wont work

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: Saving values in array wont work

Posted 18 December 2011 - 01:37 PM

Could you post your entire code please? Also, a good point I've seen in a few books and websites regarding templates in C++, is to write the code without templates first. Get it to work for a simple data type like int or double. Then proceed to make it a template. This way, you should be able to make sure that there's no bugs in the original code. If you have problems with the template version, you know that it has something to do with the template and not your actual implementation of whatever it is you are implementing. In your case, it would be a stack. So write a stack of integers, get it to work and thoroughly test it. When you feel confident that there are no bugs, it should only be minor tweaks you'll have to make to make it a template class/function.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: Saving values in array wont work

Posted 18 December 2011 - 01:50 PM

I would also suggest that you may want to use C++ iostreams instead of C stdio functions.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Re: Saving values in array wont work

Posted 18 December 2011 - 02:15 PM

thanks for your replys above
so it seems the problem is not from the code, but its from the way im calling it
cuz i tryed it with a differente way, and it seems to work

This post has been edited by James_Alex: 25 December 2011 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Re: Saving values in array wont work

Posted 25 December 2011 - 08:03 AM

ok, now i have a differente kind of problem
what i want to is to send the array over a TCP socket connection, i have every thing setup

struct stPacket
{
    unsigned char *ucData;
    unsigned int uiArgs;
};



now i copy that array into the packet array like that

memcpy(&packet.ucData, &pFunctionArguments->m_ucValue, sizeof(pFunctionArguments->m_ucValue));



then send it, and then when receiving it, i copy back the packet data pointer into another function arguments pointer

when poping an integer, it works, but when poping a string it wont, it simply gives random characters, so is there something im doing wrong ? thanks
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: Saving values in array wont work

Posted 25 December 2011 - 08:16 AM

Please show the smallest possible complete program that illustrates your problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Saving values in array wont work

Posted 25 December 2011 - 08:52 AM

The string class stores it's data in a dynamically allocated array. When you call memcpy() it only copies the pointer, so when you receive the packet you have a pointer pointing to a "random" location.
Was This Post Helpful? 1
  • +
  • -

#8 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Re: Saving values in array wont work

Posted 25 December 2011 - 09:31 AM

View PostHezekiah, on 25 December 2011 - 04:52 PM, said:

The string class stores it's data in a dynamically allocated array. When you call memcpy() it only copies the pointer, so when you receive the packet you have a pointer pointing to a "random" location.


im not using a string class, im using a char variable type
Was This Post Helpful? 0
  • +
  • -

#9 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Saving values in array wont work

Posted 25 December 2011 - 09:37 AM

Quote

im not using a string class, im using a char variable type

You said you were:

Quote

when poping an integer, it works, but when poping a string it wont, it simply gives random characters, so is there something im doing wrong ? thanks

Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Saving values in array wont work

Posted 25 December 2011 - 09:54 AM

View PostJames_Alex, on 25 December 2011 - 11:31 AM, said:

View PostHezekiah, on 25 December 2011 - 04:52 PM, said:

The string class stores it's data in a dynamically allocated array. When you call memcpy() it only copies the pointer, so when you receive the packet you have a pointer pointing to a "random" location.


im not using a string class, im using a char variable type

Doesn't matter. When you sent an int you were sending actual data. Now you're not sending the char array; you're just sending a pointer. But pointer to what? The computer that receives the packet doesn't have that char array at that address. I'm surprised that it prints anything at all. I would have expected it to crash due to invalid memory access.
Was This Post Helpful? 1
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: Saving values in array wont work

Posted 25 December 2011 - 09:54 AM

You need to show more code. How are you trying to write the information? How are you trying to read the information?

Jim
Was This Post Helpful? 0
  • +
  • -

#12 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Re: Saving values in array wont work

Posted 25 December 2011 - 11:28 AM

@r.stiltskin: yes im aware that it is actually sending a pointer, but thats why i was asking for help, sorry for describing my problem badly

now let me resume, i have written that (pastebin), then i copy the the "m_ucData" to another declared pointer into a struct

struct stPacket
{
    unsigned char *ucData;
    unsigned int uiArgs;
};

memcpy(&packet.ucData, &pFunctionArguments->m_ucData, sizeof(pFunctionArguments->m_ucData));



and send it, and then when receiving, i copy back to another CFunctionArguments pointer, it works perfect with integers, dword, byte ... , but with string it wont (not actually the string class, but using the "char" type), and i know cuz with strings it only sends a pointer

so is there anyway to send strings with that class ?
Thanks

This post has been edited by James_Alex: 25 December 2011 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#13 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: Saving values in array wont work

Posted 25 December 2011 - 11:41 AM

View Postjimblumberg, on 25 December 2011 - 10:16 AM, said:

Please show the smallest possible complete program that illustrates your problem.

Jim

Just want to reemphasize that we can help you better if you post the smallest complete program that illustrates your problem. Just posting a snippet might help, but the problem could be somewhere else and we don't know that until we see the whole program.
Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Saving values in array wont work

Posted 25 December 2011 - 11:59 AM

The class isn't the problem. The problem is in the stPacket. You're trying to copy the entire char array into a struct that only has room for a char pointer + an int.

Make a bigger packet.

struct stPacket
{
    unsigned char ucData[ /* max number of chars in array */ ];
    unsigned int uiArgs;
};


Was This Post Helpful? 0
  • +
  • -

#15 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Re: Saving values in array wont work

Posted 25 December 2011 - 03:42 PM

View Postr.stiltskin, on 25 December 2011 - 07:59 PM, said:

The class isn't the problem. The problem is in the stPacket. You're trying to copy the entire char array into a struct that only has room for a char pointer + an int.

Make a bigger packet.

struct stPacket
{
    unsigned char ucData[ /* max number of chars in array */ ];
    unsigned int uiArgs;
};



i changed it to a bigger packet, but its still the same thing, it only works for integers, what i need to a way to store strings(not pointer to them) without having to make a special function for them
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2